Class: SimplerWorkflow::Workflow

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

Defined Under Namespace

Classes: ActivityTaskCompletedHandler, ActivityTaskFailedHandler, ActivityTaskTimedOutHandler, DefaultEventHandler, WorkflowExecutionStartedHandler

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



109
110
111
# File 'lib/simpler_workflow/workflow.rb', line 109

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

.register(name, version, workflow) ⇒ Object



113
114
115
# File 'lib/simpler_workflow/workflow.rb', line 113

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

Instance Method Details

#decision_loopObject



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

def decision_loop
  SimplerWorkflow.child_processes << fork do

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

    Signal.trap('QUIT') do
      # Don't log in trap, ruby 2 complains
      # since we need to exit quickly, only delay quit
      # if we are in the middle of a task
      if @in_task
        @time_to_exit = true 
      else
        Process.exit 0
      end
    end

    Signal.trap('INT') do
      # Don't log in trap, ruby 2 complains
      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|
          @in_task = true # lock for TERM signal handling
          handle_decision_task(decision_task)
        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 => to_workflow_type
        }
        SimplerWorkflow.exception_reporter.report(e, context)
        raise e
      ensure
        @in_task = false
      end
    end
  end
end

#initial_activity(name, version = nil) ⇒ Object



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

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

#last_activity(decision_task, event) ⇒ Object



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

def last_activity(decision_task, event)
  scheduled_event(decision_task, event).attributes.activity_type
end

#last_input(decision_task, event) ⇒ Object



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

def last_input(decision_task, event)
  scheduled_event(decision_task, event).attributes.input
end

#on_activity_completed(&block) ⇒ Object



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

def on_activity_completed(&block)
  event_handlers['ActivityTaskCompleted'] = block
end

#on_activity_failed(&block) ⇒ Object



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

def on_activity_failed(&block)
  event_handlers['ActivityTaskFailed'] = block
end

#on_activity_timed_out(&block) ⇒ Object



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

def on_activity_timed_out(&block)
  event_handlers['ActivityTaskTimedOut'] = block
end

#on_start_execution(&block) ⇒ Object



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

def on_start_execution(&block)
  event_handlers['WorkflowExecutionStarted'] = block
end

#scheduled_event(decision_task, event) ⇒ Object



117
118
119
# File 'lib/simpler_workflow/workflow.rb', line 117

def scheduled_event(decision_task, event)
  decision_task.scheduled_event(event)
end

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



88
89
90
91
# File 'lib/simpler_workflow/workflow.rb', line 88

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

#to_workflow_typeObject



84
85
86
# File 'lib/simpler_workflow/workflow.rb', line 84

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