Skip to content

Commit 320e726

Browse files
wuputahhone
authored andcommitted
use system calls instead of process tree
1 parent fd1d9ae commit 320e726

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

lib/resque/worker.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,22 +300,26 @@ def shutdown?
300300
end
301301

302302
# Kills the forked child immediately with minimal remorse. The job it
303-
# is processing will not be completed.
303+
# is processing will not be completed. Send the child a TERM signal,
304+
# wait 5 seconds, and then a KILL signal if it has not quit
304305
def kill_child
305306
if @child
306-
log! "Killing child at #{@child}"
307-
if system("ps -o pid,state -p #{@child}")
308-
Process.kill("TERM", @child) rescue nil
309-
10.times do
310-
return unless system("ps -o pid,state -p #{@child}")
307+
unless Process.waitpid(@child, Process::WNOHANG)
308+
log! "Sending TERM signal to child #{@child}"
309+
Process.kill("TERM", @child)
310+
50.times do |i|
311311
sleep(0.1)
312+
return if Process.waitpid(@child, Process::WNOHANG)
312313
end
313-
Process.kill("KILL", @child) rescue nil
314+
log! "Sending KILL signal to child #{@child}"
315+
Process.kill("KILL", @child)
314316
else
315317
log! "Child #{@child} not found, restarting."
316318
shutdown
317319
end
318320
end
321+
rescue SystemCallError
322+
log! "Child #{@child} already quit."
319323
end
320324

321325
# are we paused?

0 commit comments

Comments
 (0)