Class: Cadence::Workflow::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/cadence/workflow/context.rb

Instance Method Summary collapse

Constructor Details

#initialize(state_manager, dispatcher, metadata) ⇒ Context

Returns a new instance of Context.



18
19
20
21
22
# File 'lib/cadence/workflow/context.rb', line 18

def initialize(state_manager, dispatcher, )
  @state_manager = state_manager
  @dispatcher = dispatcher
  @metadata = 
end

Instance Method Details

#cancel(target, cancelation_id) ⇒ Object



233
234
235
236
237
238
239
240
241
242
# File 'lib/cadence/workflow/context.rb', line 233

def cancel(target, cancelation_id)
  case target.type
  when History::EventTarget::ACTIVITY_TYPE
    cancel_activity(cancelation_id)
  when History::EventTarget::TIMER_TYPE
    cancel_timer(cancelation_id)
  else
    raise "#{target} can not be canceled"
  end
end

#cancel_activity(activity_id) ⇒ Object



227
228
229
230
231
# File 'lib/cadence/workflow/context.rb', line 227

def cancel_activity(activity_id)
  decision = Decision::RequestActivityCancellation.new(activity_id: activity_id)

  schedule_decision(decision)
end

#cancel_timer(timer_id) ⇒ Object



180
181
182
183
# File 'lib/cadence/workflow/context.rb', line 180

def cancel_timer(timer_id)
  decision = Decision::CancelTimer.new(timer_id: timer_id)
  schedule_decision(decision)
end

#complete(result = nil) ⇒ Object

TODO: check if workflow can be completed



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

def complete(result = nil)
  decision = Decision::CompleteWorkflow.new(result: result)
  schedule_decision(decision)
end

#execute_activity(activity_class, *input, **args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cadence/workflow/context.rb', line 38

def execute_activity(activity_class, *input, **args)
  options = args.delete(:options) || {}
  input << args unless args.empty?

  execution_options = ExecutionOptions.new(activity_class, options)

  decision = Decision::ScheduleActivity.new(
    activity_id: options[:activity_id],
    activity_type: execution_options.name,
    input: input,
    domain: execution_options.domain,
    task_list: execution_options.task_list,
    retry_policy: execution_options.retry_policy,
    timeouts: execution_options.timeouts,
    headers: execution_options.headers
  )

  target, cancelation_id = schedule_decision(decision)
  future = Future.new(target, self, cancelation_id: cancelation_id)

  dispatcher.register_handler(target, 'completed') do |result|
    future.set(result)
    future.callbacks.each { |callback| call_in_fiber(callback, result) }
  end

  dispatcher.register_handler(target, 'failed') do |reason, details|
    future.fail(reason, details)
  end

  future
end

#execute_activity!(activity_class, *input, **args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cadence/workflow/context.rb', line 70

def execute_activity!(activity_class, *input, **args)
  future = execute_activity(activity_class, *input, **args)
  result = future.get

  if future.failed?
    reason, details = result

    error_class = safe_constantize(reason) || Cadence::ActivityException

    raise error_class, details
  end

  result
end

#execute_local_activity(activity_class, *input, **args) ⇒ Object

TODO: how to handle failures?



86
87
88
89
90
91
92
93
94
# File 'lib/cadence/workflow/context.rb', line 86

def execute_local_activity(activity_class, *input, **args)
  input << args unless args.empty?

  side_effect do
    # TODO: this probably requires a local context implementation
    context = Activity::Context.new(nil, nil)
    activity_class.execute_in_context(context, input)
  end
end

#execute_workflow(workflow_class, *input, **args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cadence/workflow/context.rb', line 96

def execute_workflow(workflow_class, *input, **args)
  options = args.delete(:options) || {}
  input << args unless args.empty?

  execution_options = ExecutionOptions.new(workflow_class, options)

  decision = Decision::StartChildWorkflow.new(
    workflow_id: options[:workflow_id] || SecureRandom.uuid,
    workflow_type: execution_options.name,
    input: input,
    domain: execution_options.domain,
    task_list: execution_options.task_list,
    retry_policy: execution_options.retry_policy,
    timeouts: execution_options.timeouts,
    headers: execution_options.headers
  )

  target, cancelation_id = schedule_decision(decision)
  future = Future.new(target, self, cancelation_id: cancelation_id)

  dispatcher.register_handler(target, 'completed') do |result|
    future.set(result)
    future.callbacks.each { |callback| call_in_fiber(callback, result) }
  end

  dispatcher.register_handler(target, 'failed') do |reason, details|
    future.fail(reason, details)
  end

  future
end

#execute_workflow!(workflow_class, *input, **args) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cadence/workflow/context.rb', line 128

def execute_workflow!(workflow_class, *input, **args)
  future = execute_workflow(workflow_class, *input, **args)
  result = future.get

  if future.failed?
    reason, details = result

    error_class = safe_constantize(reason) || StandardError.new(details)

    raise error_class, details
  end

  result
end

#fail(reason, details = nil) ⇒ Object

TODO: check if workflow can be failed



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

def fail(reason, details = nil)
  decision = Decision::FailWorkflow.new(reason: reason, details: details)
  schedule_decision(decision)
end

#has_release?(release_name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/cadence/workflow/context.rb', line 34

def has_release?(release_name)
  state_manager.release?(release_name.to_s)
end

#headersObject



30
31
32
# File 'lib/cadence/workflow/context.rb', line 30

def headers
  .headers
end

#loggerObject



24
25
26
27
28
# File 'lib/cadence/workflow/context.rb', line 24

def logger
  @logger ||= ReplayAwareLogger.new(Cadence.logger)
  @logger.replay = state_manager.replay?
  @logger
end

#nowObject



215
216
217
# File 'lib/cadence/workflow/context.rb', line 215

def now
  state_manager.local_time
end

#on_signal(&block) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/cadence/workflow/context.rb', line 219

def on_signal(&block)
  target = History::EventTarget.workflow

  dispatcher.register_handler(target, 'signaled') do |signal, input|
    call_in_fiber(block, signal, input)
  end
end

#side_effect(&block) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/cadence/workflow/context.rb', line 143

def side_effect(&block)
  marker = state_manager.next_side_effect
  return marker.last if marker

  result = block.call
  decision = Decision::RecordMarker.new(name: StateManager::SIDE_EFFECT_MARKER, details: result)
  schedule_decision(decision)

  result
end

#sleep(timeout) ⇒ Object



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

def sleep(timeout)
  start_timer(timeout).wait
end

#sleep_until(end_time) ⇒ Object



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

def sleep_until(end_time)
  delay = (end_time.to_time - now).to_i
  sleep(delay) if delay > 0
end

#start_timer(timeout, timer_id = nil) ⇒ Object



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

def start_timer(timeout, timer_id = nil)
  decision = Decision::StartTimer.new(timeout: timeout, timer_id: timer_id)
  target, cancelation_id = schedule_decision(decision)
  future = Future.new(target, self, cancelation_id: cancelation_id)

  dispatcher.register_handler(target, 'fired') do |result|
    future.set(result)
    future.callbacks.each { |callback| call_in_fiber(callback, result) }
  end

  dispatcher.register_handler(target, 'canceled') do |reason, details|
    future.fail(reason, details)
  end

  future
end

#wait_for(future) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/cadence/workflow/context.rb', line 203

def wait_for(future)
  fiber = Fiber.current

  dispatcher.register_handler(future.target, Dispatcher::WILDCARD) do
    fiber.resume if future.finished?
  end

  Fiber.yield

  return
end

#wait_for_all(*futures) ⇒ Object



197
198
199
200
201
# File 'lib/cadence/workflow/context.rb', line 197

def wait_for_all(*futures)
  futures.each(&:wait)

  return
end