Class: Floe::Workflow::Context

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/floe/workflow/context.rb

Instance Method Summary collapse

Methods included from Logging

included, #logger, #logger=

Constructor Details

#initialize(context = nil, input: nil, credentials: nil, logger: nil) ⇒ Context

Returns a new instance of Context.

Parameters:

  • context (Json|Hash) (defaults to: nil)

    (default, create another with input and execution params)

  • input (Json) (defaults to: nil)

    (default: '{}')



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/floe/workflow/context.rb', line 12

def initialize(context = nil, input: nil, credentials: nil, logger: nil)
  context = JSON.parse(context) if context.kind_of?(String)

  @context = context || {}
  self["Credentials"]        ||= credentials || {}
  self["Execution"]          ||= {}
  self["Execution"]["Input"] ||= JSON.parse(input || "{}")
  self["State"]              ||= {}
  self["StateHistory"]       ||= []
  self["StateMachine"]       ||= {}
  self["Task"]               ||= {}

  self.logger = logger if logger
rescue JSON::ParserError => err
  raise Floe::InvalidExecutionInput, "Invalid State Machine Execution Input: #{err}: was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')"
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



187
188
189
# File 'lib/floe/workflow/context.rb', line 187

def ==(other)
  other.kind_of?(self.class) && other.instance_variable_get(:@context) == @context
end

#[](key) ⇒ Object



150
151
152
# File 'lib/floe/workflow/context.rb', line 150

def [](key)
  @context[key]
end

#[]=(key, val) ⇒ Object



154
155
156
# File 'lib/floe/workflow/context.rb', line 154

def []=(key, val)
  @context[key] = val
end

#child_context(input) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/floe/workflow/context.rb', line 162

def child_context(input)
  require "active_support/core_ext/object/deep_dup"

  # Copy the Execution context minus any keys which are set at runtime.
  # This allows any user defined state-machine execution values be used
  # by child workflows.
  #
  # The deep_dup is important here, otherwise the Execution hash object is
  # shared between all child workflows.
  child_execution = execution
    .except("Input", "StartTime", "EndTime")
    .deep_dup
    .merge("Input" => input)

  self.class.new({"Execution" => child_execution})
end

#credentialsObject



52
53
54
# File 'lib/floe/workflow/context.rb', line 52

def credentials
  @context["Credentials"]
end

#dig(*args) ⇒ Object



158
159
160
# File 'lib/floe/workflow/context.rb', line 158

def dig(*args)
  @context.dig(*args)
end

#ended?Boolean

Returns:

  • (Boolean)


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

def ended?
  execution.key?("EndTime")
end

#executionObject



44
45
46
# File 'lib/floe/workflow/context.rb', line 44

def execution
  @context["Execution"]
end

#execution_idObject



48
49
50
# File 'lib/floe/workflow/context.rb', line 48

def execution_id
  execution["Id"]
end

#failed?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/floe/workflow/context.rb', line 64

def failed?
  (output.kind_of?(Hash) && output.key?("Error")) || false
end

#hashObject



192
193
194
# File 'lib/floe/workflow/context.rb', line 192

def hash
  @context.hash
end

#inputObject



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

def input
  state["Input"]
end

#inspectObject



179
180
181
# File 'lib/floe/workflow/context.rb', line 179

def inspect
  "#<#{self.class.name}: #{safe_context.inspect}>"
end

#json_inputObject



80
81
82
# File 'lib/floe/workflow/context.rb', line 80

def json_input
  input.to_json
end

#json_outputObject



88
89
90
# File 'lib/floe/workflow/context.rb', line 88

def json_output
  output.to_json
end

#next_stateObject



100
101
102
# File 'lib/floe/workflow/context.rb', line 100

def next_state
  state["NextState"]
end

#next_state=(val) ⇒ Object



104
105
106
# File 'lib/floe/workflow/context.rb', line 104

def next_state=(val)
  state["NextState"] = val
end

#outputObject



84
85
86
# File 'lib/floe/workflow/context.rb', line 84

def output
  state["Output"]
end

#output=(val) ⇒ Object



92
93
94
# File 'lib/floe/workflow/context.rb', line 92

def output=(val)
  state["Output"] = val
end

#prepare_start(start_at) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/floe/workflow/context.rb', line 29

def prepare_start(start_at)
  return if started?

  state["Name"]  = start_at
  state["Input"] = execution["Input"].dup
  state["Guid"]  = SecureRandom.uuid

  execution["Id"]      ||= SecureRandom.uuid
  execution["StartTime"] = Time.now.utc.iso8601

  if logger.respond_to?(:execution_id=)
    logger.execution_id = execution["Id"]
  end
end

#running?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/floe/workflow/context.rb', line 60

def running?
  started? && !ended?
end

#started?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/floe/workflow/context.rb', line 56

def started?
  execution.key?("StartTime")
end

#stateObject



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

def state
  @context["State"]
end

#state=(val) ⇒ Object



134
135
136
# File 'lib/floe/workflow/context.rb', line 134

def state=(val)
  @context["State"] = val
end

#state_finished?Boolean

State#running? also checks docker to see if it is running. You possibly want to use that instead

Returns:

  • (Boolean)


130
131
132
# File 'lib/floe/workflow/context.rb', line 130

def state_finished?
  state.key?("FinishedTime")
end

#state_historyObject



138
139
140
# File 'lib/floe/workflow/context.rb', line 138

def state_history
  @context["StateHistory"]
end

#state_machineObject



142
143
144
# File 'lib/floe/workflow/context.rb', line 142

def state_machine
  @context["StateMachine"]
end

#state_nameObject



96
97
98
# File 'lib/floe/workflow/context.rb', line 96

def state_name
  state["Name"]
end

#state_started?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/floe/workflow/context.rb', line 124

def state_started?
  state.key?("EnteredTime")
end

#statusObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/floe/workflow/context.rb', line 108

def status
  if !started?
    "pending"
  elsif running?
    "running"
  elsif failed?
    "failure"
  else
    "success"
  end
end

#success?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/floe/workflow/context.rb', line 120

def success?
  status == "success"
end

#taskObject



146
147
148
# File 'lib/floe/workflow/context.rb', line 146

def task
  @context["Task"]
end

#to_hObject



183
184
185
# File 'lib/floe/workflow/context.rb', line 183

def to_h
  safe_context
end