lib/oversip/launcher.rb in oversip-1.1.2 vs lib/oversip/launcher.rb in oversip-1.2.0
- old
+ new
@@ -1,10 +1,10 @@
module OverSIP::Launcher
extend ::OverSIP::Logger
- READY_PIPE_TIMEOUT = 6
+ READY_PIPE_TIMEOUT = 8
@log_id = "launcher"
def self.daemonize!(options)
@@ -52,10 +52,11 @@
begin
::Timeout::timeout(READY_PIPE_TIMEOUT/2) do
master_ok = (rd.read(2) rescue nil)
end
rescue ::Timeout::Error
+ log_system_crit "master process is not ready within #{READY_PIPE_TIMEOUT/2} seconds, killing it..."
begin
::Process.kill(:TERM, master_pid)
10.times do |i|
sleep 0.05
::Process.wait(master_pid, ::Process::WNOHANG) rescue nil
@@ -63,11 +64,11 @@
end
::Process.kill(0, master_pid)
::Process.kill(:KILL, master_pid) rescue nil
rescue ::Errno::ESRCH
end
- fatal "master process is not ready within #{READY_PIPE_TIMEOUT/2} seconds, killing it"
+ fatal "master process killed"
end
unless master_ok == "ok"
fatal "master process failed to start"
end
@@ -277,13 +278,33 @@
# Create PID file.
create_pid_file(options[:pid_file])
log_system_info "reactor running"
- # Run the user provided on_started callback.
- log_system_info "calling OverSIP::SystemEvents.on_started()..."
+ # Run the user provided on_initialize method.
+ log_system_info "calling OverSIP::SystemEvents.on_initialize() method..."
begin
+ ::OverSIP::SystemEvents.on_initialize
+ rescue ::Exception => e
+ log_system_crit "error calling OverSIP::SystemEvents.on_initialize():"
+ fatal e
+ end
+
+ # Run the on_started provided callbacks.
+ log_system_info "executing OverSIP::SystemCallbacks.on_started_callbacks..."
+ ::OverSIP::SystemCallbacks.on_started_callbacks.each do |cb|
+ begin
+ cb.call
+ rescue ::Exception => e
+ log_system_crit "error executing a callback in OverSIP::SystemCallbacks.on_started_callbacks:"
+ fatal e
+ end
+ end
+
+ # Run the user provided on_started method.
+ log_system_info "calling OverSIP::SystemEvents.on_started() method..."
+ begin
::OverSIP::SystemEvents.on_started
rescue ::Exception => e
log_system_crit "error calling OverSIP::SystemEvents.on_started():"
fatal e
end
@@ -321,10 +342,18 @@
end
end # def self.run
+ def self.fatal msg
+ log_system_crit msg
+ log_system_crit "exiting with error status"
+
+ terminate error=true, fatal=true
+ end
+
+
def self.create_pid_file(path)
# Check that the PID file is accesible.
begin
assert_file_is_writable_readable_deletable(path)
rescue ::OverSIP::Error => e
@@ -422,15 +451,26 @@
# Signal HUP reloads OverSIP system configuration.
trap :HUP do
log_system_notice "HUP signal received, reloading configuration files..."
::OverSIP::Config.system_reload
+
+ # Run the on_reload provided callbacks.
+ log_system_info "executing OverSIP::SystemCallbacks.on_reload_callbacks..."
+ ::OverSIP::SystemCallbacks.on_reload_callbacks.each do |cb|
+ begin
+ cb.call
+ rescue ::Exception => e
+ log_system_crit "error executing a callback in OverSIP::SystemCallbacks.on_reload_callbacks:"
+ log_system_crit e
+ end
+ end
end
# Signal USR1 reloads custom code provided by the user.
trap :USR1 do
- log_system_notice "USR1 signal received, calling OverSIP::SystemEvents.on_user_reload()..."
+ log_system_notice "USR1 signal received, calling OverSIP::SystemEvents.on_user_reload() method..."
# Run the user provided on_started callback.
begin
::OverSIP::SystemEvents.on_user_reload
rescue ::Exception => e
log_system_crit "error calling OverSIP::SystemEvents.on_user_reload():"
@@ -445,12 +485,27 @@
end
end
def self.terminate error=false, fatal=false
+ # Trap TERM/QUIT signals (we are already exiting).
+ trap(:TERM) {}
+ trap(:QUIT) {}
+
unless fatal
- # Run the user provided on_terminated callback.
- log_system_info "calling OverSIP::SystemEvents.on_terminated()..."
+ # Run the on_terminated provided callbacks.
+ log_system_info "executing OverSIP::SystemCallbacks.on_terminated_callbacks..."
+ ::OverSIP::SystemCallbacks.on_terminated_callbacks.each do |cb|
+ begin
+ cb.call error
+ rescue ::Exception => e
+ log_system_crit "error executing a callback in OverSIP::SystemCallbacks.on_terminated_callbacks:"
+ log_system_crit e
+ end
+ end
+
+ # Run the user provided on_terminated method.
+ log_system_info "calling OverSIP::SystemEvents.on_terminated() method..."
begin
::OverSIP::SystemEvents.on_terminated error
rescue ::Exception => e
log_system_crit "error calling OverSIP::SystemEvents.on_terminated():"
log_system_crit e