Module: Chef::Expander::Daemonizable
Constant Summary
Constants included from Loggable
Instance Method Summary collapse
-
#configure_process ⇒ Object
Daemonizes the process if configured to do so, and ensures that only one copy of the process is running with a given config by obtaining an exclusive lock on the pidfile.
- #daemonize ⇒ Object
-
#ensure_exclusive ⇒ Object
When not forking into the background, this ensures only one chef-expander is running with a given config and writes the process id to the pidfile.
-
#release_locks ⇒ Object
Deletes the pidfile, releasing the exclusive lock on it in the process.
- #set_user_and_group ⇒ Object
Methods included from Loggable
Instance Method Details
#configure_process ⇒ Object
Daemonizes the process if configured to do so, and ensures that only one copy of the process is running with a given config by obtaining an exclusive lock on the pidfile. Also sets process user and group if so configured.
Raises
- AlreadyRunning:
-
when another process has the exclusive lock on the pidfile
- NoSuchUser:
-
when a user is configured that doesn’t exist
- NoSuchGroup:
-
when a group is configured that doesn’t exist
- SystemCallError:
-
if there is an error creating the pidfile
46 47 48 49 |
# File 'lib/chef/expander/daemonizable.rb', line 46 def configure_process Expander.config.daemonize? ? daemonize : ensure_exclusive set_user_and_group end |
#daemonize ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/chef/expander/daemonizable.rb', line 51 def daemonize acquire_locks exit if fork Process.setsid exit if fork write_pid Dir.chdir('/') STDIN.reopen("/dev/null") STDOUT.reopen("/dev/null", "a") STDERR.reopen("/dev/null", "a") end |
#ensure_exclusive ⇒ Object
When not forking into the background, this ensures only one chef-expander is running with a given config and writes the process id to the pidfile.
65 66 67 68 |
# File 'lib/chef/expander/daemonizable.rb', line 65 def ensure_exclusive acquire_locks write_pid end |
#release_locks ⇒ Object
Deletes the pidfile, releasing the exclusive lock on it in the process.
85 86 87 88 |
# File 'lib/chef/expander/daemonizable.rb', line 85 def release_locks File.unlink(@pidfile.path) if File.exist?(@pidfile.path) @pidfile.close unless @pidfile.closed? end |
#set_user_and_group ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/chef/expander/daemonizable.rb', line 70 def set_user_and_group return nil if Expander.config.user.nil? if Expander.config.group.nil? log.info {"Changing user to #{Expander.config.user}"} else log.info {"Changing user to #{Expander.config.user} and group to #{Expander.config.group}"} end unless (set_group && set_user) log.error {"Unable to change user to #{Expander.config.user} - Are you root?"} end end |