Permalink
Browse files

updating stuff to exit the child process on finish and to signal the …

…child process when the parent receives a signal
  • Loading branch information...
1 parent ecd6de8 commit 547935e1c60b65b90e1c5c2093833b191002fd7a John Le committed Jul 7, 2011
Showing with 53 additions and 21 deletions.
  1. +11 −13 Gemfile.lock
  2. +42 −8 lib/resque-multi-job-forks.rb
View
@@ -3,26 +3,26 @@ PATH
specs:
resque-multi-job-forks (0.3.1)
json
- resque (~> 1.10.0)
+ resque (>= 1.10.0)
GEM
remote: http://rubygems.org/
specs:
- json (1.4.6)
- rack (1.2.1)
+ json (1.5.3)
+ rack (1.3.0)
rake (0.8.7)
- redis (2.1.1)
- redis-namespace (0.8.0)
+ redis (2.2.1)
+ redis-namespace (1.0.3)
redis (< 3.0.0)
- resque (1.10.0)
- json (~> 1.4.6)
- redis-namespace (~> 0.8.0)
+ resque (1.17.1)
+ json (>= 1.4.6, < 1.6)
+ redis-namespace (~> 1.0.2)
sinatra (>= 0.9.2)
vegas (~> 0.1.2)
- sinatra (1.1.0)
+ sinatra (1.2.6)
rack (~> 1.1)
- tilt (~> 1.1)
- tilt (1.1)
+ tilt (>= 1.2.2, < 2.0)
+ tilt (1.3.2)
vegas (0.1.8)
rack (>= 1.0.0)
@@ -31,7 +31,5 @@ PLATFORMS
DEPENDENCIES
bundler
- json
rake
- resque (~> 1.10.0)
resque-multi-job-forks!
@@ -7,6 +7,15 @@ class Worker
attr_accessor :jobs_per_fork
attr_reader :jobs_processed
+ def send_child_process_signal(signal)
+ if @child
+ log! "Killing child at #{@child}"
+ if system("ps -o pid,state -p #{@child}")
+ Process.kill(signal, @child) rescue nil
+ end
+ end
+ end
+
unless method_defined?(:shutdown_without_multi_job_forks)
def perform_with_multi_job_forks(job = nil)
perform_without_multi_job_forks(job)
@@ -16,19 +25,40 @@ def perform_with_multi_job_forks(job = nil)
alias_method :perform_without_multi_job_forks, :perform
alias_method :perform, :perform_with_multi_job_forks
+ def shutdown_with_multi_job_forks?
+ # if this gets changed due to a signal handler we need to
+ # release the fork
+ #
+ # if the fork has been hijacked and we have reached our limit
+ # then we need to again release the fork
+ #
+ # if the fork is hijacked? when we are in the child process
+ release_fork if fork_hijacked? && (shutdown_without_multi_job_forks? || fork_job_limit_reached?)
+ shutdown_without_multi_job_forks?
+ end
+ alias_method :shutdown_without_multi_job_forks?, :shutdown?
+ alias_method :shutdown?, :shutdown_with_multi_job_forks?
+
def shutdown_with_multi_job_forks
- release_fork if fork_hijacked? && fork_job_limit_reached?
+ send_child_process_signal("QUIT")
shutdown_without_multi_job_forks
end
- alias_method :shutdown_without_multi_job_forks, :shutdown?
- alias_method :shutdown?, :shutdown_with_multi_job_forks
+ alias_method :shutdown_without_multi_job_forks, :shutdown
+ alias_method :shutdown, :shutdown_with_multi_job_forks
+
+ def pause_processing_with_multi_job_forks
+ send_child_process_signal("USR2")
+ pause_processing_without_multi_job_forks
+ end
+ alias_method :pause_processing_without_multi_job_forks, :pause_processing
+ alias_method :pause_processing, :pause_processing_with_multi_job_forks
- def working_on_with_worker_registration(job)
- register_worker
- working_on_without_worker_registration(job)
+ def unpause_processing_with_multi_job_forks
+ send_child_process_signal("CONT")
+ unpause_processing_without_multi_job_forks
end
- alias_method :working_on_without_worker_registration, :working_on
- alias_method :working_on, :working_on_with_worker_registration
+ alias_method :unpause_processing_without_multi_job_forks, :unpause_processing
+ alias_method :unpause_processing, :unpause_processing_with_multi_job_forks
end
def fork_hijacked?
@@ -51,6 +81,10 @@ def release_fork
@release_fork_limit = @jobs_processed = @cant_fork = nil
log 'hijack over, counter terrorists win.'
@shutdown = true unless $TESTING
+ # only the child calls release_fork, and it should exit and not
+ # passively shutdown otherwise it will call unregister worker
+ # after the work loop
+ exit!
end
def fork_job_limit

0 comments on commit 547935e

Please sign in to comment.