Class: ActiveSaga::Execution

Inherits:
Object
  • Object
show all
Defined in:
lib/active_saga/execution.rb

Overview

Lightweight value object representing a workflow execution.

Instance Attribute Summary collapse

Instance Method Summary collapse

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_atObject (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_atObject (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

#ctxObject (readonly)

Returns the value of attribute ctx.



6
7
8
# File 'lib/active_saga/execution.rb', line 6

def ctx
  @ctx
end

#cursor_stepObject (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

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/active_saga/execution.rb', line 6

def id
  @id
end

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'lib/active_saga/execution.rb', line 6

def state
  @state
end

#updated_atObject (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_classObject (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.

Parameters:

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

    seconds to wait, nil for indefinite

  • interval (Numeric) (defaults to: 0.5)

    polling interval



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

Returns:

  • (Boolean)


28
29
30
# File 'lib/active_saga/execution.rb', line 28

def cancelled?
  state == "cancelled"
end

#completed?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/active_saga/execution.rb', line 20

def completed?
  state == "completed"
end

#failed?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


50
51
52
# File 'lib/active_saga/execution.rb', line 50

def terminal?
  %w[completed failed cancelled timed_out].include?(state)
end