Skip to content

Commit e5c8a47

Browse files
committed
Merge pull request resque#743 from mrzor/extra_module_hooks
Implement Resque.before_perform and Resque.after_perform
2 parents bfad2f1 + 1cf6d83 commit e5c8a47

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

lib/resque.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,36 @@ def after_pause=(block)
153153
register_hook(:after_pause, block)
154154
end
155155

156+
# The `before_perform` hook will be run in the child process before
157+
# the job code is performed. This hook will run before any
158+
# Job.before_perform hook.
159+
#
160+
# Call with a block to register a hook.
161+
# Call with no arguments to return all registered hooks.
162+
def before_perform(&block)
163+
block ? register_hook(:before_perform, block) : hooks(:before_perform)
164+
end
165+
166+
# Register an before_perform proc.
167+
def before_perform=(block)
168+
register_hook(:before_perform, block)
169+
end
170+
171+
# The `after_perform` hook will be run in the child process after
172+
# the job code has performed. This hook will run after any
173+
# Job.after_perform hook.
174+
#
175+
# Call with a block to register a hook.
176+
# Call with no arguments to return all registered hooks.
177+
def after_perform(&block)
178+
block ? register_hook(:after_perform, block) : hooks(:after_perform)
179+
end
180+
181+
# Register an after_perform proc.
182+
def after_perform=(block)
183+
register_hook(:after_perform, block)
184+
end
185+
156186
def to_s
157187
"Resque Client connected to #{redis_id}"
158188
end

lib/resque/worker.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ def process(job = nil, &block)
181181
def perform(job)
182182
begin
183183
run_hook :after_fork, job if will_fork?
184+
run_hook :before_perform, job
184185
job.perform
186+
run_hook :after_perform, job
185187
rescue Object => e
186188
Resque.logger.info "#{job.inspect} failed: #{e.inspect}"
187189
begin

test/resque_hook_test.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
Resque.before_first_fork = nil
88
Resque.before_fork = nil
99
Resque.after_fork = nil
10+
Resque.before_perform = nil
11+
Resque.after_perform = nil
1012

1113
@worker = Resque::Worker.new(:jobs)
1214

@@ -23,6 +25,8 @@ def self.perform
2325
assert_equal [], Resque.before_first_fork
2426
assert_equal [], Resque.before_fork
2527
assert_equal [], Resque.after_fork
28+
assert_equal [], Resque.before_perform
29+
assert_equal [], Resque.after_perform
2630
end
2731

2832
it 'calls before_first_fork once' do
@@ -47,6 +51,17 @@ def self.perform
4751
assert_equal(@worker.will_fork? ? 2 : 0, counter)
4852
end
4953

54+
it 'calls before_perform before each job' do
55+
counter = 0
56+
57+
Resque.before_perform { counter += 1 }
58+
2.times { Resque::Job.create(:jobs, CallNotifyJob) }
59+
60+
assert_equal(0, counter)
61+
@worker.work(0)
62+
assert_equal(2, counter)
63+
end
64+
5065
it 'calls after_fork after each job if forking' do
5166
counter = 0
5267

@@ -58,6 +73,17 @@ def self.perform
5873
assert_equal(@worker.will_fork? ? 2 : 0, counter)
5974
end
6075

76+
it 'calls after_perform after each job' do
77+
counter = 0
78+
79+
Resque.after_perform { counter += 1 }
80+
2.times { Resque::Job.create(:jobs, CallNotifyJob) }
81+
82+
assert_equal(0, counter)
83+
@worker.work(0)
84+
assert_equal(2, counter)
85+
end
86+
6187
it 'calls before_first_fork before forking' do
6288
Resque.before_first_fork { assert(!$called) }
6389

@@ -163,4 +189,31 @@ def self.perform
163189

164190
assert(first && second)
165191
end
192+
193+
it 'registers multiple before_perform' do
194+
first = false
195+
second = false
196+
197+
Resque.before_perform { first = true }
198+
Resque.before_perform { second = true }
199+
Resque::Job.create(:jobs, CallNotifyJob)
200+
201+
assert(!first && !second)
202+
@worker.work(0)
203+
assert(first && second)
204+
end
205+
206+
it 'registers multiple after_perform' do
207+
first = false
208+
second = false
209+
210+
Resque.after_perform { first = true }
211+
Resque.after_perform { second = true }
212+
Resque::Job.create(:jobs, CallNotifyJob)
213+
214+
assert(!first && !second)
215+
@worker.work(0)
216+
assert(first && second)
217+
end
218+
166219
end

0 commit comments

Comments
 (0)