Class: Phronomy::Task::Backend

Inherits:
Object
  • Object
show all
Defined in:
lib/phronomy/task/backend.rb

Overview

Abstract base class for Task execution backends.

A backend encapsulates the execution primitive (Thread, Fiber, etc.) and the lifecycle transitions it drives. Concrete backends must implement all abstract methods. The default concrete implementation is ThreadBackend.

Backends receive a reference to the owning Phronomy::Task so they can call #transition! at the appropriate lifecycle points.

Direct Known Subclasses

FiberBackend, ImmediateBackend, ThreadBackend

Instance Method Summary collapse

Constructor Details

#initialize(task:, &block) ⇒ Backend

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 a new instance of Backend.

Parameters:

  • task (Task)

    the owning Task (used for status callbacks)

  • block (Proc)

    the work to execute



17
18
19
20
# File 'lib/phronomy/task/backend.rb', line 17

def initialize(task:, &block)
  @task = task
  @block = block
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 +true+ while execution is still ongoing.

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/phronomy/task/backend.rb', line 34

def alive?
  raise NotImplementedError, "#{self.class}#alive? not implemented"
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.

Blocks until the task completes and returns its value. Re-raises errors from the block.

Returns:

  • (Object)

Raises:

  • (Exception)


27
28
29
# File 'lib/phronomy/task/backend.rb', line 27

def await
  raise NotImplementedError, "#{self.class}#await not implemented"
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.

Requests cancellation. Thread-based backends may use +Thread#raise+; cooperative backends should mark the task cancelled and rely on Phronomy::Task.checkpoint!.

Returns:

  • (self)

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/phronomy/task/backend.rb', line 43

def cancel!
  raise NotImplementedError, "#{self.class}#cancel! not implemented"
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 the exception raised by the task, or +nil+ on success/cancellation. Only valid to call after the task is done. Subclasses should override if they store errors.

Returns:

  • (Exception, nil)


71
72
73
# File 'lib/phronomy/task/backend.rb', line 71

def completed_error
  nil
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 the task's result value once it has reached a terminal state. Only valid to call after the task is done. Subclasses should override if they store the result.

Returns:

  • (Object, nil)


62
63
64
# File 'lib/phronomy/task/backend.rb', line 62

def completed_value
  nil
end

#join(limit = nil) ⇒ 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.

Joins the execution context with an optional timeout. Returns +nil+ when a non-nil +limit+ expires before completion, matching +Thread#join+ semantics.

Parameters:

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

Returns:

  • (Object, nil)

Raises:

  • (NotImplementedError)


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

def join(limit = nil)
  raise NotImplementedError, "#{self.class}#join not implemented"
end