Class: ActiveSaga::Execution
- Inherits:
-
Object
- Object
- ActiveSaga::Execution
- Defined in:
- lib/active_saga/execution.rb
Overview
Lightweight value object representing a workflow execution.
Instance Attribute Summary collapse
-
#cancelled_at ⇒ Object
readonly
Returns the value of attribute cancelled_at.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#ctx ⇒ Object
readonly
Returns the value of attribute ctx.
-
#cursor_step ⇒ Object
readonly
Returns the value of attribute cursor_step.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
-
#workflow_class ⇒ Object
readonly
Returns the value of attribute workflow_class.
Instance Method Summary collapse
-
#await(timeout: nil, interval: 0.5) ⇒ Object
Blocks until execution reaches a terminal state or timeout expires.
- #cancel!(reason: nil) ⇒ Object
- #cancelled? ⇒ Boolean
- #completed? ⇒ Boolean
- #failed? ⇒ Boolean
-
#initialize(id:, workflow_class:, state:, ctx:, cursor_step:, created_at:, updated_at:, cancelled_at: nil, store: ActiveSaga.store) ⇒ Execution
constructor
A new instance of Execution.
- #reload! ⇒ Object
- #terminal? ⇒ Boolean
Constructor Details
#initialize(id:, workflow_class:, state:, ctx:, cursor_step:, created_at:, updated_at:, cancelled_at: nil, store: ActiveSaga.store) ⇒ Execution
Returns a new instance of Execution.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/active_saga/execution.rb', line 8 def initialize(id:, workflow_class:, state:, ctx:, cursor_step:, created_at:, updated_at:, cancelled_at: nil, store: ActiveSaga.store) @id = id @workflow_class = workflow_class @state = state @ctx = ActiveSaga::Context.new(ctx) @cursor_step = cursor_step&.to_sym @created_at = created_at @updated_at = updated_at @cancelled_at = cancelled_at @store = store end |
Instance Attribute Details
#cancelled_at ⇒ Object (readonly)
Returns the value of attribute cancelled_at.
6 7 8 |
# File 'lib/active_saga/execution.rb', line 6 def cancelled_at @cancelled_at end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
6 7 8 |
# File 'lib/active_saga/execution.rb', line 6 def created_at @created_at end |
#ctx ⇒ Object (readonly)
Returns the value of attribute ctx.
6 7 8 |
# File 'lib/active_saga/execution.rb', line 6 def ctx @ctx end |
#cursor_step ⇒ Object (readonly)
Returns the value of attribute cursor_step.
6 7 8 |
# File 'lib/active_saga/execution.rb', line 6 def cursor_step @cursor_step end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/active_saga/execution.rb', line 6 def id @id end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
6 7 8 |
# File 'lib/active_saga/execution.rb', line 6 def state @state end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
6 7 8 |
# File 'lib/active_saga/execution.rb', line 6 def updated_at @updated_at end |
#workflow_class ⇒ Object (readonly)
Returns the value of attribute workflow_class.
6 7 8 |
# File 'lib/active_saga/execution.rb', line 6 def workflow_class @workflow_class end |
Instance Method Details
#await(timeout: nil, interval: 0.5) ⇒ Object
Blocks until execution reaches a terminal state or timeout expires. Returns the final execution snapshot.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_saga/execution.rb', line 37 def await(timeout: nil, interval: 0.5) clock = ActiveSaga.configuration.clock deadline = timeout && clock.call + timeout loop do break if terminal? raise Timeout::Error, "Execution #{id} did not finish within #{timeout}s" if deadline && clock.call > deadline sleep(interval) reload! end self end |
#cancel!(reason: nil) ⇒ Object
60 61 62 63 |
# File 'lib/active_saga/execution.rb', line 60 def cancel!(reason: nil) ActiveSaga.cancel!(id, reason: reason) reload! end |
#cancelled? ⇒ Boolean
28 29 30 |
# File 'lib/active_saga/execution.rb', line 28 def cancelled? state == "cancelled" end |
#completed? ⇒ Boolean
20 21 22 |
# File 'lib/active_saga/execution.rb', line 20 def completed? state == "completed" end |
#failed? ⇒ Boolean
24 25 26 |
# File 'lib/active_saga/execution.rb', line 24 def failed? state == "failed" end |
#reload! ⇒ Object
54 55 56 57 58 |
# File 'lib/active_saga/execution.rb', line 54 def reload! fresh = @store.load_execution(id) update_from(fresh) if fresh self end |
#terminal? ⇒ Boolean
50 51 52 |
# File 'lib/active_saga/execution.rb', line 50 def terminal? %w[completed failed cancelled timed_out].include?(state) end |