cascade 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cascade/job/callbacks.rb +13 -9
- data/lib/cascade/worker.rb +41 -5
- metadata +1 -1
@@ -2,14 +2,11 @@ module Cascade
|
|
2
2
|
module Job
|
3
3
|
module Callbacks
|
4
4
|
def run_callbacks(action, job_spec)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
else
|
11
|
-
instance_exec job_spec, &callback
|
12
|
-
end
|
5
|
+
self.class.callbacks[action].each do |callback|
|
6
|
+
if callback.is_a?(Symbol)
|
7
|
+
send(callback, job_spec)
|
8
|
+
else
|
9
|
+
instance_exec job_spec, &callback
|
13
10
|
end
|
14
11
|
end
|
15
12
|
end
|
@@ -21,8 +18,15 @@ module Cascade
|
|
21
18
|
end
|
22
19
|
end
|
23
20
|
|
21
|
+
def callbacks
|
22
|
+
@callbacks ||= if superclass.respond_to?(:callbacks)
|
23
|
+
superclass.send(:callbacks)
|
24
|
+
else
|
25
|
+
Hash.new { |h,k| h[k] = [] }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
24
29
|
def add_callback(action, method = nil, &block)
|
25
|
-
callbacks = @callbacks ||= Hash.new { |h,k| h[k] = [] }
|
26
30
|
callbacks[action] << (method || block)
|
27
31
|
end
|
28
32
|
end
|
data/lib/cascade/worker.rb
CHANGED
@@ -5,42 +5,78 @@ module Cascade
|
|
5
5
|
trap('INT') { $exit = true }
|
6
6
|
|
7
7
|
loop do
|
8
|
+
result = run
|
9
|
+
|
10
|
+
break if $exit
|
11
|
+
|
12
|
+
count = result.sum
|
13
|
+
if count.zero?
|
14
|
+
sleep(5)
|
15
|
+
end
|
16
|
+
|
8
17
|
break if $exit
|
9
|
-
run
|
10
18
|
end
|
11
19
|
end
|
12
20
|
|
13
21
|
def self.run
|
22
|
+
success = 0
|
23
|
+
failure = 0
|
24
|
+
|
14
25
|
find_available.each do |job_spec|
|
15
26
|
break if $exit
|
16
27
|
if lock_exclusively!(job_spec)
|
17
|
-
run_forked(job_spec)
|
28
|
+
if run_forked(job_spec)
|
29
|
+
success += 1
|
30
|
+
else
|
31
|
+
failure += 1
|
32
|
+
end
|
18
33
|
end
|
19
34
|
end
|
35
|
+
|
36
|
+
[success, failure]
|
20
37
|
end
|
21
38
|
|
22
39
|
def self.run_forked(job_spec)
|
40
|
+
read, write = IO.pipe
|
41
|
+
|
23
42
|
pid = fork do
|
24
43
|
job = job_spec.job
|
25
44
|
$0 = "Cascade::Job : #{name} : #{job.describe}"
|
26
|
-
run_job(job_spec, job)
|
45
|
+
if run_job(job_spec, job)
|
46
|
+
write.puts '1'
|
47
|
+
else
|
48
|
+
write.puts '0'
|
49
|
+
end
|
27
50
|
end
|
51
|
+
write.close
|
52
|
+
result = read.read.strip
|
28
53
|
Process.wait(pid)
|
54
|
+
|
55
|
+
return result == '1'
|
29
56
|
end
|
30
57
|
|
31
58
|
def self.run_job(job_spec, job)
|
59
|
+
job_spec.re_run = false
|
32
60
|
completed_successully = true
|
33
61
|
begin
|
34
62
|
job.run_callbacks(:before_run, job_spec)
|
63
|
+
|
35
64
|
job.run
|
65
|
+
|
36
66
|
job.run_callbacks(:on_success, job_spec)
|
67
|
+
|
37
68
|
rescue Exception => ex
|
38
69
|
job_spec.last_error = [ex, ex.backtrace].flatten.join("\n")
|
39
70
|
job_spec.failed_at = Time.now.utc
|
71
|
+
|
40
72
|
job.run_callbacks(:on_error, job_spec)
|
73
|
+
|
41
74
|
completed_successully = false
|
42
75
|
ensure
|
43
76
|
job.run_callbacks(:after_run, job_spec)
|
77
|
+
|
78
|
+
job_spec.locked_by = nil
|
79
|
+
job_spec.locked_at = nil
|
44
80
|
end
|
45
81
|
if completed_successully && !job_spec.re_run?
|
46
82
|
job_spec.destroy
|
@@ -76,7 +112,7 @@ module Cascade
|
|
76
112
|
end
|
77
113
|
|
78
114
|
private
|
79
|
-
def self.find_available
|
115
|
+
def self.find_available(num = 10)
|
80
116
|
right_now = Time.now.utc
|
81
117
|
|
82
118
|
conditions = {
|
@@ -85,7 +121,7 @@ module Cascade
|
|
85
121
|
:locked_at => nil
|
86
122
|
}
|
87
123
|
|
88
|
-
job_specs = JobSpec.where(conditions).limit(-
|
124
|
+
job_specs = JobSpec.where(conditions).limit(-num).sort([[:priority, 1], [:run_at, 1]]).all
|
89
125
|
job_specs
|
90
126
|
end
|
91
127
|
|