Skip to content

Commit ee1ab81

Browse files
committed
Add before_pause, and after_pause hooks to be run when the worker is paused
1 parent 4913f19 commit ee1ab81

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

lib/resque.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ def after_fork(&block)
121121
# Set the after_fork proc.
122122
attr_writer :after_fork
123123

124+
# The `before_pause` hook will be run in the parent process before the
125+
# worker has paused processing (via #pause_processing or SIGUSR2).
126+
def before_pause(&block)
127+
block ? (@before_pause = block) : @before_pause
128+
end
129+
130+
# Set the after_pause proc.
131+
attr_writer :before_pause
132+
133+
# The `after_pause` hook will be run in the parent process after the
134+
# worker has paused (via SIGCONT).
135+
def after_pause(&block)
136+
block ? (@after_pause = block) : @after_pause
137+
end
138+
139+
# Set the after_continue proc.
140+
attr_writer :after_pause
141+
124142
def to_s
125143
"Resque Client connected to #{redis_id}"
126144
end

lib/resque/worker.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,10 @@ def pause
358358
wr.write 'x'
359359
wr.close
360360
}
361+
run_hook :before_pause, self
361362
rd.read 1
362363
rd.close
364+
run_hook :after_pause, self
363365
end
364366

365367
# Stop processing jobs after the current one has completed (if we're

test/worker_test.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,52 @@ def self.perform
470470
assert_not_equal original_connection, Resque.redis.client.connection.instance_variable_get("@sock")
471471
end
472472

473+
it "Will call before_pause before it is paused" do
474+
$BEFORE_PAUSE_CALLED = false
475+
$CAPTURED_WORKER = nil
476+
477+
Resque.before_pause do |worker|
478+
$BEFORE_PAUSE_CALLED = true
479+
$CAPTURED_WORKER = worker
480+
end
481+
482+
@worker.instance_variable_set(:@paused, true)
483+
484+
assert !$BEFORE_PAUSE_CALLED
485+
486+
t = Thread.start { sleep(1); Process.kill('CONT', @worker.pid) }
487+
488+
@worker.work(0)
489+
490+
t.join
491+
492+
assert $BEFORE_PAUSE_CALLED
493+
assert_equal @worker, $CAPTURED_WORKER
494+
end
495+
496+
it "Will call after_pause after it is paused" do
497+
$AFTER_PAUSED_CALLED = false
498+
$CAPTURED_WORKER = nil
499+
500+
Resque.after_pause do |worker|
501+
$AFTER_PAUSED_CALLED = true
502+
$CAPTURED_WORKER = worker
503+
end
504+
505+
@worker.instance_variable_set(:@paused, true)
506+
507+
assert !$AFTER_PAUSED_CALLED
508+
509+
t = Thread.start { sleep(1); Process.kill('CONT', @worker.pid) }
510+
511+
@worker.work(0)
512+
513+
t.join
514+
515+
assert $AFTER_PAUSED_CALLED
516+
assert_equal @worker, $CAPTURED_WORKER
517+
end
518+
473519
if !defined?(RUBY_ENGINE) || defined?(RUBY_ENGINE) && RUBY_ENGINE != "jruby"
474520
[SignalException, Resque::TermException].each do |exception|
475521
{

0 commit comments

Comments
 (0)