Class: Temporalio::Internal::Worker::MultiRunner
- Inherits:
-
Object
- Object
- Temporalio::Internal::Worker::MultiRunner
- Defined in:
- lib/temporalio/internal/worker/multi_runner.rb
Defined Under Namespace
Classes: Event, InjectEventForTesting
Instance Method Summary collapse
- #apply_thread_or_fiber_block ⇒ Object
-
#initialize(workers:, shutdown_signals:) ⇒ MultiRunner
constructor
A new instance of MultiRunner.
-
#initiate_shutdown ⇒ Object
Clarify this is the only thread-safe function here.
-
#next_event ⇒ Object
Intentionally not an enumerable/enumerator since stop semantics are caller determined.
- #raise_in_thread_or_fiber_block(error) ⇒ Object
- #wait_complete_and_finalize_shutdown ⇒ Object
Constructor Details
#initialize(workers:, shutdown_signals:) ⇒ MultiRunner
Returns a new instance of MultiRunner.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/temporalio/internal/worker/multi_runner.rb', line 10 def initialize(workers:, shutdown_signals:) @workers = workers @queue = Queue.new @shutdown_initiated_mutex = Mutex.new @shutdown_initiated = false # Trap signals to push to queue shutdown_signals.each do |signal| Signal.trap(signal) { @queue.push(Event::ShutdownSignalReceived.new) } end # Start pollers Bridge::Worker.async_poll_all(workers.map(&:_bridge_worker), @queue) end |
Instance Method Details
#apply_thread_or_fiber_block ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/temporalio/internal/worker/multi_runner.rb', line 26 def apply_thread_or_fiber_block(&) return unless block_given? @thread_or_fiber = if Fiber.current_scheduler Fiber.schedule do @queue.push(Event::BlockSuccess.new(result: yield)) rescue InjectEventForTesting => e @queue.push(e.event) @queue.push(Event::BlockSuccess.new(result: e)) rescue Exception => e # rubocop:disable Lint/RescueException Intentionally catch all @queue.push(Event::BlockFailure.new(error: e)) end else Thread.new do @queue.push(Event::BlockSuccess.new(result: yield)) rescue InjectEventForTesting => e @queue.push(e.event) @queue.push(Event::BlockSuccess.new(result: e)) rescue Exception => e # rubocop:disable Lint/RescueException Intentionally catch all @queue.push(Event::BlockFailure.new(error: e)) end end end |
#initiate_shutdown ⇒ Object
Clarify this is the only thread-safe function here
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/temporalio/internal/worker/multi_runner.rb', line 55 def initiate_shutdown should_call = @shutdown_initiated_mutex.synchronize do break false if @shutdown_initiated @shutdown_initiated = true end return unless should_call @workers.each(&:_initiate_shutdown) end |
#next_event ⇒ Object
Intentionally not an enumerable/enumerator since stop semantics are caller determined
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/temporalio/internal/worker/multi_runner.rb', line 76 def next_event # Queue value is one of the following: # * Event - non-poller event # * [worker index, :activity/:workflow, bytes] - poll success # * [worker index, :activity/:workflow, error] - poll fail # * [worker index, :activity/:workflow, nil] - worker shutdown # * [nil, nil, nil] - all pollers done result = @queue.pop if result.is_a?(Event) result else worker_index, worker_type, poll_result = result if worker_index.nil? || worker_type.nil? Event::AllPollersShutDown.instance else worker = @workers[worker_index] case poll_result when nil Event::PollerShutDown.new(worker:, worker_type:) when Exception Event::PollFailure.new(worker:, worker_type:, error: poll_result) else Event::PollSuccess.new(worker:, worker_type:, bytes: poll_result) end end end end |
#raise_in_thread_or_fiber_block(error) ⇒ Object
50 51 52 |
# File 'lib/temporalio/internal/worker/multi_runner.rb', line 50 def raise_in_thread_or_fiber_block(error) @thread_or_fiber&.raise(error) end |
#wait_complete_and_finalize_shutdown ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/temporalio/internal/worker/multi_runner.rb', line 66 def wait_complete_and_finalize_shutdown # Wait for them all to complete @workers.each(&:_wait_all_complete) # Finalize them all Bridge::Worker.finalize_shutdown_all(@workers.map(&:_bridge_worker)) end |