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 (Hash) (defaults to: nil)

    (default: {})



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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)
  input   = JSON.parse(input || "{}")

  @context = context || {}
  self["Credentials"]        ||= credentials || {}
  self["Execution"]          ||= {}
  self["Execution"]["Input"] ||= 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?



171
172
173
# File 'lib/floe/workflow/context.rb', line 171

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

#[](key) ⇒ Object



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

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

#[]=(key, val) ⇒ Object



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

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

#credentialsObject



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

def credentials
  @context["Credentials"]
end

#dig(*args) ⇒ Object



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

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

#ended?Boolean

Returns:

  • (Boolean)


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

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

#executionObject



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

def execution
  @context["Execution"]
end

#execution_idObject



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

def execution_id
  execution["Id"]
end

#failed?Boolean

Returns:

  • (Boolean)


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

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

#hashObject



176
177
178
# File 'lib/floe/workflow/context.rb', line 176

def hash
  @context.hash
end

#inputObject



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

def input
  state["Input"]
end

#inspectObject



163
164
165
# File 'lib/floe/workflow/context.rb', line 163

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

#json_inputObject



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

def json_input
  input.to_json
end

#json_outputObject



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

def json_output
  output.to_json
end

#next_stateObject



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

def next_state
  state["NextState"]
end

#next_state=(val) ⇒ Object



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

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

#outputObject



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

def output
  state["Output"]
end

#output=(val) ⇒ Object



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

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

#prepare_start(start_at) ⇒ Object



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

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)


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

def running?
  started? && !ended?
end

#started?Boolean

Returns:

  • (Boolean)


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

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

#stateObject



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

def state
  @context["State"]
end

#state=(val) ⇒ Object



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

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)


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

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

#state_historyObject



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

def state_history
  @context["StateHistory"]
end

#state_machineObject



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

def state_machine
  @context["StateMachine"]
end

#state_nameObject



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

def state_name
  state["Name"]
end

#state_started?Boolean

Returns:

  • (Boolean)


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

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

#statusObject



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

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

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  status == "success"
end

#taskObject



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

def task
  @context["Task"]
end

#to_hObject



167
168
169
# File 'lib/floe/workflow/context.rb', line 167

def to_h
  safe_context
end