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



182
183
184
# File 'lib/simpler_workflow/workflow.rb', line 182

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

.register(name, version, workflow) ⇒ Object



186
187
188
# File 'lib/simpler_workflow/workflow.rb', line 186

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

Instance Method Details

#activity_completed(decision_task, event) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/simpler_workflow/workflow.rb', line 104

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/simpler_workflow/workflow.rb', line 120

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, :cancel
        decision_task.cancel_workflow_execution
      when :fail
        decision.task.fail_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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/simpler_workflow/workflow.rb', line 142

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

def decision_loop
  SimplerWorkflow.child_processes << fork do

    $0 = "Workflow: #{name} #{version}"

    Signal.trap('QUIT') do
      logger.info("Received SIGQUIT")
      @time_to_exit = true
    end

    Signal.trap('INT') do 
      logger.info("Received SIGINT")
      Process.exit!(0)
    end

    if SimplerWorkflow.after_fork
      SimplerWorkflow.after_fork.call
    end


    loop do
      begin
        logger.info("Waiting for a decision task for #{name.to_s}, #{version} listening to #{task_list}")
        domain.decision_tasks.poll_for_single_task(task_list) do |decision_task|
          decision_task.extend AWS::SimpleWorkflow::DecisionTaskAdditions
          logger.info("Received decision task")
          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
        end
        Process.exit 0 if @time_to_exit
      rescue Timeout::Error => e
        if @time_to_exit
          Process.exit 0
        else
          retry
        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
  end
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



170
171
172
# File 'lib/simpler_workflow/workflow.rb', line 170

def on_activity_completed(&block)
  @on_activity_completed = block
end

#on_activity_failed(&block) ⇒ Object



174
175
176
# File 'lib/simpler_workflow/workflow.rb', line 174

def on_activity_failed(&block)
  @on_activity_failed = block
end

#on_activity_timed_out(&block) ⇒ Object



178
179
180
# File 'lib/simpler_workflow/workflow.rb', line 178

def on_activity_timed_out(&block)
  @on_activity_timed_out = block
end

#on_start_execution(&block) ⇒ Object



166
167
168
# File 'lib/simpler_workflow/workflow.rb', line 166

def on_start_execution(&block)
  @on_start_execution = block
end

#start_execution(decision_task, event) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/simpler_workflow/workflow.rb', line 95

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



161
162
163
164
# File 'lib/simpler_workflow/workflow.rb', line 161

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

#to_workflow_typeObject



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

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