simpler_workflow 0.3.0.beta → 0.3.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,6 @@
1
1
  Copyright (c) 2012 Snugg Home LLC
2
+ Copyright (c) 2012-2013 Frederic Jean
3
+ Copyright (c) 2012-2013 Command-Shift-N LLC
2
4
 
3
5
  MIT License
4
6
 
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # SimplerWorkflow
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/fredjean/simpler_workflow.png)](http://travis-ci.org/fredjean/simpler_workflow)
4
+ [![Code Climate](https://codeclimate.com/github/fredjean/simpler_workflow.png)](https://codeclimate.com/github/fredjean/simpler_workflow)
4
5
 
5
6
  A wrapper around Amazon's Simple Workflow Service meant to simplify declaring and using activities and workflow. Provides some sane defaults
6
7
  and work around some idiosyncracies of the platform.
@@ -176,6 +177,16 @@ You can also define similar hooks for events using the following methods:
176
177
  * ```on_activity_completed``` is called when an activity completes and SWF reports back to the decision loop.
177
178
  * ```on_activity_failed``` is called when an activity reports a failure to SWF.
178
179
 
180
+ ### Triggering Workflows
181
+
182
+ You can trigger the worklow with the following command:
183
+
184
+ ```ruby
185
+ SimplerWorkflow::Domain["my-test-domain"].start_workflow("hello-world", "1.0.1", "AWS")
186
+ ```
187
+
188
+ The parameters are the name of the workflow, the version as well as a string representing the input to the workflow.
189
+
179
190
  ## Workflow and Activity Versioning
180
191
 
181
192
  All workflow types and activity types are versioned under SWF. This allows an organization to release new versions of a workflow or activity
@@ -2,22 +2,23 @@ module SimplerWorkflow
2
2
  class Activity
3
3
  include OptionsAsMethods
4
4
 
5
- DEFAULT_OPTIONS = {
6
- :default_task_list => name,
7
- :default_task_start_to_close_timeout => 5 * 60,
8
- :default_task_schedule_to_start_timeout => 5 * 60,
9
- :default_task_schedule_to_close_timeout => 10 * 60,
10
- :default_task_heartbeat_timeout => :none
11
- }
12
-
13
- attr_reader :domain, :name, :version, :options, :next_activity
14
-
15
- def initialize(domain, name, version)
16
- @options = DEFAULT_OPTIONS.dup
5
+ attr_reader :domain, :name, :version, :options, :next_activity, :task_list
6
+
7
+ def initialize(domain, name, version, options = {})
8
+ default_options =
9
+ {
10
+ :default_task_list => name,
11
+ :default_task_start_to_close_timeout => 5 * 60,
12
+ :default_task_schedule_to_start_timeout => 5 * 60,
13
+ :default_task_schedule_to_close_timeout => 10 * 60,
14
+ :default_task_heartbeat_timeout => :none
15
+ }
16
+ @options = default_options.merge(options)
17
17
  @domain = domain
18
18
  @name = name
19
19
  @version = version
20
20
  @failure_policy = :fail
21
+ @task_list = name.to_s
21
22
  end
22
23
 
23
24
  def on_success(activity, version = nil)
@@ -100,7 +101,7 @@ module SimplerWorkflow
100
101
  @time_to_exit = true
101
102
  end
102
103
 
103
- Signal.trap('INT') do
104
+ Signal.trap('INT') do
104
105
  logger.info("Received SIGINT")
105
106
  Process.exit!(0)
106
107
  end
@@ -113,17 +114,12 @@ module SimplerWorkflow
113
114
  loop do
114
115
  begin
115
116
  logger.info("Starting activity_loop for #{name}")
116
- domain.activity_tasks.poll(name.to_s) do |task|
117
+ domain.activity_tasks.poll(task_list) do |task|
117
118
  begin
118
119
  logger.info("Received task...")
119
120
  perform_task(task)
120
121
  unless task.responded?
121
- if next_activity
122
- result = {:next_activity => next_activity}.to_json
123
- task.complete!(:result => result)
124
- else
125
- task.complete!
126
- end
122
+ task.complete!
127
123
  end
128
124
  rescue => e
129
125
  context = {}
@@ -1,3 +1,3 @@
1
1
  module SimplerWorkflow
2
- VERSION = "0.3.0.beta"
2
+ VERSION = "0.3.0.beta2"
3
3
  end
@@ -35,7 +35,7 @@ module SimplerWorkflow
35
35
  @time_to_exit = true
36
36
  end
37
37
 
38
- Signal.trap('INT') do
38
+ Signal.trap('INT') do
39
39
  logger.info("Received SIGINT")
40
40
  Process.exit!(0)
41
41
  end
@@ -82,19 +82,19 @@ module SimplerWorkflow
82
82
  end
83
83
 
84
84
  def on_start_execution(&block)
85
- event_handlers['WorkflowExecutionStarted'] = WorkflowEventHandler.new(&block)
85
+ event_handlers['WorkflowExecutionStarted'] = block
86
86
  end
87
87
 
88
88
  def on_activity_completed(&block)
89
- event_handlers['ActivityTaskCompleted'] = WorkflowEventHandler.new(&block)
89
+ event_handlers['ActivityTaskCompleted'] = block
90
90
  end
91
91
 
92
92
  def on_activity_failed(&block)
93
- event_handlers['ActivityTaskFailed'] = WorkflowEventHandler.new(&block)
93
+ event_handlers['ActivityTaskFailed'] = block
94
94
  end
95
95
 
96
96
  def on_activity_timed_out(&block)
97
- event_handlers['ActivityTaskTimedOut'] = WorkflowEventHandler.new(&block)
97
+ event_handlers['ActivityTaskTimedOut'] = block
98
98
  end
99
99
 
100
100
  def self.[](name, version)
@@ -135,15 +135,15 @@ module SimplerWorkflow
135
135
  logger.info("Received decision task")
136
136
  decision_task.new_events.each do |event|
137
137
  logger.info("Processing #{event.event_type}")
138
- event_handlers.fetch(event.event_type, DefaultEventHandler.new(self)).process(decision_task, event)
138
+ event_handlers.fetch(event.event_type, DefaultEventHandler.new(self)).call(decision_task, event)
139
139
  end
140
140
  end
141
141
 
142
142
  def event_handlers
143
143
  @event_handlers ||= Map[
144
- :WorkflowExecutionStarted , WorkflowExecutionStartedHandler.new(self) ,
145
- :ActivityTaskCompleted , ActivityTaskCompletedHandler.new(self) ,
146
- :ActivityTaskFailed , ActivityTaskFailedHandler.new(self) ,
144
+ :WorkflowExecutionStarted , WorkflowExecutionStartedHandler.new(self) ,
145
+ :ActivityTaskCompleted , ActivityTaskCompletedHandler.new(self) ,
146
+ :ActivityTaskFailed , ActivityTaskFailedHandler.new(self) ,
147
147
  :ActivityTaskTimedOut , ActivityTaskTimedOutHandler.new(self)
148
148
  ]
149
149
  end
@@ -175,23 +175,11 @@ module SimplerWorkflow
175
175
  workflow.initial_activity_type
176
176
  end
177
177
 
178
- def process(*args); end
179
- end
180
-
181
- class WorkflowEventHandler
182
- attr_accessor :handler
183
-
184
- def initialize(&block)
185
- @handler = block
186
- end
187
-
188
- def process(decision_task, event)
189
- handler.call(decision_task, event)
190
- end
178
+ def call(*args); end
191
179
  end
192
180
 
193
181
  class ActivityTaskTimedOutHandler < DefaultEventHandler
194
- def process(decision_task, event)
182
+ def call(decision_task, event)
195
183
  case event.attributes.timeoutType
196
184
  when 'START_TO_CLOSE', 'SCHEDULE_TO_START', 'SCHEDULE_TO_CLOSE'
197
185
  last_activity_type = last_activity(decision_task, event)
@@ -204,10 +192,10 @@ module SimplerWorkflow
204
192
  end
205
193
 
206
194
  class ActivityTaskFailedHandler < DefaultEventHandler
207
- def process(decision_task, event)
195
+ def call(decision_task, event)
208
196
  last_activity_type = last_activity(decision_task, event)
209
197
  failed_activity = domain.activities[last_activity_type]
210
-
198
+
211
199
  case failed_activity.failure_policy
212
200
  when :abort, :cancel
213
201
  SimplerWorkflow.logger.info("Cancelling workflow execution.")
@@ -223,7 +211,7 @@ module SimplerWorkflow
223
211
  end
224
212
 
225
213
  class ActivityTaskCompletedHandler < DefaultEventHandler
226
- def process(decision_task, event)
214
+ def call(decision_task, event)
227
215
  last_activity_type = last_activity(decision_task, event)
228
216
 
229
217
  completed_activity = domain.activities[last_activity_type]
@@ -238,7 +226,7 @@ module SimplerWorkflow
238
226
  end
239
227
 
240
228
  class WorkflowExecutionStartedHandler < DefaultEventHandler
241
- def process(decision_task, event)
229
+ def call(decision_task, event)
242
230
  decision_task.schedule_activity_task initial_activity_type, input: event.attributes.input
243
231
  end
244
232
  end
@@ -44,6 +44,7 @@ module SimplerWorkflow
44
44
  its(:version) { should == '1.0.0' }
45
45
  its(:domain) { should == domain }
46
46
  its(:failure_policy) { should == :fail }
47
+ its(:task_list) { should == 'test-activity' }
47
48
  end
48
49
 
49
50
  context "Setting the failure policy" do
@@ -35,7 +35,7 @@ module SimplerWorkflow
35
35
  end
36
36
 
37
37
  context "Registering a new workflow." do
38
- before :each do
38
+ before :each do
39
39
  Workflow.send :public, :event_handlers
40
40
  end
41
41
 
@@ -82,7 +82,7 @@ module SimplerWorkflow
82
82
 
83
83
  decision_task.should_receive(:new_events).and_return([new_event])
84
84
 
85
- event_handlers[new_event.event_type].should_receive(:process).with(decision_task, new_event)
85
+ event_handlers[new_event.event_type].should_receive(:call).with(decision_task, new_event)
86
86
 
87
87
  workflow.send :handle_decision_task, decision_task
88
88
  end
@@ -102,7 +102,7 @@ module SimplerWorkflow
102
102
  event = stub( :attributes => stub( :input => "Mary had a little lamb"))
103
103
  decision_task.should_receive(:schedule_activity_task).with(domain.activity_types[:test_activity, '1.0.0'], input: event.attributes.input)
104
104
 
105
- event_handlers[:WorkflowExecutionStarted].process(decision_task, event)
105
+ event_handlers[:WorkflowExecutionStarted].call(decision_task, event)
106
106
  end
107
107
  end
108
108
 
@@ -120,7 +120,7 @@ module SimplerWorkflow
120
120
  decision_task.should_receive(:scheduled_event).with(event).and_return(scheduled_event)
121
121
  decision_task.should_receive(:complete_workflow_execution).with(result: 'success')
122
122
 
123
- event_handlers[:ActivityTaskCompleted].process(decision_task, event)
123
+ event_handlers[:ActivityTaskCompleted].call(decision_task, event)
124
124
  end
125
125
 
126
126
  it "should schedule the next activity if the current one declares one" do
@@ -129,7 +129,7 @@ module SimplerWorkflow
129
129
 
130
130
  test_activity = domain.register_activity(:test_activity, '1.0.0')
131
131
 
132
- scheduled_activity = domain.register_activity(:success_activity, '1.0.0') do
132
+ scheduled_activity = domain.register_activity(:success_activity, '1.0.0') do
133
133
  on_success :test_activity, '1.0.0'
134
134
  end
135
135
 
@@ -142,7 +142,7 @@ module SimplerWorkflow
142
142
  decision_task.should_receive(:scheduled_event).with(event).twice.and_return(scheduled_event)
143
143
  decision_task.should_receive(:schedule_activity).with(next_activity, input: scheduled_event.attributes.input)
144
144
 
145
- event_handlers[:ActivityTaskCompleted].process(decision_task, event)
145
+ event_handlers[:ActivityTaskCompleted].call(decision_task, event)
146
146
  end
147
147
  end
148
148
 
@@ -159,7 +159,7 @@ module SimplerWorkflow
159
159
  decision_task.should_receive(:fail_workflow_execution)
160
160
  decision_task.should_receive(:scheduled_event).with(event).and_return(scheduled_event)
161
161
 
162
- event_handlers[:ActivityTaskFailed].process(decision_task, event)
162
+ event_handlers[:ActivityTaskFailed].call(decision_task, event)
163
163
  end
164
164
 
165
165
  it "should cancel the execution if instructed to abort" do
@@ -175,7 +175,7 @@ module SimplerWorkflow
175
175
  decision_task.should_receive(:scheduled_event).with(event).and_return(scheduled_event)
176
176
  decision_task.should_receive(:cancel_workflow_execution)
177
177
 
178
- event_handlers[:ActivityTaskFailed].process(decision_task, event)
178
+ event_handlers[:ActivityTaskFailed].call(decision_task, event)
179
179
  end
180
180
 
181
181
  it "should cancel the execution if instructed to do so" do
@@ -192,7 +192,7 @@ module SimplerWorkflow
192
192
  decision_task.should_receive(:scheduled_event).with(event).and_return(scheduled_event)
193
193
  decision_task.should_receive(:cancel_workflow_execution)
194
194
 
195
- event_handlers[:ActivityTaskFailed].process(decision_task, event)
195
+ event_handlers[:ActivityTaskFailed].call(decision_task, event)
196
196
  end
197
197
 
198
198
  it "should reschedule the activity if requested" do
@@ -209,9 +209,8 @@ module SimplerWorkflow
209
209
  decision_task.should_receive(:scheduled_event).with(event).twice.and_return(scheduled_event)
210
210
  decision_task.should_receive(:schedule_activity_task).with(test_activity.to_activity_type, input: scheduled_event.attributes.input)
211
211
 
212
- event_handlers[:ActivityTaskFailed].process(decision_task, event)
212
+ event_handlers[:ActivityTaskFailed].call(decision_task, event)
213
213
  end
214
-
215
214
  end
216
215
 
217
216
  context "an activity timed out" do
@@ -226,7 +225,7 @@ module SimplerWorkflow
226
225
 
227
226
  decision_task.should_receive(:scheduled_event).twice.and_return(scheduled_event)
228
227
  decision_task.should_receive(:schedule_activity_task).with(activity_type, input: scheduled_event.attributes.input)
229
- event_handlers[:ActivityTaskTimedOut].process(decision_task, event)
228
+ event_handlers[:ActivityTaskTimedOut].call(decision_task, event)
230
229
  end
231
230
  end
232
231
 
@@ -236,7 +235,7 @@ module SimplerWorkflow
236
235
 
237
236
  decision_task.should_receive(:fail_workflow_execution)
238
237
 
239
- event_handlers[:ActivityTaskTimedOut].process(decision_task, event)
238
+ event_handlers[:ActivityTaskTimedOut].call(decision_task, event)
240
239
  end
241
240
  end
242
241
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: simpler_workflow
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: 6
5
- version: 0.3.0.beta
5
+ version: 0.3.0.beta2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Frederic Jean
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-26 00:00:00.000000000 Z
12
+ date: 2013-04-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  version_requirements: !ruby/object:Gem::Requirement
@@ -192,7 +192,7 @@ files:
192
192
  - spec/workflow_spec.rb
193
193
  homepage: https://github.com/fredjean/simpler_workflow
194
194
  licenses: []
195
- post_install_message: ! "simpler_workflow 0.3.0.beta\n========================\n\nHave
195
+ post_install_message: ! "simpler_workflow 0.3.0.beta2\n========================\n\nHave
196
196
  a look at https://github.com/fredjean/simpler_workflow/wiki/MIgrating-to-0.2.0 if
197
197
  you\nare upgrading from a 0.1.x version of the gem. There is a fundamental change
198
198
  in how the \nactivity and decision loops are run. You may need to adjust your application