Class: Floe::WorkflowBase

Inherits:
Object
  • Object
show all
Includes:
ValidationMixin
Defined in:
lib/floe/workflow_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValidationMixin

included, #invalid_field_error!, #missing_field_error!, #parser_error!, #runtime_field_error!, #workflow_state?, #wrap_parser_error

Constructor Details

#initialize(payload, name = nil) ⇒ WorkflowBase

Returns a new instance of WorkflowBase.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/floe/workflow_base.rb', line 9

def initialize(payload, name = nil)
  # NOTE: this is a string, and states use an array
  @name     = name || "State Machine"
  @payload  = payload
  @start_at = payload["StartAt"]

  # NOTE: Everywhere else we include our name (i.e.: parent name) when building the child name.
  #       When creating the states, we are dropping our name (i.e.: the workflow name)
  @states         = payload["States"].to_a.map { |state_name, state| Floe::Workflow::State.build!(self, ["States", state_name], state) }
  @states_by_name = @states.to_h { |state| [state.short_name, state] }

  validate_workflow!
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/floe/workflow_base.rb', line 7

def name
  @name
end

#payloadObject (readonly)

Returns the value of attribute payload.



7
8
9
# File 'lib/floe/workflow_base.rb', line 7

def payload
  @payload
end

#start_atObject (readonly)

Returns the value of attribute start_at.



7
8
9
# File 'lib/floe/workflow_base.rb', line 7

def start_at
  @start_at
end

#statesObject (readonly)

Returns the value of attribute states.



7
8
9
# File 'lib/floe/workflow_base.rb', line 7

def states
  @states
end

#states_by_nameObject (readonly)

Returns the value of attribute states_by_name.



7
8
9
# File 'lib/floe/workflow_base.rb', line 7

def states_by_name
  @states_by_name
end

Instance Method Details

#current_state(context) ⇒ Object



68
69
70
# File 'lib/floe/workflow_base.rb', line 68

def current_state(context)
  states_by_name[context.state_name]
end

#end?(context) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/floe/workflow_base.rb', line 72

def end?(context)
  context.ended?
end

#output(context) ⇒ Object



76
77
78
# File 'lib/floe/workflow_base.rb', line 76

def output(context)
  context.output.to_json if end?(context)
end

#run(context) ⇒ Object



23
24
25
# File 'lib/floe/workflow_base.rb', line 23

def run(context)
  run_nonblock(context) until context.ended?
end

#run_nonblock(context) ⇒ Object



27
28
29
30
31
# File 'lib/floe/workflow_base.rb', line 27

def run_nonblock(context)
  start_workflow(context)
  loop while step_nonblock(context) == 0 && !context.ended?
  self
end

#start_workflow(context) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/floe/workflow_base.rb', line 57

def start_workflow(context)
  return if context.state_name

  context.state["Name"]  = start_at
  context.state["Input"] = context.execution["Input"].dup

  context.execution["StartTime"] = Time.now.utc.iso8601

  self
end

#step_nonblock(context) ⇒ Object



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

def step_nonblock(context)
  return Errno::EPERM if context.ended?

  result = current_state(context).run_nonblock!(context)
  return result if result != 0

  context.state_history << context.state
  context.next_state ? step!(context) : end_workflow!(context)

  result
end

#step_nonblock_ready?(context) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/floe/workflow_base.rb', line 45

def step_nonblock_ready?(context)
  !context.started? || current_state(context).ready?(context)
end

#wait_until(context) ⇒ Object



53
54
55
# File 'lib/floe/workflow_base.rb', line 53

def wait_until(context)
  current_state(context)&.wait_until(context)
end

#waiting?(context) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/floe/workflow_base.rb', line 49

def waiting?(context)
  current_state(context)&.waiting?(context)
end