Class: Phronomy::Task::ThreadBackend

Inherits:
Backend
  • Object
show all
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

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.

Returns:

  • (Boolean)


53
54
55
# File 'lib/phronomy/task/thread_backend.rb', line 53

def alive?
  @thread.alive?
end

#awaitObject

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.

Returns:

  • (Object)

Raises:

  • (Exception)


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.

Returns:

  • (self)


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_errorException?

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.

Returns:

  • (Exception, nil)


79
80
81
# File 'lib/phronomy/task/thread_backend.rb', line 79

def completed_error
  @error
end

#completed_valueObject?

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.

Returns:

  • (Object, nil)


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.

Parameters:

  • limit (Numeric, nil) (defaults to: nil)

Returns:

  • (Thread, nil)


67
68
69
# File 'lib/phronomy/task/thread_backend.rb', line 67

def join(limit = nil)
  @thread.join(limit)
end