Class: Puppet::Daemon Private
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Run periodic actions and a network server in a daemonized process.
A Daemon has 3 parts:
* config reparse
* (optional) an agent that responds to #run
* (optional) a server that response to #stop, #start, and #wait_for_shutdown
The config reparse will occur periodically based on Settings. The server will be started and is expected to manage its own run loop (and so not block the start call). The server will, however, still be waited for by using the #wait_for_shutdown method. The agent is run periodically and a time interval based on Settings. The config reparse will update this time interval when needed.
The Daemon is also responsible for signal handling, starting, stopping, running the agent on demand, and reloading the entire process. It ensures that only one Daemon is running by using a lockfile.
Instance Attribute Summary collapse
- #agent ⇒ Object private
- #argv ⇒ Object private
- #server ⇒ Object private
Class Method Summary collapse
-
.close_streams ⇒ Object
private
Close stdin/stdout/stderr so that we can finish our transition into ‘daemon’ mode.
Instance Method Summary collapse
-
#close_streams ⇒ Object
private
Convenience signature for calling Puppet::Daemon.close_streams.
-
#daemonize ⇒ Object
private
Put the daemon into the background.
- #daemonname ⇒ Object private
-
#initialize(pidfile, scheduler = Puppet::Scheduler::Scheduler.new()) ⇒ Daemon
constructor
private
A new instance of Daemon.
- #reexec ⇒ Object private
- #reload ⇒ Object private
- #reopen_logs ⇒ Object private
- #restart ⇒ Object private
-
#set_signal_traps ⇒ Object
private
Trap a couple of the main signals.
- #start ⇒ Object private
-
#stop(args = {:exit => true}) ⇒ Object
private
Stop everything.
Constructor Details
#initialize(pidfile, scheduler = Puppet::Scheduler::Scheduler.new()) ⇒ Daemon
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Daemon.
26 27 28 29 |
# File 'lib/puppet/daemon.rb', line 26 def initialize(pidfile, scheduler = Puppet::Scheduler::Scheduler.new()) @scheduler = scheduler @pidfile = pidfile end |
Instance Attribute Details
#agent ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
24 25 26 |
# File 'lib/puppet/daemon.rb', line 24 def agent @agent end |
#argv ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
24 25 26 |
# File 'lib/puppet/daemon.rb', line 24 def argv @argv end |
#server ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
24 25 26 |
# File 'lib/puppet/daemon.rb', line 24 def server @server end |
Class Method Details
.close_streams ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Close stdin/stdout/stderr so that we can finish our transition into ‘daemon’ mode.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/puppet/daemon.rb', line 55 def self.close_streams() Puppet.debug("Closing streams for daemon mode") begin $stdin.reopen "/dev/null" $stdout.reopen "/dev/null", "a" $stderr.reopen $stdout Puppet::Util::Log.reopen Puppet.debug("Finished closing streams for daemon mode") rescue => detail Puppet.err "Could not start #{Puppet.run_mode.name}: #{detail}" Puppet::Util::replace_file("/tmp/daemonout", 0644) do |f| f.puts "Could not start #{Puppet.run_mode.name}: #{detail}" end exit(12) end end |
Instance Method Details
#close_streams ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convenience signature for calling Puppet::Daemon.close_streams
73 74 75 |
# File 'lib/puppet/daemon.rb', line 73 def close_streams() Puppet::Daemon.close_streams end |
#daemonize ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Put the daemon into the background.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/puppet/daemon.rb', line 36 def daemonize if pid = fork Process.detach(pid) exit(0) end create_pidfile # Get rid of console logging Puppet::Util::Log.close(:console) Process.setsid Dir.chdir("/") close_streams end |
#daemonname ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
31 32 33 |
# File 'lib/puppet/daemon.rb', line 31 def daemonname Puppet.run_mode.name end |
#reexec ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
77 78 79 80 81 82 83 |
# File 'lib/puppet/daemon.rb', line 77 def reexec raise Puppet::DevError, "Cannot reexec unless ARGV arguments are set" unless argv command = $0 + " " + argv.join(" ") Puppet.notice "Restarting with '#{command}'" stop(:exit => false) exec(command) end |
#reload ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
85 86 87 88 89 90 91 92 93 |
# File 'lib/puppet/daemon.rb', line 85 def reload return unless agent if agent.running? Puppet.notice "Not triggering already-running agent" return end agent.run({:splay => false}) end |
#reopen_logs ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
100 101 102 |
# File 'lib/puppet/daemon.rb', line 100 def reopen_logs Puppet::Util::Log.reopen end |
#restart ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
95 96 97 98 |
# File 'lib/puppet/daemon.rb', line 95 def restart Puppet::Application.restart! reexec unless agent and agent.running? end |
#set_signal_traps ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Trap a couple of the main signals. This should probably be handled in a way that anyone else can register callbacks for traps, but, eh.
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/puppet/daemon.rb', line 106 def set_signal_traps signals = {:INT => :stop, :TERM => :stop } # extended signals not supported under windows signals.update({:HUP => :restart, :USR1 => :reload, :USR2 => :reopen_logs }) unless Puppet.features.microsoft_windows? signals.each do |signal, method| Signal.trap(signal) do Puppet.notice "Caught #{signal}; calling #{method}" send(method) end end end |
#start ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/puppet/daemon.rb', line 131 def start set_signal_traps create_pidfile raise Puppet::DevError, "Daemons must have an agent, server, or both" unless agent or server # Start the listening server, if required. server.start if server # Finally, loop forever running events - or, at least, until we exit. run_event_loop server.wait_for_shutdown if server end |
#stop(args = {:exit => true}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Stop everything
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/puppet/daemon.rb', line 119 def stop(args = {:exit => true}) Puppet::Application.stop! server.stop if server remove_pidfile Puppet::Util::Log.close_all exit if args[:exit] end |