lib/daemon_kit/initializer.rb in kennethkalmer-daemon-kit-0.1.7.9 vs lib/daemon_kit/initializer.rb in kennethkalmer-daemon-kit-0.1.7.10
- old
+ new
@@ -35,10 +35,14 @@
def trap( *args, &block )
self.configuration.trap( *args, &block )
end
+ def at_shutdown( &block )
+ self.configuration.at_shutdown( &block )
+ end
+
end
# This class does all the nightmare work of setting up a working
# environment for your daemon.
@@ -61,13 +65,21 @@
end
def self.shutdown( clean = false )
return unless $daemon_kit_shutdown_hooks_ran.nil?
$daemon_kit_shutdown_hooks_ran = true
-
+
DaemonKit.logger.info "Running shutdown hooks"
+ DaemonKit.configuration.shutdown_hooks.each do |hook|
+ begin
+ hook.call
+ rescue => e
+ DaemonKit.logger.exception( e )
+ end
+ end
+
log_exceptions if DaemonKit.configuration.backtraces && !clean
DaemonKit.logger.warn "Shutting down #{DaemonKit.configuration.daemon_name}"
exit
end
@@ -85,20 +97,30 @@
load_environment
load_predaemonize_configs
end
def after_daemonize
+ set_umask
+
initialize_logger
initialize_signal_traps
include_core_lib
load_postdaemonize_configs
configure_backtraces
set_process_name
DaemonKit.logger.info( "DaemonKit (#{DaemonKit::VERSION}) booted, now running #{DaemonKit.configuration.daemon_name}" )
+
+ if DaemonKit.configuration.user || DaemonKit.configuration.group
+ euid = Process.euid
+ egid = Process.egid
+ uid = Process.uid
+ gid = Process.gid
+ DaemonKit.logger.info( "DaemonKit dropped privileges to: #{euid} (EUID), #{egid} (EGID), #{uid} (UID), #{gid} (GID)" )
+ end
end
def set_load_path
configuration.load_paths.each do |d|
$:.unshift( "#{DAEMON_ROOT}/#{d}" ) if File.directory?( "#{DAEMON_ROOT}/#{d}" )
@@ -136,10 +158,14 @@
Dir[ File.join( DAEMON_ROOT, 'config', 'post-daemonize', '*.rb' ) ].each do |f|
require f
end
end
+ def set_umask
+ File.umask configuration.umask
+ end
+
def initialize_logger
return if DaemonKit.logger
unless logger = configuration.logger
logger = AbstractLogger.new( configuration.log_path )
@@ -244,15 +270,27 @@
configurable :force_kill_wait
# Should be log backtraces
configurable :backtraces, false
+ # Configurable umask
+ configurable :umask, 0022
+
+ # Configurable user
+ configurable :user, :locked => true
+
+ # Confgiruable group
+ configurable :group, :locked => true
+
# Collection of signal traps
attr_reader :signal_traps
# Our safety net (#Safety) instance
attr_accessor :safety_net
+
+ # :nodoc: Shutdown hooks
+ attr_reader :shutdown_hooks
def initialize
parse_arguments!
set_root_path!
@@ -265,10 +303,11 @@
self.force_kill_wait = false
self.safety_net = DaemonKit::Safety.instance
@signal_traps = {}
+ @shutdown_hooks = []
end
def environment
::DAEMON_ENV
end
@@ -290,9 +329,16 @@
unless @signal_traps.has_key?( signal )
set_trap( signal )
end
@signal_traps[signal].unshift( proc || block )
+ end
+
+ # Add a block or proc to be called during shutdown
+ def at_shutdown( proc = nil, &block )
+ return if proc.nil? && !block_given?
+
+ @shutdown_hooks << ( proc || block )
end
def pid_file
@pid_file ||= "#{File.dirname(self.log_path)}/#{self.daemon_name}.pid"
end