Skip to content

Commit 7cce309

Browse files
committed
Merge branch 'master' of github.com:defunkt/resque
2 parents 8ddefc7 + 1031430 commit 7cce309

File tree

4 files changed

+218
-7
lines changed

4 files changed

+218
-7
lines changed

HOOKS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ like this:
6060

6161
Once this hook is made available to your job (either by way of
6262
inheritence or `extend`), it will be run before the job's `perform`
63-
method is called.
63+
method is called. Hooks of each type are executed in alphabetical order,
64+
so `before_perform_a` will always be executed before `before_perform_b`.
65+
An unnamed hook (`before_perform`) will be executed first.
6466

6567
The available hooks are:
6668

lib/resque/job.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ def perform
109109
job_args = args || []
110110
job_was_performed = false
111111

112-
before_hooks = job.methods.grep(/^before_perform/)
113-
around_hooks = job.methods.grep(/^around_perform/)
114-
after_hooks = job.methods.grep(/^after_perform/)
115-
failure_hooks = job.methods.grep(/^on_failure/)
112+
before_hooks = job.methods.grep(/^before_perform/).sort
113+
around_hooks = job.methods.grep(/^around_perform/).sort
114+
after_hooks = job.methods.grep(/^after_perform/).sort
115+
failure_hooks = job.methods.grep(/^on_failure/).sort
116116

117117
begin
118118
# Execute before_perform hook. Abort the job gracefully if
@@ -132,7 +132,7 @@ def perform
132132
else
133133
# We want to nest all around_perform plugins, with the last one
134134
# finally calling perform
135-
stack = around_hooks.inject(nil) do |last_hook, hook|
135+
stack = around_hooks.reverse.inject(nil) do |last_hook, hook|
136136
if last_hook
137137
lambda do
138138
job.send(hook, *job_args) { last_hook.call }

test/job_plugins_test.rb

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
require File.dirname(__FILE__) + '/test_helper'
2+
3+
context "Multiple plugins with multiple hooks" do
4+
include PerformJob
5+
6+
module Plugin1
7+
def before_perform_record_history1(history)
8+
history << :before1
9+
end
10+
def after_perform_record_history1(history)
11+
history << :after1
12+
end
13+
end
14+
15+
module Plugin2
16+
def before_perform_record_history2(history)
17+
history << :before2
18+
end
19+
def after_perform_record_history2(history)
20+
history << :after2
21+
end
22+
end
23+
24+
class ManyBeforesJob
25+
extend Plugin1
26+
extend Plugin2
27+
def self.perform(history)
28+
history << :perform
29+
end
30+
end
31+
32+
test "hooks of each type are executed in alphabetical order" do
33+
result = perform_job(ManyBeforesJob, history=[])
34+
assert_equal true, result, "perform returned true"
35+
assert_equal [:before1, :before2, :perform, :after1, :after2], history
36+
end
37+
end
38+
39+
context "Resque::Plugin ordering before_perform" do
40+
include PerformJob
41+
42+
module BeforePerformPlugin
43+
def before_perform1(history)
44+
history << :before_perform1
45+
end
46+
end
47+
48+
class BeforePerformJob
49+
extend BeforePerformPlugin
50+
def self.perform(history)
51+
history << :perform
52+
end
53+
def self.before_perform(history)
54+
history << :before_perform
55+
end
56+
end
57+
58+
test "before_perform hooks are executed in order" do
59+
result = perform_job(BeforePerformJob, history=[])
60+
assert_equal true, result, "perform returned true"
61+
assert_equal [:before_perform, :before_perform1, :perform], history
62+
end
63+
end
64+
65+
context "Resque::Plugin ordering after_perform" do
66+
include PerformJob
67+
68+
module AfterPerformPlugin
69+
def after_perform_record_history(history)
70+
history << :after_perform1
71+
end
72+
end
73+
74+
class AfterPerformJob
75+
extend AfterPerformPlugin
76+
def self.perform(history)
77+
history << :perform
78+
end
79+
def self.after_perform(history)
80+
history << :after_perform
81+
end
82+
end
83+
84+
test "after_perform hooks are executed in order" do
85+
result = perform_job(AfterPerformJob, history=[])
86+
assert_equal true, result, "perform returned true"
87+
assert_equal [:perform, :after_perform, :after_perform1], history
88+
end
89+
end
90+
91+
context "Resque::Plugin ordering around_perform" do
92+
include PerformJob
93+
94+
module AroundPerformPlugin1
95+
def around_perform1(history)
96+
history << :around_perform_plugin1
97+
yield
98+
end
99+
end
100+
101+
class AroundPerformJustPerformsJob
102+
extend AroundPerformPlugin1
103+
def self.perform(history)
104+
history << :perform
105+
end
106+
end
107+
108+
test "around_perform hooks are executed before the job" do
109+
result = perform_job(AroundPerformJustPerformsJob, history=[])
110+
assert_equal true, result, "perform returned true"
111+
assert_equal [:around_perform_plugin1, :perform], history
112+
end
113+
114+
class AroundPerformJob
115+
extend AroundPerformPlugin1
116+
def self.perform(history)
117+
history << :perform
118+
end
119+
def self.around_perform(history)
120+
history << :around_perform
121+
yield
122+
end
123+
end
124+
125+
test "around_perform hooks are executed in order" do
126+
result = perform_job(AroundPerformJob, history=[])
127+
assert_equal true, result, "perform returned true"
128+
assert_equal [:around_perform, :around_perform_plugin1, :perform], history
129+
end
130+
131+
module AroundPerformPlugin2
132+
def around_perform2(history)
133+
history << :around_perform_plugin2
134+
yield
135+
end
136+
end
137+
138+
class AroundPerformJob2
139+
extend AroundPerformPlugin1
140+
extend AroundPerformPlugin2
141+
def self.perform(history)
142+
history << :perform
143+
end
144+
def self.around_perform(history)
145+
history << :around_perform
146+
yield
147+
end
148+
end
149+
150+
test "many around_perform are executed in order" do
151+
result = perform_job(AroundPerformJob2, history=[])
152+
assert_equal true, result, "perform returned true"
153+
assert_equal [:around_perform, :around_perform_plugin1, :around_perform_plugin2, :perform], history
154+
end
155+
156+
module AroundPerformDoesNotYield
157+
def around_perform0(history)
158+
history << :around_perform0
159+
end
160+
end
161+
162+
class AroundPerformJob3
163+
extend AroundPerformPlugin1
164+
extend AroundPerformPlugin2
165+
extend AroundPerformDoesNotYield
166+
def self.perform(history)
167+
history << :perform
168+
end
169+
def self.around_perform(history)
170+
history << :around_perform
171+
yield
172+
end
173+
end
174+
175+
test "the job is aborted if an around_perform hook does not yield" do
176+
result = perform_job(AroundPerformJob3, history=[])
177+
assert_equal false, result, "perform returned false"
178+
assert_equal [:around_perform, :around_perform0], history
179+
end
180+
end
181+
182+
context "Resque::Plugin ordering on_failure" do
183+
include PerformJob
184+
185+
module OnFailurePlugin
186+
def on_failure1(exception, history)
187+
history << "#{exception.message} plugin"
188+
end
189+
end
190+
191+
class FailureJob
192+
extend OnFailurePlugin
193+
def self.perform(history)
194+
history << :perform
195+
raise StandardError, "oh no"
196+
end
197+
def self.on_failure(exception, history)
198+
history << exception.message
199+
end
200+
end
201+
202+
test "on_failure hooks are executed in order" do
203+
history = []
204+
assert_raises StandardError do
205+
perform_job(FailureJob, history)
206+
end
207+
assert_equal [:perform, "oh no", "oh no plugin"], history
208+
end
209+
end

test/worker_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@worker.work(0)
1818
assert_equal 1, Resque::Failure.count
1919
end
20-
20+
2121
test "failed jobs report excpetion and message" do
2222
Resque::Job.create(:jobs, BadJobWithSyntaxError)
2323
@worker.work(0)

0 commit comments

Comments
 (0)