seam 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/seam/version.rb +1 -1
- data/lib/seam/worker.rb +90 -35
- data/spec/seam/worker_spec.rb +57 -1
- metadata +4 -4
data/lib/seam/version.rb
CHANGED
data/lib/seam/worker.rb
CHANGED
@@ -1,9 +1,61 @@
|
|
1
1
|
module Seam
|
2
2
|
class Worker
|
3
|
-
def handles
|
3
|
+
def handles step
|
4
4
|
@step = step
|
5
5
|
end
|
6
6
|
|
7
|
+
def execute effort
|
8
|
+
set_current_effort effort
|
9
|
+
before_process
|
10
|
+
process
|
11
|
+
after_process
|
12
|
+
end
|
13
|
+
|
14
|
+
def execute_all
|
15
|
+
efforts_to_execute.each { |e| execute e }
|
16
|
+
end
|
17
|
+
|
18
|
+
def eject
|
19
|
+
@operation_to_execute = :eject
|
20
|
+
end
|
21
|
+
|
22
|
+
def move_to_next_step
|
23
|
+
@operation_to_execute = :move_to_next_step
|
24
|
+
end
|
25
|
+
|
26
|
+
def try_again_in seconds
|
27
|
+
@operation_to_execute = :try_again_in
|
28
|
+
operation_args[:seconds] = seconds
|
29
|
+
end
|
30
|
+
|
31
|
+
attr_accessor :operation_args
|
32
|
+
def operation_args
|
33
|
+
@operation_args ||= {}
|
34
|
+
end
|
35
|
+
|
36
|
+
def operations
|
37
|
+
{
|
38
|
+
try_again_in: -> do
|
39
|
+
seconds = operation_args[:seconds]
|
40
|
+
try_again_on = Time.now + seconds
|
41
|
+
|
42
|
+
history[:try_again_on] = try_again_on
|
43
|
+
|
44
|
+
effort.next_execute_at = try_again_on
|
45
|
+
end,
|
46
|
+
move_to_next_step: -> do
|
47
|
+
effort.completed_steps << effort.next_step
|
48
|
+
|
49
|
+
steps = effort.flow['steps'].map { |x| x['name'] }
|
50
|
+
next_step = steps[effort.completed_steps.count]
|
51
|
+
|
52
|
+
effort.next_step = next_step
|
53
|
+
mark_effort_as_complete if next_step.nil?
|
54
|
+
end,
|
55
|
+
eject: -> { mark_effort_as_complete }
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
7
59
|
def effort
|
8
60
|
@current_effort
|
9
61
|
end
|
@@ -12,53 +64,56 @@ module Seam
|
|
12
64
|
@current_run
|
13
65
|
end
|
14
66
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
67
|
+
private
|
68
|
+
|
69
|
+
def mark_effort_as_complete
|
70
|
+
effort.next_step = nil
|
71
|
+
effort.complete = true
|
72
|
+
effort.completed_at = Time.now
|
73
|
+
end
|
74
|
+
|
75
|
+
def set_current_effort effort
|
21
76
|
@current_effort = effort
|
22
|
-
process
|
23
|
-
history[:data_after] = effort.data.clone
|
24
|
-
history[:stopped_at] = Time.now
|
25
|
-
effort.history << history
|
26
|
-
effort.save
|
27
77
|
end
|
28
78
|
|
29
|
-
def
|
30
|
-
|
31
|
-
|
32
|
-
|
79
|
+
def before_process
|
80
|
+
run = {
|
81
|
+
started_at: Time.now,
|
82
|
+
step: step,
|
83
|
+
data_before: effort.data.clone
|
84
|
+
}
|
85
|
+
@current_run = HashWithIndifferentAccess.new run
|
33
86
|
end
|
34
87
|
|
35
|
-
def
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
effort.next_step = nil
|
40
|
-
effort.save
|
88
|
+
def after_process
|
89
|
+
execute_the_appropriate_operation
|
90
|
+
stamp_the_new_history_record
|
91
|
+
save_the_effort
|
41
92
|
end
|
42
93
|
|
43
|
-
def
|
44
|
-
|
45
|
-
|
94
|
+
def efforts_to_execute
|
95
|
+
Seam::Effort.find_all_by_step step
|
96
|
+
end
|
46
97
|
|
47
|
-
|
98
|
+
def step
|
99
|
+
s = @step || self.class.name.underscore.gsub('_worker', '')
|
100
|
+
s.to_s
|
101
|
+
end
|
48
102
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
effort.save
|
103
|
+
def execute_the_appropriate_operation
|
104
|
+
@operation_to_execute ||= :move_to_next_step
|
105
|
+
operations[@operation_to_execute].call
|
53
106
|
end
|
54
107
|
|
55
|
-
def
|
56
|
-
|
108
|
+
def stamp_the_new_history_record
|
109
|
+
history[:result] = @operation_to_execute
|
110
|
+
history[:data_after] = effort.data.clone
|
111
|
+
history[:stopped_at] = Time.now
|
57
112
|
|
58
|
-
history
|
59
|
-
|
113
|
+
effort.history << history
|
114
|
+
end
|
60
115
|
|
61
|
-
|
116
|
+
def save_the_effort
|
62
117
|
effort.save
|
63
118
|
end
|
64
119
|
end
|
data/spec/seam/worker_spec.rb
CHANGED
@@ -34,6 +34,29 @@ describe "worker" do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
+
describe "move_to_next_step as a default" do
|
38
|
+
it "should go to move_to_next_step by default" do
|
39
|
+
flow = Seam::Flow.new
|
40
|
+
flow.apple
|
41
|
+
flow.orange
|
42
|
+
|
43
|
+
effort = flow.start( { first_name: 'John' } )
|
44
|
+
effort = Seam::Effort.find(effort.id)
|
45
|
+
|
46
|
+
effort.next_step.must_equal "apple"
|
47
|
+
|
48
|
+
apple_worker = Seam::Worker.new
|
49
|
+
apple_worker.handles(:apple)
|
50
|
+
def apple_worker.process
|
51
|
+
end
|
52
|
+
|
53
|
+
apple_worker.execute effort
|
54
|
+
|
55
|
+
effort = Seam::Effort.find(effort.id)
|
56
|
+
effort.next_step.must_equal "orange"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
37
60
|
describe "try_again_in" do
|
38
61
|
|
39
62
|
let(:effort) do
|
@@ -271,7 +294,7 @@ describe "worker" do
|
|
271
294
|
effort.data['hit 3'].must_equal 1
|
272
295
|
|
273
296
|
effort.complete?.must_equal true
|
274
|
-
|
297
|
+
effort.completed_at.must_equal Time.now
|
275
298
|
|
276
299
|
# FUTURE WAVES
|
277
300
|
send_postcard_if_necessary_worker.execute_all
|
@@ -575,10 +598,43 @@ describe "worker" do
|
|
575
598
|
fresh_effort = Seam::Effort.find(effort.id)
|
576
599
|
fresh_effort.next_step.nil?.must_equal true
|
577
600
|
end
|
601
|
+
|
602
|
+
it "should mark the completed_at date" do
|
603
|
+
fresh_effort = Seam::Effort.find(effort.id)
|
604
|
+
fresh_effort.completed_at.must_equal Time.now
|
605
|
+
end
|
578
606
|
|
579
607
|
it "should mark the history" do
|
580
608
|
effort.history[0].contrast_with!({"step"=>"apple", "result" => "eject" } )
|
581
609
|
end
|
582
610
|
|
583
611
|
end
|
612
|
+
|
613
|
+
describe "use the name of the worker to tie to a step" do
|
614
|
+
|
615
|
+
let(:effort) do
|
616
|
+
flow = Seam::Flow.new
|
617
|
+
flow.i_will_not_call_handles
|
618
|
+
|
619
|
+
e = flow.start
|
620
|
+
Seam::Effort.find(e.id)
|
621
|
+
end
|
622
|
+
|
623
|
+
before do
|
624
|
+
effort
|
625
|
+
worker = IWillNotCallHandlesWorker.new
|
626
|
+
IWillNotCallHandlesWorker.new.execute_all
|
627
|
+
end
|
628
|
+
|
629
|
+
it "should complete the effort" do
|
630
|
+
fresh_effort = Seam::Effort.find effort.id
|
631
|
+
fresh_effort.complete?.must_equal true
|
632
|
+
end
|
633
|
+
|
634
|
+
end
|
635
|
+
end
|
636
|
+
|
637
|
+
class IWillNotCallHandlesWorker < Seam::Worker
|
638
|
+
# no calling handles here
|
639
|
+
def process; end
|
584
640
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seam
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-08-
|
12
|
+
date: 2013-08-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
@@ -227,7 +227,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
227
227
|
version: '0'
|
228
228
|
segments:
|
229
229
|
- 0
|
230
|
-
hash: -
|
230
|
+
hash: -2272473425204476348
|
231
231
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
232
232
|
none: false
|
233
233
|
requirements:
|
@@ -236,7 +236,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
236
236
|
version: '0'
|
237
237
|
segments:
|
238
238
|
- 0
|
239
|
-
hash: -
|
239
|
+
hash: -2272473425204476348
|
240
240
|
requirements: []
|
241
241
|
rubyforge_project:
|
242
242
|
rubygems_version: 1.8.24
|