lib/navy/captain.rb in navy-0.0.1 vs lib/navy/captain.rb in navy-0.0.2
- old
+ new
@@ -9,18 +9,28 @@
SIG_QUEUE = []
# list of signals we care about and trap in admiral.
QUEUE_SIGS = [ :WINCH, :QUIT, :INT, :TERM, :USR1, :USR2, :HUP, :TTIN, :TTOU ]
- attr_accessor :label, :captain_pid, :timeout, :reexec_pid, :number
+ attr_accessor :label, :captain_pid, :timeout, :number
attr_reader :admiral, :options
def initialize(admiral, label, options = {})
@admiral, @options = admiral, options.dup
@label = label
- @number = 1
+ @number = @options[:number] || 1
@timeout = 15
+ # self.pid = "/tmp/navy-#{label}.pid"
+ self.after_fork = ->(captain, officer) do
+ captain.logger.info("(#{captain.label}) officer=#{officer.number} spawned pid=#{$$}")
+ end
+ self.before_fork = ->(captain, officer) do
+ captain.logger.info("(#{captain.label}) officer=#{officer.number} spawning...")
+ end
+ self.before_exec = ->(captain) do
+ captain.logger.info("forked child re-executing...")
+ end
end
def ==(other_label)
@label == other_label
end
@@ -64,11 +74,11 @@
when :TERM, :INT # immediate shutdown
stop(false)
break
when :USR1 # rotate logs
logger.info "captain[#{label}] reopening logs..."
- # Unicorn::Util.reopen_logs
+ Navy::Util.reopen_logs
logger.info "captaion[#{label}] done reopening logs"
kill_each_officer(:USR1)
when :WINCH
# if Unicorn::Configurator::RACKUP[:daemonized]
# respawn = false
@@ -132,11 +142,11 @@
self.reexec_pid = 0
# self.pid = pid.chomp('.oldbin') if pid
proc_name "captain[#{label}]"
else
officer = OFFICERS.delete(opid) rescue nil
- m = "reaped #{status.inspect} officer=#{officer.number rescue 'unknown'}"
+ m = "reaped #{status.inspect} (#{label}) officer=#{officer.number rescue 'unknown'}"
status.success? ? logger.info(m) : logger.error(m)
end
rescue Errno::ECHILD
break
end while true
@@ -145,13 +155,15 @@
def spawn_missing_officers
n = -1
until (n += 1) == @number
OFFICERS.value?(n) and next
officer = Navy::Officer.new(self, n, options[:job])
+ before_fork.call(self, officer) if before_fork
if pid = fork
OFFICERS[pid] = officer
else
+ after_fork.call(self, officer) if after_fork
officer.start
exit
end
end
self
@@ -166,21 +178,21 @@
OFFICERS.dup.each_pair { |opid,o|
o.number >= @number and kill_officer(:QUIT, opid) rescue nil
}
end
- # delivers a signal to a worker and fails gracefully if the worker
+ # delivers a signal to a officer and fails gracefully if the officer
# is no longer running.
def kill_officer(signal, opid)
logger.warn "captain[#{label}] sending #{signal} to #{opid}"
Process.kill(signal, opid)
rescue Errno::ESRCH
officer = OFFICERS.delete(opid) rescue nil
end
- # delivers a signal to each worker
+ # delivers a signal to each officer
def kill_each_officer(signal)
- OFFICERS.keys.each { |wpid| kill_officer(signal, wpid) }
+ OFFICERS.keys.each { |opid| kill_officer(signal, opid) }
end
def init_self_pipe!
SELF_PIPE.each { |io| io.close rescue nil }
SELF_PIPE.replace(Kgio::Pipe.new)
\ No newline at end of file