Skip to content

Commit 2f91e91

Browse files
committed
PooledWorker#shutdown? checks for pool master *and* for worker parent
1 parent ac6ddb4 commit 2f91e91

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

lib/resque/pool.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ def spawn_worker!(queues)
395395
worker = create_worker(queues)
396396
pid = fork do
397397
Process.setpgrp unless Resque::Pool.single_process_group
398+
worker.worker_parent_pid = Process.pid
398399
log_worker "Starting worker #{worker}"
399400
call_after_prefork!
400401
reset_sig_handlers!
@@ -407,6 +408,7 @@ def spawn_worker!(queues)
407408
def create_worker(queues)
408409
queues = queues.to_s.split(',')
409410
worker = ::Resque::Worker.new(*queues)
411+
worker.pool_master_pid = Process.pid
410412
worker.term_timeout = ENV['RESQUE_TERM_TIMEOUT'] || 4.0
411413
worker.term_child = ENV['TERM_CHILD']
412414
if worker.respond_to?(:run_at_exit_hooks=)

lib/resque/pool/pooled_worker.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
class Resque::Pool
44
module PooledWorker
5+
attr_accessor :pool_master_pid
6+
attr_accessor :worker_parent_pid
57

6-
def initialize_with_pool(*args)
7-
@pool_master_pid = Process.pid
8-
initialize_without_pool(*args)
8+
# We can't just check if we've been re-parented to PID 1 (init) because we
9+
# want to support docker (which will make the pool master PID 1).
10+
#
11+
# We also check the worker_parent_pid, because resque-multi-jobs-fork calls
12+
# Worker#shutdown? from inside the worker child process.
13+
def pool_master_has_gone_away?
14+
not potential_parent_pids.include?(Process.ppid)
915
end
1016

11-
def pool_master_has_gone_away?
12-
@pool_master_pid && @pool_master_pid != Process.ppid
17+
def potential_parent_pids
18+
[pool_master_pid, worker_parent_pid].compact
1319
end
1420

1521
def shutdown_with_pool?
@@ -18,8 +24,6 @@ def shutdown_with_pool?
1824

1925
def self.included(base)
2026
base.instance_eval do
21-
alias_method :initialize_without_pool, :initialize
22-
alias_method :initialize, :initialize_with_pool
2327
alias_method :shutdown_without_pool?, :shutdown?
2428
alias_method :shutdown?, :shutdown_with_pool?
2529
end

0 commit comments

Comments
 (0)