Skip to content

Commit 7817e35

Browse files
committed
refactor hook calling a bit
1 parent a145eda commit 7817e35

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

lib/resque/helpers.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ def redis
66
Resque.redis
77
end
88

9-
#Direct access to the before_first_fork proc
10-
def before_first_fork
11-
Resque.before_first_fork
12-
end
13-
14-
#Direct access to the after_fork proc
15-
def after_fork
16-
Resque.after_fork
17-
end
18-
199
# Given a Ruby object, returns a string suitable for storage in a
2010
# queue.
2111
def encode(object)

lib/resque/worker.rb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def work(interval = 5, &block)
112112

113113
if not @paused and job = reserve
114114
log "got: #{job.inspect}"
115+
run_hook :before_fork
115116

116117
if @child = fork
117118
rand # Reseeding
@@ -142,7 +143,7 @@ def process(job = nil)
142143
return unless job ||= reserve
143144

144145
begin
145-
call_after_fork
146+
run_hook :after_fork, job
146147
working_on job
147148
job.perform
148149
rescue Object => e
@@ -203,7 +204,7 @@ def startup
203204
enable_gc_optimizations
204205
register_signal_handlers
205206
prune_dead_workers
206-
call_before_first_fork
207+
run_hook :before_first_fork
207208
register_worker
208209
end
209210

@@ -308,18 +309,14 @@ def register_worker
308309
started!
309310
end
310311

311-
#Call any before_first_fork procs, if any
312-
def call_before_first_fork
313-
return unless before_first_fork
314-
log "Calling before_first_fork"
315-
before_first_fork.call
316-
end
312+
# Runs a named hook, passing along any arguments.
313+
def run_hook(name, *args)
314+
return unless hook = Resque.send(name)
315+
msg = "Running #{name} hook"
316+
msg << " with #{args.inspect}" if args.any?
317+
log msg
317318

318-
#Call any before_first_fork procs, if any
319-
def call_after_fork
320-
return unless after_fork
321-
log "Calling after_fork"
322-
after_fork.call
319+
args.any? ? hook.call(*args) : hook.call
323320
end
324321

325322
# Unregisters ourself as a worker. Useful when shutting down.

test/worker_test.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
setup do
55
Resque.redis.flush_all
66

7+
Resque.before_first_fork = nil
8+
Resque.before_fork = nil
9+
Resque.after_fork = nil
10+
711
@worker = Resque::Worker.new(:jobs)
812
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
913
end
@@ -243,15 +247,31 @@
243247

244248
test "Will call a before_first_fork hook only once" do
245249
Resque.redis.flush_all
246-
$BEFORE_FORK_CALLED = false
247-
Resque.before_first_fork = Proc.new { $BEFORE_FORK_CALLED = true }
250+
$BEFORE_FORK_CALLED = 0
251+
Resque.before_first_fork = Proc.new { $BEFORE_FORK_CALLED += 1 }
248252
workerA = Resque::Worker.new(:jobs)
249253
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
250254

255+
assert_equal 0, $BEFORE_FORK_CALLED
256+
257+
workerA.work(0)
258+
assert_equal 1, $BEFORE_FORK_CALLED
259+
260+
# TODO: Verify it's only run once. Not easy.
261+
# workerA.work(0)
262+
# assert_equal 1, $BEFORE_FORK_CALLED
263+
end
264+
265+
test "Will call a before_fork hook before forking" do
266+
Resque.redis.flush_all
267+
$BEFORE_FORK_CALLED = false
268+
Resque.before_fork = Proc.new { $BEFORE_FORK_CALLED = true }
269+
workerA = Resque::Worker.new(:jobs)
270+
251271
assert !$BEFORE_FORK_CALLED
272+
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
252273
workerA.work(0)
253274
assert $BEFORE_FORK_CALLED
254-
Resque.before_first_fork = nil
255275
end
256276

257277
test "Will call an after_fork hook after forking" do
@@ -264,6 +284,5 @@
264284
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
265285
workerA.work(0)
266286
assert $AFTER_FORK_CALLED
267-
Resque.after_fork = nil
268287
end
269288
end

0 commit comments

Comments
 (0)