Class: Phronomy::Task::ImmediateBackend

Inherits:
Backend
  • Object
show all
Defined in:
lib/phronomy/engine/task/immediate_backend.rb

Overview

Synchronous task backend that executes the block on the calling thread.

Used by Runtime::FakeScheduler to allow tests to verify cooperative scheduling behaviour without spawning additional Threads. The block runs to completion before #initialize returns, so #await and #join always return immediately.

Thread count invariant: ImmediateBackend never creates a new Thread.

Instance Method Summary collapse

Constructor Details

#initialize(task:, &block) ⇒ ImmediateBackend

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.

Executes block synchronously on the calling thread. Saves and restores Task.current so nested ImmediateBackend tasks compose correctly.

Parameters:

Yield Returns:

  • (Object)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/phronomy/engine/task/immediate_backend.rb', line 21

def initialize(task:, &block)
  super
  @value = nil
  @error = nil
  previous_task = Thread.current[:phronomy_current_task]
  Thread.current[:phronomy_current_task] = task
  task.transition!(:running)
  begin
    @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
    task.transition!(:cancelled) unless task.done?
    Thread.current[:phronomy_current_task] = previous_task
  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.

Always false — block has already completed by the time the task is visible to callers.

Returns:

  • (Boolean)


69
70
71
# File 'lib/phronomy/engine/task/immediate_backend.rb', line 69

def alive?
  false
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.

No-op: the block has already completed.

Returns:

  • (self)


76
77
78
# File 'lib/phronomy/engine/task/immediate_backend.rb', line 76

def cancel!
  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)


61
62
63
# File 'lib/phronomy/engine/task/immediate_backend.rb', line 61

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)


55
56
57
# File 'lib/phronomy/engine/task/immediate_backend.rb', line 55

def completed_value
  @value
end

#join(_limit = nil) ⇒ 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 immediately — nothing to wait for.

Parameters:

  • limit (Numeric, nil)

    ignored

Returns:

  • (self)


84
85
86
# File 'lib/phronomy/engine/task/immediate_backend.rb', line 84

def join(_limit = nil)
  self
end

#wait_resultObject

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 the block's return value, or re-raises its exception.

Returns:

  • (Object)

Raises:

  • (Exception)


47
48
49
50
51
# File 'lib/phronomy/engine/task/immediate_backend.rb', line 47

def wait_result
  raise @error if @error

  @value
end