Skip to content

Commit

Permalink
Merge ff268ee into 6db48e7
Browse files Browse the repository at this point in the history
  • Loading branch information
m1foley committed May 28, 2013
2 parents 6db48e7 + ff268ee commit 8468d49
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
14 changes: 6 additions & 8 deletions lib/resque/child_process.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'resque/worker_hooks'
require 'resque/signal_trapper'

module Resque
# A child process processes a single job. It is created by a Resque Worker.
Expand Down Expand Up @@ -46,15 +47,12 @@ def reconnect
end

def unregister_signal_handlers
trap('TERM') { raise TermException.new("SIGTERM") }
trap('INT', 'DEFAULT')
SignalTrapper.trap('TERM') { raise TermException.new("SIGTERM") }
SignalTrapper.trap('INT', 'DEFAULT')

begin
trap('QUIT', 'DEFAULT')
trap('USR1', 'DEFAULT')
trap('USR2', 'DEFAULT')
rescue ArgumentError
end
SignalTrapper.trap_or_warn('QUIT', 'DEFAULT')
SignalTrapper.trap_or_warn('USR1', 'DEFAULT')
SignalTrapper.trap_or_warn('USR2', 'DEFAULT')
end

# Kills the forked child immediately with minimal remorse. The job it
Expand Down
5 changes: 3 additions & 2 deletions lib/resque/ioawaiter.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
require 'resque/signal_trapper'
module Resque
class IOAwaiter
def await
rd, wr = IO.pipe
trap('CONT') {
SignalTrapper.trap('CONT') {
wr.write 'x'
wr.close
}

rd.read 1
rd.close

trap('CONT', 'DEFAULT')
SignalTrapper.trap('CONT', 'DEFAULT')
end
end
end
19 changes: 19 additions & 0 deletions lib/resque/signal_trapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Resque
# Helper for trapping signals.
#
# Trap a signal (accepts same params as Signal#trap):
# SignalTrapper.trap('INT') { do_stuff }
# Trap a signal, but don't raise exception if unsupported signal:
# SignalTrapper.trap_or_warn('INT') { do_stuff }
module SignalTrapper
def self.trap(*args, &block)
Signal.trap(*args, &block)
end

def self.trap_or_warn(*args, &block)
trap(*args, &block)
rescue ArgumentError => e
warn e
end
end
end
28 changes: 10 additions & 18 deletions lib/resque/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'resque/backend'
require 'resque/ioawaiter'
require 'resque/options'
require 'resque/signal_trapper'

module Resque
# A Resque Worker processes jobs. On platforms that support fork(2),
Expand Down Expand Up @@ -310,27 +311,20 @@ def enable_gc_optimizations

# Registers the various signal handlers a worker responds to.
#
# TERM: Shutdown immediately, stop processing jobs.
# INT: Shutdown immediately, stop processing jobs.
# TERM/INT: Shutdown immediately, stop processing jobs.
# QUIT: Shutdown after the current job has finished processing.
# USR1: Kill the forked child immediately, continue processing jobs.
# USR2: Don't process any new jobs
# CONT: Start processing jobs again after a USR2
def register_signal_handlers
trap('TERM') { shutdown! }
trap('INT') { shutdown! }

begin
# The signal QUIT & USR1 is in use by the JVM and will not work correctly on jRuby
unless jruby?
trap('QUIT') { shutdown }
trap('USR1') { @child.kill }
end
trap('USR2') { pause_processing }
trap('CONT') { unpause_processing }
rescue ArgumentError
warn "Signals QUIT, USR1, USR2, and/or CONT not supported."
SignalTrapper.trap('TERM') { shutdown! }
SignalTrapper.trap('INT') { shutdown! }

# these signals are in use by the JVM and will not work correctly on jRuby
unless jruby?
SignalTrapper.trap_or_warn('QUIT') { shutdown }
SignalTrapper.trap_or_warn('USR1') { @child.kill }
end
SignalTrapper.trap_or_warn('USR2') { pause_processing }

logger.debug "Registered signals"
end
Expand All @@ -341,14 +335,12 @@ def processed!
Stat << "processed:#{self}"
end


# Tells Redis we've failed a job.
def failed!
Stat << "failed"
Stat << "failed:#{self}"
end


# Given a string, sets the procline ($0) and logs.
# Procline is always in the format of:
# resque-VERSION: STRING
Expand Down

0 comments on commit 8468d49

Please sign in to comment.