state_machine_job 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjBhMmYyZDQ1ZmFkMDczYzI1OGZmYjY5YmZjYmYxMGRlZTU2ZDJhZQ==
4
+ ZGViMzAyYTY5MDM0ODM1YTEyZWQ4ODI5OGMyMjE1NWUxYjcwYTQzYQ==
5
5
  data.tar.gz: !binary |-
6
- NTZmMjhhYzU5Y2Q2YzA0M2ZiM2M3MjFhZmNlYjU3YjdlMjM2YjJmNg==
6
+ NTlhMzgwNzk4OWJhZDNjMWE1YWJmOTAyOWIwNzkxOGZlZWYxMDY1OA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjAxNTY4YzQ5OGI0MmIzYjA5YzFkYWYwZDFmZmZlYjJkYjVhNzFhZjkwYjZj
10
- Yzg4NDEyZGRjOTM0MmU5OGQxOWRmNGJjN2FlYzg2YmYzNmQxNWVkZDlhMDUw
11
- YTE4ZjlhZGI5OWMwYjBhNTc4Mjg3OTI2MzM0ZGI5YzM2NWUwMWM=
9
+ NmQ4NjU2Mzc0NjZiOThjMmVkYTkwODYzYTQwZmVhZGFhNWNmNmM1ZDAxNzUz
10
+ NDJiNDVlY2VmMjY4OWVmYzUwYmE2MWMzYjA4OTk4NjEyNzc3ZjIyZTU2MjA0
11
+ M2VlNmUxNjRmNzdmM2NkZGNkOTZmYjhjMmUwMTNlZmFkZGQ5ZjY=
12
12
  data.tar.gz: !binary |-
13
- ZDc0MDZiNTVkMDI0MGJjNTgxYjRjODFkMGRjYmFjYzIxYzI2YjhmNDRjNmFj
14
- NmU1YTQwNDQ1NWQ1NzJkNmFlNzI2MDQxZDQ5YjY5ODk0MzViODkxM2E5ODZj
15
- MTc2OTU1MDQwYjc3YzEwNDcwMjQ2MjlhZjMxOWFhZjg1MmM3Nzg=
13
+ YzFjNDZhYWU4YmNkYjMzMGI3ZDE3NmY2NmQ5YjI1ZGU5Mjk5ODY4NzZmNGQ4
14
+ MWM1OWIyNzhjZDAzMDA5NjMwMzdhZWM4NGU4M2EzY2U5YjE0YzRhODg1NzAy
15
+ MDdhOWRkODI4ZDUyYjVlOGZlNTRmYWI2M2JiZjBiOTA1ZTViNDM=
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # CHANGELOG
2
+
3
+ ### Version 0.2.0
4
+
5
+ * `:if` option for job `result` to change to different states based on
6
+ conditionals.
7
+ * Raise an exception if the shorthand `result` signature is used with
8
+ further options which would be ignored.
9
+
10
+ ### Version 0.1.0
11
+
12
+ * `:retry_if_state` option to rerun jobs on completion if the state
13
+ changed since the job was scheduled.
data/README.md CHANGED
@@ -12,7 +12,7 @@ Add this line to your application's Gemfile:
12
12
 
13
13
  gem 'state_machine_job'
14
14
 
15
- Requires the [resque-logger](https://github.com/salizzar/resque-logger)
15
+ Requires the [resque-logger](https://github.com/salizzar/resque-logger)
16
16
  gem to be present and configured.
17
17
 
18
18
  ## Usage
@@ -83,6 +83,26 @@ method using the `payload` method:
83
83
  `perform_with_result` is now called with the given hash of options as
84
84
  the second parameter.
85
85
 
86
+ ### Changing to States With Conditions
87
+
88
+ One job result can lead to different states based on a conditional.
89
+ When the job finishes with the given result, the state machine
90
+ transitions to the first state whose conditional evaluates to true.
91
+
92
+ job SomeJob do
93
+ on_enter 'running'
94
+
95
+ result :ok, :state => 'special', :if => lambda { |record| record.some_condition? }
96
+ result :ok, :state => 'other', :if => :other_condition?
97
+ result :ok, :state => 'done'
98
+
99
+ result :error => 'failed'
100
+ end
101
+
102
+ A conditional can either be a lambda which optionally accepting the
103
+ record as parameter or a symbol specifying a method to call on the
104
+ record.
105
+
86
106
  ### Retrying Jobs after a Delay
87
107
 
88
108
  You can tell the state machine to retry a job based on its result:
@@ -97,7 +117,7 @@ You can tell the state machine to retry a job based on its result:
97
117
 
98
118
  When `perform_with_result` returns the result `:pending`, the state
99
119
  machine will remain in the `runnning` state and enqueue a delayed
100
- job. This functionality requires the [`resque-scheduler`](https://github.com/resque/resque-scheduler)
120
+ job. This functionality requires the [`resque-scheduler`](https://github.com/resque/resque-scheduler)
101
121
  gem.
102
122
 
103
123
  ### Retrying Jobs Based on State
@@ -122,3 +142,7 @@ can transition to a state signaling that the job will need to run
122
142
  again once it has finished. In example, passing the `:retry_if_state`
123
143
  option causes the state machine to transition back to the `running`
124
144
  state once the job finishes with result `:ok`.
145
+
146
+ ## See also
147
+
148
+ [CHANGELOG](https://github.com/codevise/state_machine_job/blob/master/CHANGELOG.md)
@@ -16,6 +16,10 @@ module StateMachineJob
16
16
 
17
17
  def result(job_result, options = {})
18
18
  if job_result.is_a?(Hash)
19
+ if job_result.size > 1
20
+ raise("Use an explicit :state option when passing additional options.\n\n result :ok, :state => :done, :if => ...\n NOT result :ok => :done, :if => ...\n\n")
21
+ end
22
+
19
23
  return result(job_result.first.first, :state => job_result.first.last)
20
24
  end
21
25
 
@@ -27,6 +31,10 @@ module StateMachineJob
27
31
  raise('The on_enter call must appear above any result using the :retry_if_state option.')
28
32
  end
29
33
 
34
+ if options[:if] && options[:retry_after]
35
+ raise('Combining the :retry_after and :if options is not supported at the moment.')
36
+ end
37
+
30
38
  on_enter_state = @on_enter_state
31
39
 
32
40
  if options[:state]
@@ -35,7 +43,7 @@ module StateMachineJob
35
43
  transition options[:retry_if_state] => on_enter_state
36
44
  end
37
45
 
38
- transition all => options[:state]
46
+ transition(all => options[:state], :if => options[:if])
39
47
  end
40
48
  elsif options[:retry_after]
41
49
  @state_machine.define_helper :instance, retry_job_method_name(job_result) do |machine, object|
@@ -1,3 +1,3 @@
1
1
  module StateMachineJob
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -145,6 +145,104 @@ module StateMachineJob
145
145
  expect(object.state).to eq('done')
146
146
  end
147
147
 
148
+ it 'result raises descriptive error when trying to use hash only signature with additional options' do
149
+ expect {
150
+ Class.new do
151
+ state_machine :initial => :idle do
152
+ extend StateMachineJob::Macro
153
+
154
+ job TestJob do
155
+ on_enter :running
156
+ result :ok => :done, :if => true
157
+ end
158
+ end
159
+ end
160
+ }.to raise_error(/Use an explicit :state option/)
161
+ end
162
+
163
+ describe ':if option' do
164
+ it 'allows skipping matching results' do
165
+ queue = double('queue')
166
+ object = Class.new do
167
+ def id
168
+ 43
169
+ end
170
+
171
+ state_machine :initial => :idle do
172
+ extend StateMachineJob::Macro
173
+
174
+ state :idle
175
+ state :running
176
+ state :done
177
+ state :other
178
+
179
+ event :run do
180
+ transition :idle => :running
181
+ end
182
+
183
+ job TestJob, queue do
184
+ on_enter :running
185
+ result :ok, :state => :done, :if => lambda { false }
186
+ result :ok, :state => :other
187
+ end
188
+ end
189
+ end.new
190
+
191
+ object.state = :running
192
+ object.state_machine_job_test_job_ok
193
+
194
+ expect(object.state).to eq('other')
195
+ end
196
+
197
+ it 'uses matching results if condition is truthy' do
198
+ queue = double('queue')
199
+ object = Class.new do
200
+ def id
201
+ 43
202
+ end
203
+
204
+ state_machine :initial => :idle do
205
+ extend StateMachineJob::Macro
206
+
207
+ state :idle
208
+ state :running
209
+ state :done
210
+ state :other
211
+
212
+ event :run do
213
+ transition :idle => :running
214
+ end
215
+
216
+ job TestJob, queue do
217
+ on_enter :running
218
+ result :ok, :state => :done, :if => lambda { true }
219
+ result :ok, :state => :other
220
+ end
221
+ end
222
+ end.new
223
+
224
+ object.state = :running
225
+ object.state_machine_job_test_job_ok
226
+
227
+ expect(object.state).to eq('done')
228
+ end
229
+
230
+ it 'raises descriptive error when used in combination with :retry_after option' do
231
+ expect {
232
+ Class.new do
233
+ state_machine :initial => :idle do
234
+ extend StateMachineJob::Macro
235
+
236
+ job TestJob do
237
+ on_enter :running
238
+ result :ok, :state => :done, :if => true, :retry_after => 100
239
+ end
240
+ end
241
+ end
242
+ }.to raise_error(/not supported/)
243
+ end
244
+ end
245
+
148
246
  describe ':retry_if_state option' do
149
247
  it 'returns to on_enter state if state matches option when job finishes' do
150
248
  queue = double('queue')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machine_job
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Codevise Solutions Ltd.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-16 00:00:00.000000000 Z
11
+ date: 2014-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -117,6 +117,7 @@ extra_rdoc_files: []
117
117
  files:
118
118
  - .gitignore
119
119
  - .travis.yml
120
+ - CHANGELOG.md
120
121
  - Gemfile
121
122
  - LICENSE.txt
122
123
  - README.md