Skip to content

Commit

Permalink
PooledWorker#shutdown? checks for pool master *and* for worker parent
Browse files Browse the repository at this point in the history
  • Loading branch information
nevans committed Jan 28, 2015
1 parent ac6ddb4 commit 2f91e91
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/resque/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ def spawn_worker!(queues)
worker = create_worker(queues)
pid = fork do
Process.setpgrp unless Resque::Pool.single_process_group
worker.worker_parent_pid = Process.pid
log_worker "Starting worker #{worker}"
call_after_prefork!
reset_sig_handlers!
Expand All @@ -407,6 +408,7 @@ def spawn_worker!(queues)
def create_worker(queues)
queues = queues.to_s.split(',')
worker = ::Resque::Worker.new(*queues)
worker.pool_master_pid = Process.pid
worker.term_timeout = ENV['RESQUE_TERM_TIMEOUT'] || 4.0
worker.term_child = ENV['TERM_CHILD']
if worker.respond_to?(:run_at_exit_hooks=)
Expand Down
18 changes: 11 additions & 7 deletions lib/resque/pool/pooled_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

class Resque::Pool
module PooledWorker
attr_accessor :pool_master_pid
attr_accessor :worker_parent_pid

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

def pool_master_has_gone_away?
@pool_master_pid && @pool_master_pid != Process.ppid
def potential_parent_pids
[pool_master_pid, worker_parent_pid].compact
end

def shutdown_with_pool?
Expand All @@ -18,8 +24,6 @@ def shutdown_with_pool?

def self.included(base)
base.instance_eval do
alias_method :initialize_without_pool, :initialize
alias_method :initialize, :initialize_with_pool
alias_method :shutdown_without_pool?, :shutdown?
alias_method :shutdown?, :shutdown_with_pool?
end
Expand Down

0 comments on commit 2f91e91

Please sign in to comment.