Skip to content

Commit 9001fcf

Browse files
committed
make new kill child logic opt-in
1 parent 5f54f44 commit 9001fcf

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

lib/resque/tasks.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
worker.verbose = ENV['LOGGING'] || ENV['VERBOSE']
1616
worker.very_verbose = ENV['VVERBOSE']
1717
worker.term_timeout = ENV['RESQUE_TERM_TIMEOUT'] || 4.0
18+
worker.term_child = ENV['TERM_CHILD'].to_i || 1
1819
rescue Resque::NoQueueError
1920
abort "set QUEUE env var, e.g. $ QUEUE=critical,high rake resque:work"
2021
end

lib/resque/worker.rb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class Worker
2222

2323
attr_accessor :term_timeout
2424

25+
# decide whether to use new_kill_child logic
26+
attr_accessor :term_child
27+
2528
attr_writer :to_s
2629

2730
# Returns an array of all worker objects.
@@ -278,7 +281,11 @@ def register_signal_handlers
278281

279282
begin
280283
trap('QUIT') { shutdown }
281-
trap('USR1') { kill_child }
284+
if term_child
285+
trap('USR1') { new_kill_child }
286+
else
287+
trap('USR1') { kill_child }
288+
end
282289
trap('USR2') { pause_processing }
283290
trap('CONT') { unpause_processing }
284291
rescue ArgumentError
@@ -310,18 +317,36 @@ def shutdown
310317
# Kill the child and shutdown immediately.
311318
def shutdown!
312319
shutdown
313-
kill_child
320+
if term_child
321+
new_kill_child
322+
else
323+
kill_child
324+
end
314325
end
315326

316327
# Should this worker shutdown as soon as current job is finished?
317328
def shutdown?
318329
@shutdown
319330
end
320331

332+
# Kills the forked child immediately, without remorse. The job it
333+
# is processing will not be completed.
334+
def kill_child
335+
if @child
336+
log! "Killing child at #{@child}"
337+
if system("ps -o pid,state -p #{@child}")
338+
Process.kill("KILL", @child) rescue nil
339+
else
340+
log! "Child #{@child} not found, restarting."
341+
shutdown
342+
end
343+
end
344+
end
345+
321346
# Kills the forked child immediately with minimal remorse. The job it
322347
# is processing will not be completed. Send the child a TERM signal,
323348
# wait 5 seconds, and then a KILL signal if it has not quit
324-
def kill_child
349+
def new_kill_child
325350
if @child
326351
unless Process.waitpid(@child, Process::WNOHANG)
327352
log! "Sending TERM signal to child #{@child}"

test/worker_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ def self.perform( run_time, rescue_time=nil )
469469

470470
worker = Resque::Worker.new(:long_running_job)
471471
worker.term_timeout = 1
472+
worker.term_child = 1
472473

473474
worker.work(0)
474475
exit!

0 commit comments

Comments
 (0)