0
@@ -52,17 +52,18 @@ module Merb
0
- puts "About to check if port #{port} is alive..." if Merb::Config[:verbose]
0
pidfile = pid_file(port)
0
- puts "Pidfile is #{pidfile}..." if Merb::Config[:verbose]
0
- pid = File.read(pidfile).chomp.to_i
0
- puts "Process id is #{pid}" if Merb::Config[:verbose]
0
+ pid = pid_in_file(pidfile)
0
rescue Errno::ESRCH, Errno::ENOENT
0
rescue Errno::EACCES => e
0
- Merb.fatal!("You don't have access to the PID file at #{pidfile}.", e)
0
+ Merb.fatal!("You don't have access to the PID file at #{pidfile}: #{e.message}")
0
+ def pid_in_file(pidfile)
0
+ File.read(pidfile).chomp.to_i
0
@@ -84,9 +85,32 @@ module Merb
0
Merb::BootLoader::BuildFramework.run
0
# assume that if we kill master,
0
# workers should be reaped, too
0
- if %w(main all).include?(port)
0
- Dir["#{Merb.log_path}" / "*.pid"].each do |file|
0
+ if %w(main master all).include?(port)
0
+ # if graceful exit is requested,
0
+ # send INT to master process and
0
+ # it's gonna do it's job
0
+ # Otherwise read pids from pid files
0
+ # and try to kill each process in
0
+ kill_pid(sig, pid_file("main"))
0
+ # order is important here
0
+ # there is no way to handle KILL so
0
+ # at_exit block does not work and
0
+ # processes do not clean up their
0
+ # pid files: lets do it ourselves
0
+ Dir["#{Merb.log_path}" / "*.pid"].each do |file|
0
+ ::FileUtils.rm_rf file
0
+ kill_pid(sig, pid_file("main"))
0
kill_pid(sig, pid_file(port))
0
@@ -97,29 +121,29 @@ module Merb
0
def kill_pid(sig, file)
0
- pid =
File.read(file).chomp.to_i0
+ pid =
pid_in_file(file)0
Merb.logger.fatal! "Killing pid #{pid} with #{sig}"
0
FileUtils.rm(file) if File.exist?(file)
0
- Merb.
fatal! "Failed to kill PID #{pid} with #{sig}: '#{sig}' is an invalid " \
0
+ Merb.
logger.fatal! "Failed to kill PID #{pid} with #{sig}: '#{sig}' is an invalid " \
0
"or unsupported signal number."
0
- Merb.
fatal! "Failed to kill PID #{pid} with #{sig}: Insufficient permissions."
0
+ Merb.
logger.fatal! "Failed to kill PID #{pid} with #{sig}: Insufficient permissions."
0
- Merb.
fatal! "Failed to kill PID #{pid} with #{sig}: Process is " \
0
+ Merb.
logger.fatal! "Failed to kill PID #{pid} with #{sig}: Process is " \
0
rescue Errno::EACCES => e
0
- Merb.
fatal! e.message, e
0
+ Merb.
logger.fatal! e.message
0
rescue Errno::ENOENT => e
0
# This should not cause abnormal exit, that's why
0
# we do not use Merb.fatal but instead just
0
- Merb.logger.fatal! "Could not find a PID file at #{file}. Probably process is no longer running but pid file wasn't cleaned up."
, e0
+ Merb.logger.fatal! "Could not find a PID file at #{file}. Probably process is no longer running but pid file wasn't cleaned up."
0
if !e.is_a?(SystemExit)
0
- Merb.
fatal! "Failed to kill PID #{pid} with #{sig}", e0
+ Merb.
logger.fatal! "Failed to kill PID #{pid.inspect} with #{sig.inspect}: #{e.message}"