lib/splash/helpers.rb in prometheus-splash-0.3.0 vs lib/splash/helpers.rb in prometheus-splash-0.4.0
- old
+ new
@@ -1,5 +1,6 @@
+# @option options [String] :stdout_trace the path of the file where to redirect STDOUT
# coding: utf-8
module Splash
module Helpers
@@ -87,10 +88,13 @@
# @option options [String] :pid_file the pid filename
# @option options [String] :daemon_user the user to change privileges
# @option options [String] :daemon_group the group to change privileges
# @option options [String] :stderr_trace the path of the file where to redirect STDERR
# @option options [String] :stdout_trace the path of the file where to redirect STDOUT
+ # @option options [Proc] :sigint_handler handler Proc for SIGINT signal
+ # @option options [Proc] :sigterm_handler handler Proc for SIGTERM signal
+ # @option options [Proc] :sighup_handler handler Proc for SIGHuP signal
# @option options [Bool] :foreground option to run foreground
# @yield a process definion or block given
# @example usage inline
# class Test
# include Splash::Helpers
@@ -127,14 +131,36 @@
# @return [Fixnum] pid the pid of the forked processus
def daemonize(options)
#Process.euid = 0
#Process.egid = 0
- trap("SIGINT"){ exit! 0 }
- trap("SIGTERM"){ exit! 0 }
- trap("SIGHUP"){ exit! 0 }
- return yield if options[:foreground]
+ trap("SIGINT"){
+ if options[:sigint_handler] then
+ options[:sigint_handler].call
+ else
+ exit! 0
+ end
+ }
+ trap("SIGTERM"){
+ if options[:sigterm_handler] then
+ options[:sigterm_handler].call
+ else
+ exit! 0
+ end
+ }
+ trap("SIGHUP"){
+ if options[:sighup_handler] then
+ options[:sighup_handler].call
+ else
+ exit! 0
+ end
+ }
+ if options[:foreground]
+ change_logger logger: :dual
+ return yield
+ end
fork do
+ change_logger logger: :daemon
#Process.daemon
File.open(options[:pid_file],"w"){|f| f.puts Process.pid } if options[:pid_file]
if options[:daemon_user] and options[:daemon_group] then
uid = Etc.getpwnam(options[:daemon_user]).uid
gid = Etc.getgrnam(options[:daemon_group]).gid