Skip to content

Commit 4544e63

Browse files
tenderlovehone
authored andcommitted
use a pipe to put the main process to sleep
Conflicts: lib/resque/worker.rb
1 parent c7d2021 commit 4544e63

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lib/resque/worker.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ def work(interval = 5.0, &block)
128128
loop do
129129
break if shutdown?
130130

131-
if not paused? and job = reserve(interval)
131+
pause if should_pause?
132+
133+
if job = reserve(interval)
132134
log "got: #{job.inspect}"
133135
job.worker = self
134136
run_hook :before_fork, job
@@ -273,7 +275,6 @@ def register_signal_handlers
273275
trap('QUIT') { shutdown }
274276
trap('USR1') { kill_child }
275277
trap('USR2') { pause_processing }
276-
trap('CONT') { unpause_processing }
277278
rescue ArgumentError
278279
warn "Signals QUIT, USR1, USR2, and/or CONT not supported."
279280
end
@@ -314,9 +315,22 @@ def kill_child
314315
end
315316

316317
# are we paused?
317-
def paused?
318+
def should_pause?
318319
@paused
319320
end
321+
alias :paused? :should_pause?
322+
323+
def pause
324+
rd, wr = IO.pipe
325+
trap('CONT') {
326+
log "CONT received; resuming job processing"
327+
@paused = false
328+
wr.write 'x'
329+
wr.close
330+
}
331+
rd.read 1
332+
rd.close
333+
end
320334

321335
# Stop processing jobs after the current one has completed (if we're
322336
# currently running one).
@@ -325,12 +339,6 @@ def pause_processing
325339
@paused = true
326340
end
327341

328-
# Start processing jobs again after a pause
329-
def unpause_processing
330-
log "CONT received; resuming job processing"
331-
@paused = false
332-
end
333-
334342
# Looks for any workers which should be running on this server
335343
# and, if they're not, removes them from Redis.
336344
#

0 commit comments

Comments
 (0)