Class: ActiveWorkflow::Execution

Inherits:
Object
  • Object
show all
Defined in:
lib/active_workflow/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:, store: ActiveWorkflow.store) ⇒ Execution

Returns a new instance of Execution.



8
9
10
11
12
13
14
15
16
17
# File 'lib/active_workflow/execution.rb', line 8

def initialize(id:, workflow_class:, state:, ctx:, cursor_step:, created_at:, updated_at:, store: ActiveWorkflow.store)
  @id             = id
  @workflow_class = workflow_class
  @state          = state
  @ctx            = ActiveWorkflow::Context.new(ctx)
  @cursor_step    = cursor_step&.to_sym
  @created_at     = created_at
  @updated_at     = updated_at
  @store          = store
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#ctxObject (readonly)

Returns the value of attribute ctx.



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

def ctx
  @ctx
end

#cursor_stepObject (readonly)

Returns the value of attribute cursor_step.



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

def cursor_step
  @cursor_step
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



6
7
8
# File 'lib/active_workflow/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_workflow/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



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_workflow/execution.rb', line 32

def await(timeout: nil, interval: 0.5)
  clock = ActiveWorkflow.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

#completed?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/active_workflow/execution.rb', line 19

def completed?
  state == "completed"
end

#failed?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/active_workflow/execution.rb', line 23

def failed?
  state == "failed"
end

#reload!Object



49
50
51
52
53
# File 'lib/active_workflow/execution.rb', line 49

def reload!
  fresh = @store.load_execution(id)
  update_from(fresh) if fresh
  self
end

#terminal?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/active_workflow/execution.rb', line 45

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