Class: Phronomy::Task::ThreadBackend
- Defined in:
- lib/phronomy/task/thread_backend.rb
Overview
Thread-based Task backend (default).
Each task runs on its own OS thread. Cancellation is delivered via +Thread#raise(CancellationError)+, which cooperates with +rescue+ clauses inside the block. This backend is always available and requires no external dependencies.
When the cooperative scheduler backend is introduced, this backend will remain available as the fallback for blocking I/O operations that must run outside the scheduler (e.g. inside BlockingAdapterPool).
Instance Method Summary collapse
- #alive? ⇒ Boolean private
- #await ⇒ Object private
- #cancel! ⇒ self private
- #completed_error ⇒ Exception? private
- #completed_value ⇒ Object? private
-
#initialize(task:, &block) ⇒ ThreadBackend
constructor
A new instance of ThreadBackend.
- #join(limit = nil) ⇒ Thread? private
Constructor Details
#initialize(task:, &block) ⇒ ThreadBackend
Returns a new instance of ThreadBackend.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/phronomy/task/thread_backend.rb', line 16 def initialize(task:, &block) super @value = nil @error = nil @thread = Thread.new do Thread.current.name = task.name if task.name Thread.current[:phronomy_current_task] = task Thread.current[:phronomy_task_cpu_slice_start_ms] = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) task.transition!(:running) @value = block.call task.transition!(:completed, value: @value) rescue CancellationError => e task.transition!(:cancelled, error: e) @error = e rescue => e task.transition!(:failed, error: e) @error = e ensure # Guard against Thread#raise firing before the rescue handler has a # chance to run (e.g. when cancel! is called immediately after spawn). task.transition!(:cancelled) unless task.done? end end |
Instance Method Details
#alive? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
53 54 55 |
# File 'lib/phronomy/task/thread_backend.rb', line 53 def alive? @thread.alive? end |
#await ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
44 45 46 47 48 49 |
# File 'lib/phronomy/task/thread_backend.rb', line 44 def await @thread.join raise @error if @error @value end |
#cancel! ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
59 60 61 62 |
# File 'lib/phronomy/task/thread_backend.rb', line 59 def cancel! @thread.raise(CancellationError, "Task cancelled") if @thread.alive? self end |
#completed_error ⇒ Exception?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
79 80 81 |
# File 'lib/phronomy/task/thread_backend.rb', line 79 def completed_error @error end |
#completed_value ⇒ Object?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
73 74 75 |
# File 'lib/phronomy/task/thread_backend.rb', line 73 def completed_value @value end |
#join(limit = nil) ⇒ Thread?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
67 68 69 |
# File 'lib/phronomy/task/thread_backend.rb', line 67 def join(limit = nil) @thread.join(limit) end |