Class: SimplerWorkflow::Workflow

Inherits:
Object
  • Object
show all
Includes:
OptionsAsMethods
Defined in:
lib/simpler_workflow/workflow.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OptionsAsMethods

#method_missing

Constructor Details

#initialize(domain, name, version, options = {}) ⇒ Workflow

Returns a new instance of Workflow.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/simpler_workflow/workflow.rb', line 7

def initialize(domain, name, version, options = {})
  Workflow.workflows[[name, version]] ||= begin
    default_options = {
      :default_task_list => name,
      :default_task_start_to_close_timeout => 2 * 60,
      :default_execution_start_to_close_timeout => 2 * 60,
      :default_child_policy => :terminate
    }
    @options = default_options.merge(options)
    @domain = domain
    @name = name
    @version = version
    self
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SimplerWorkflow::OptionsAsMethods

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



5
6
7
# File 'lib/simpler_workflow/workflow.rb', line 5

def domain
  @domain
end

#initial_activity_typeObject (readonly)

Returns the value of attribute initial_activity_type.



5
6
7
# File 'lib/simpler_workflow/workflow.rb', line 5

def initial_activity_type
  @initial_activity_type
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/simpler_workflow/workflow.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/simpler_workflow/workflow.rb', line 5

def options
  @options
end

#task_listObject (readonly)

Returns the value of attribute task_list.



5
6
7
# File 'lib/simpler_workflow/workflow.rb', line 5

def task_list
  @task_list
end

#versionObject (readonly)

Returns the value of attribute version.



5
6
7
# File 'lib/simpler_workflow/workflow.rb', line 5

def version
  @version
end

Class Method Details

.[](name, version) ⇒ Object



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

def self.[](name, version)
  workflows[[name, version]]
end

.register(name, version, workflow) ⇒ Object



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

def self.register(name, version, workflow)
  workflows[[name, version]] = workflow
end

Instance Method Details

#activity_completed(decision_task, event) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/simpler_workflow/workflow.rb', line 78

def activity_completed(decision_task, event)
  if @on_activity_completed && @on_activity_completed.respond_to?(:call)
    @on_activity_completed.call(decision_task, event)
  else
    if event.attributes.keys.include?(:result)
      result = Map.from_json(event.attributes.result)
      next_activity = result[:next_activity]
      activity_type = domain.activity_types[next_activity[:name], next_activity[:version]]
      decision_task.schedule_activity_task activity_type, :input => scheduled_event(decision_task, event).attributes.input
    else
      logger.info("Workflow #{name}, #{version} completed")
      decision_task.complete_workflow_execution :result => 'success'
    end
  end
end

#activity_failed(decision_task, event) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/simpler_workflow/workflow.rb', line 94

def activity_failed(decision_task, event)
  logger.info("Activity failed.")
  if @on_activity_failed && @on_activity_failed.respond_to?(:call)
    @on_activity_failed.call(decision_task, event)
  else
    if event.attributes.keys.include?(:details)
      details = Map.from_json(event.attributes.details)
      case details.failure_policy.to_sym
      when :abort
        decision_task.cancel_workflow_execution
      when :retry
        logger.info("Retrying activity #{last_activity(decision_task, event).name} #{last_activity(decision_task, event).version}")
        decision_task.schedule_activity_task last_activity(decision_task, event), :input => last_input(decision_task, event)
      end
    else
      decision_task.cancel_workflow_execution
    end
  end
end

#activity_timed_out(decision_task, event) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/simpler_workflow/workflow.rb', line 114

def activity_timed_out(decision_task, event)
  logger.info("Activity timed out.")
  if @on_activity_timed_out && @on_activity_timed_out.respond_to?(:call)
    @on_activity_timed_out.call(decision_task, event)
  else
    case event.attributes.timeoutType
    when 'START_TO_CLOSE', 'SCHEDULE_TO_START', 'SCHEDULE_TO_CLOSE'
      logger.info("Retrying activity #{last_activity(decision_task, event).name} #{last_activity(decision_task, event).version} due to timeout.")
      decision_task.schedule_activity_task last_activity(decision_task, event), :input => last_input(decision_task, event)
    when 'HEARTBEAT'
      decision_task.cancel_workflow_execution
    end
  end
end

#decision_loopObject



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
# File 'lib/simpler_workflow/workflow.rb', line 31

def decision_loop
  logger.info("Starting decision loop for #{name.to_s}, #{version} listening to #{task_list}")
  domain.decision_tasks.poll(task_list) do |decision_task|
    begin
      start_time = DateTime.now
      logger.info("Received decision task #{decision_task.id} at #{start_time}")
      decision_task.extend AWS::SimpleWorkflow::DecisionTaskAdditions
      decision_task.new_events.each do |event|
        logger.info("Processing #{event.event_type}")
        case event.event_type
        when 'WorkflowExecutionStarted'
          start_execution(decision_task, event)
        when 'ActivityTaskCompleted'
          activity_completed(decision_task, event)
        when 'ActivityTaskFailed'
          activity_failed(decision_task, event)
        when 'ActivityTaskTimedOut'
          activity_timed_out(decision_task, event)
        end
      end
    rescue => e
      context = {
        :workflow_execution => decision_task.workflow_execution,
        :workflow => to_workflow_type,
        :decision_task => decision_task
      }
      SimplerWorkflow.exception_reporter.report(e, context)
      raise e
    end
  end
rescue Timeout::Error => e
  retry
end

#initial_activity(name, version = nil) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/simpler_workflow/workflow.rb', line 23

def initial_activity(name, version = nil)
  if activity = Activity[name.to_sym, version]
    @initial_activity_type = activity.to_activity_type
  elsif activity = domain.activity_types[name.to_s, version]
    @initial_activity_type = activity
  end
end

#on_activity_completed(&block) ⇒ Object



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

def on_activity_completed(&block)
  @on_activity_completed = block
end

#on_activity_failed(&block) ⇒ Object



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

def on_activity_failed(&block)
  @on_activity_failed = block
end

#on_activity_timed_out(&block) ⇒ Object



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

def on_activity_timed_out(&block)
  @on_activity_timed_out = block
end

#on_start_execution(&block) ⇒ Object



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

def on_start_execution(&block)
  @on_start_execution = block
end

#start_execution(decision_task, event) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/simpler_workflow/workflow.rb', line 69

def start_execution(decision_task, event)
  logger.info "Starting the execution of the job."
  if @on_start_execution && @on_start_execution.respond_to?(:call)
    @on_start_execution.call(decision_task, event)
  else
    decision_task.schedule_activity_task initial_activity_type, :input => event.attributes.input
  end
end

#start_workflow(input, options = {}) ⇒ Object



133
134
135
136
# File 'lib/simpler_workflow/workflow.rb', line 133

def start_workflow(input, options = {})
  options[:input] = input
  domain.workflow_types[name.to_s, version].start_execution(options)
end

#to_workflow_typeObject



129
130
131
# File 'lib/simpler_workflow/workflow.rb', line 129

def to_workflow_type
  { :name => name, :version => version }
end