File: workflow_execution.rb

package info (click to toggle)
ruby-aws-sdk 1.66.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,808 kB
  • ctags: 4,854
  • sloc: ruby: 28,354; makefile: 7
file content (391 lines) | stat: -rw-r--r-- 14,381 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

module AWS
  class SimpleWorkflow

    # @attr_reader [Symbol] child_policy The policy to use for the child
    #   workflow executions if this workflow execution is terminated.
    #   The return value will be one of the following values:
    #
    #   * `:terminate` - the child executions will be terminated.
    #   * `:request_cancel` - a request to cancel will be attempted for each
    #     child execution by recording a WorkflowExecutionCancelRequested
    #     event in its history. It is up to the decider to take appropriate
    #     actions when it receives an execution history with this event.
    #   * `:abandon` - no action will be taken. The child executions will
    #     continue to run.
    #
    # @attr_reader [String] start_to_close_timeout The total allowed
    #   duration for this workflow execution.
    #
    #   The return value will be formatted as an ISO 8601 duration (e.g.
    #   'PnYnMnDTnHnMnS').
    #
    # @attr_reader [String] task_list The task list used for the decision
    #   tasks generated for this workflow execution.
    #
    # @attr_reader [String] task_priority The task priority used for the decision
    #   tasks generated for this workflow execution.
    #
    # @attr_reader [String] task_start_to_close_timeout The maximum duration
    #   allowed for decision tasks for this workflow execution.
    #
    #   The return value will be formatted as an ISO 8601 duration (e.g.
    #   'PnYnMnDTnHnMnS').
    #
    # @attr_reader [Time,nil] closed_at The time when the workflow execution
    #   was closed.  Returns nil if this execution is not closed.
    #
    # @attr_reader [Time] started_at The time when the execution was started.
    #
    # @attr_reader [Time,nil] latest_activity_task_scheduled_at The time
    #   when the last activity task was scheduled for this workflow execution.
    #   You can use this information to determine if the workflow has not
    #   made progress for an unusually long period of time and might
    #   require a corrective action.
    #
    # @attr_reader [String,nil] latest_execution_context The latest execution
    #   context provided by the decider for this workflow execution. A decider
    #   can provide an execution context, which is a free form string, when
    #   closing a decision task.
    #
    # @attr_reader [Hash] open_counts Returns a hash of counts, including:
    #   `:open_timers`, `:open_child_workflow_executions`, `:open_decision_tasks`,
    #   and `:open_activity_tasks`.
    #
    class WorkflowExecution < Resource

      def initialize domain, workflow_id, run_id, options = {}
        @domain = domain
        @workflow_id = workflow_id
        @run_id = run_id
        super
      end

      # @return [Domain] The domain this workflow execution was started in.
      attr_reader :domain

      # @return [String] The workflow id of this execution.
      attr_reader :workflow_id

      # @return [String] The run id of this execution.
      attr_reader :run_id

      config_attribute :child_policy, :to_sym => true

      config_attribute :execution_start_to_close_timeout, :duration => true

      config_attribute :task_list do
        translates_output{|v| v['name'] }
      end

      config_attribute :task_priority do
        translates_output{|v| v.to_i }
      end

      config_attribute :task_start_to_close_timeout, :duration => true

      info_attribute :cancel_requested

      info_attribute :close_status, :to_sym => true
      protected :close_status

      info_attribute :closed_at, :from => 'closeTimestamp', :timestamp => true

      info_attribute :execution_status, :to_sym => true
      protected :execution_status

      info_attribute :parent_details, :from => 'parent', :static => true
      protected :parent_details

      info_attribute :started_at,
        :from => 'startTimestamp',
        :timestamp => true,
        :static => true

      info_attribute :tag_list, :static => true
      protected :tag_list

      info_attribute :type_details, :from => 'workflowType', :static => true
      protected :type_details

      attribute :latest_activity_task_scheduled_at,
        :from => 'latestActivityTaskTimestamp',
        :timestamp => true

      attribute :latest_execution_context

      attribute :open_counts do
        translates_output do |hash|
          hash.inject({}) do |h,(k,v)|
            h[Core::Inflection.ruby_name(k).to_sym] = v; h
          end
        end
      end

      # list_workflow_executions provides ONLY type attributes
      provider(
        :list_open_workflow_executions,
        :list_closed_workflow_executions
      ) do |provider|
        provider.provides *info_attributes.keys
        provider.find do |resp|
          execution = { 'workflowId' => workflow_id, 'runId' => run_id }
          resp.data['executionInfos'].find do |desc|
            desc['execution'] == execution
          end
        end
      end

      # describe_workflow_execution provides ALL attributes
      provider(:describe_workflow_execution) do |provider|
        provider.provides *attributes.keys
        provider.find do |resp|
          execution = { 'workflowId' => workflow_id, 'runId' => run_id }
          d = resp.data
          if d['executionInfo']['execution'] == execution
            d.merge(d['executionInfo']).merge(d['executionConfiguration'])
          else
            nil
          end
        end
      end

      # @return [Symbol] Returns the status of this execution. Possible
      #   return values are:
      #
      #   * `:open` - The execution is still running.
      #   * `:completed` - The execution was successfully completed.
      #   * `:canceled` - The execution was canceled, cancellation allows
      #      the implementation to gracefully clean up before the execution
      #      is closed.
      #   * `:failed` - The execution failed to complete.
      #     and was automatically timed out.
      #   * `:continued_as_new` - The execution is logically continued. This
      #     means the current execution was completed and a new execution
      #     was started to carry on the workflow.
      #   * `:terminated` - The execution was force terminated.
      #   * `:timed_out` - The execution did not complete in the allotted
      #     time and was automatically timed out.
      #
      def status
        AWS.memoize do
          execution_status == :open ? :open : (close_status || :closed)
        end
      end

      # @return [Boolean] Returns true if a request was made to cancel
      #   this workflow execution.
      def cancel_requested?
        cancel_requested
      end

      # @return [Boolean] Returns true if the workflow execution is still open.
      def open?
        status == :open
      end

      # @return [Boolean] Returns true if the workflow execution is closed.
      def closed?
        !open?
      end

      # @return [Boolean] Returns true if this workflow execution has an
      #   open decision task.
      def open_child_workflow_execution_count
        open_counts[:open_child_workflow_executions]
      end

      # @return [Integer] Returns the number of open activity tasks.
      def open_activity_task_count
        open_counts[:open_activity_tasks]
      end

      # @return [Integer] Returns the number of open timers.
      def open_timer_count
        open_counts[:open_timers]
      end

      # @return [Integer] Returns the number of closed activity tasks.
      def open_decision_task_count
        open_counts[:open_decision_tasks]
      end

      # @return [Array<String>] Returns an array of tags assigned to this
      #   execution.
      def tags
        tag_list || []
      end

      # @return [HistoryEventCollection] Returns a collection that enumerates
      #   history events for this workflow execution.
      def history_events
        HistoryEventCollection.new(self)
      end
      alias_method :events, :history_events

      # @return [WorkflowType] Returns the type of this workflow execution.
      def workflow_type
        type = self.type_details
        WorkflowType.new(domain, type['name'], type['version'])
      end

      # @return [WorkflowExecution,nil] Returns the parent workflow execution
      #   (if there is one).
      def parent
        if parent = self.parent_details
          domain.workflow_executions[parent['workflowId'],parent['runId']]
        else
          nil
        end
      end

      # Records a WorkflowExecutionSignaled event in the workflow execution
      # history and creates a decision task for the workflow execution.
      #
      #     workflow_execution.signal('signal_name', :input => '...')
      #
      # @param [String] signal_name The name of the signal. This name must be
      #   meaningful to the target workflow.
      #
      # @param [Hash] options
      #
      # @option options [String] :input (nil) Data to attach to the
      #   WorkflowExecutionSignaled event in the target workflow
      #   execution's history.
      #
      # @return [nil]
      #
      def signal signal_name, options = {}
        options[:run_id] = run_id
        domain.workflow_executions.signal(workflow_id, signal_name, options)
      end

      # Records a WorkflowExecutionCancelRequested event in the currently
      # running workflow execution. This logically requests the cancellation
      # of the workflow execution as a whole. It is up to the decider to
      # take appropriate actions when it receives an execution history
      # with this event.
      #
      # @note Because this action allows the workflow to properly clean up
      #   and gracefully close, it should be used instead of {#terminate}
      #   when possible.
      #
      # @return [nil]
      #
      def request_cancel
        options = { :run_id => run_id }
        domain.workflow_executions.request_cancel(workflow_id, options)
      end

      # Records a WorkflowExecutionTerminated event and forces closure of
      # the workflow execution. The child policy, registered with the
      # workflow type or specified when starting this execution, is applied
      # to any open child workflow executions of this workflow execution.
      #
      # @note If the workflow execution was in progress, it is terminated
      #   immediately.
      #
      # @note You should consider canceling the workflow execution
      #   instead because it allows the workflow to gracefully close
      #   while terminate does not.
      #
      # @param [Hash] options
      #
      # @option options [Symbol] :child_policy (nil)
      #   If set, specifies the policy to use for the child workflow
      #   executions of the workflow execution being terminated. This
      #   policy overrides the default child policy.  Valid policies include:
      #
      #   * `:terminate` - the child executions will be terminated.
      #
      #   * `:request_cancel` - a request to cancel will be attempted for each
      #     child execution by recording a WorkflowExecutionCancelRequested
      #     event in its history. It is up to the decider to take appropriate
      #     actions when it receives an execution history with this event.
      #
      #   * `:abandon` - no action will be taken. The child executions will
      #     continue to run.
      #
      # @option options [String] :details (nil) Optional details for
      #   terminating the workflow execution.
      #
      # @option options [String] :reason (nil) An optional descriptive
      #   reason for terminating the workflow execution.
      #
      # @return [nil]
      #
      def terminate options = {}
        options[:run_id] = run_id
        domain.workflow_executions.terminate(workflow_id, options)
      end

      # Counts the number of executions that share the same workflow id.
      #
      # @note See {WorkflowExecutionCollection#count} for a broader count.
      #
      # @note This operation is eventually consistent. The results are best
      #   effort and may not exactly reflect recent updates and changes.
      #
      # @param [Hash] options
      #
      # @option options [Symbol] :status (:open) Specifies that
      #   status of the workflow executions to count. Defaults to
      #   open workflows.
      #
      #   * `:open`
      #   * `:closed`
      #
      # @option options [Array<Time>] :started_between A start and end time
      #   to filter workflow execution start times by.  You may pass an
      #   array with two time values or a range.  Times should be
      #   timestamps (integers), Time, Date, DateTime or parseable time
      #   strings.
      #
      #   You may not pass both `:started_between` and `:closed_between`.
      #
      # @option options [Array<Time>] :closed_between A start and end time
      #   to filter workflow execution closed times by.  You may pass an
      #   array with two time values or a range.  Times should be
      #   timestamps (integers), Time, Date, DateTime or parseable time
      #   strings.
      #
      #   You may not pass both `:started_between` and `:closed_between`.
      #   You may also not pass `:closed_between` if the `:status` is
      #   `:open`.
      #
      # @return [Integer] Returns the count of executions that share
      #   workflow id with the current execution.
      #
      def count_executions options = {}
        options[:workflow_id] = workflow_id
        domain.workflow_executions.count(options)
      end

      protected
      def resource_identifiers
        [[:domain,domain.name], [:workflow_id,workflow_id], [:run_id,run_id]]
      end

      protected
      def resource_options
        {
          :domain => domain.name,
          :execution => { :workflow_id => workflow_id, :run_id => run_id },
        }
      end

    end
  end
end