Module: Phronomy::ThreadActorRegistry

Defined in:
lib/phronomy/thread_actor_registry.rb

Overview

Global per-thread-id Actor registry.

Maps the +:thread_id+ key from the +config:+ argument passed to Agent::Base#invoke to a Actor instance. Each thread_id gets exactly one Actor so that all operations for the same conversation are serialised automatically.

Examples:

Phronomy::ThreadActorRegistry.for("user-42").call do
  # runs sequentially on the Actor's thread
end

Class Method Summary collapse

Class Method Details

.clear_allObject

Stops and removes every registered Actor. Intended for test teardown and process shutdown.



38
39
40
41
# File 'lib/phronomy/thread_actor_registry.rb', line 38

def clear_all
  actors = @registry_actor.call { @actors.values.tap { @actors.clear } }
  actors.each(&:stop)
end

.each_actor {|Phronomy::Actor| ... } ⇒ Object

Yields each currently registered Actor. A snapshot is taken so the registry cannot change while callers iterate.

Yields:



47
48
49
# File 'lib/phronomy/thread_actor_registry.rb', line 47

def each_actor(&block)
  @registry_actor.call { @actors.values.dup }.each(&block)
end

.for(thread_id) ⇒ Phronomy::Actor

Returns (or lazily creates) the Actor for +thread_id+.

Parameters:

  • thread_id (String)

Returns:



24
25
26
# File 'lib/phronomy/thread_actor_registry.rb', line 24

def for(thread_id)
  @registry_actor.call { @actors[thread_id] ||= Actor.new }
end

.stop(thread_id) ⇒ Object

Gracefully stops the Actor for +thread_id+ and removes it from the registry. The next call to for with the same id creates a fresh Actor.

Parameters:

  • thread_id (String)


32
33
34
# File 'lib/phronomy/thread_actor_registry.rb', line 32

def stop(thread_id)
  @registry_actor.call { @actors.delete(thread_id) }&.stop
end