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 in a daemonized process.
A Daemon has 2 parts:
* config reparse
* an agent that responds to #run
The config reparse will occur periodically based on Settings. 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.
Constant Summary collapse
- SIGNAL_CHECK_INTERVAL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
5
Instance Attribute Summary collapse
- #agent ⇒ Object readonly private
- #argv ⇒ Object private
- #signals ⇒ Object readonly 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(agent, 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(agent, 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.
27 28 29 30 31 32 33 34 |
# File 'lib/puppet/daemon.rb', line 27 def initialize(agent, pidfile, scheduler = Puppet::Scheduler::Scheduler.new()) raise Puppet::DevError, _("Daemons must have an agent") unless agent @scheduler = scheduler @pidfile = pidfile @agent = agent @signals = [] end |
Instance Attribute Details
#agent ⇒ Object (readonly)
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.
25 26 27 |
# File 'lib/puppet/daemon.rb', line 25 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 |
#signals ⇒ Object (readonly)
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.
25 26 27 |
# File 'lib/puppet/daemon.rb', line 25 def signals @signals 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.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/puppet/daemon.rb', line 61 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", 0o644) 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
79 80 81 |
# File 'lib/puppet/daemon.rb', line 79 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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/puppet/daemon.rb', line 41 def daemonize pid = fork if pid 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.
36 37 38 |
# File 'lib/puppet/daemon.rb', line 36 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.
83 84 85 86 87 88 89 90 |
# File 'lib/puppet/daemon.rb', line 83 def reexec raise Puppet::DevError, _("Cannot reexec unless ARGV arguments are set") unless argv command = $PROGRAM_NAME + " " + 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.
92 93 94 95 96 |
# File 'lib/puppet/daemon.rb', line 92 def reload agent.run({ :splay => false }) rescue Puppet::LockError Puppet.notice "Not triggering already-running agent" 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.
103 104 105 |
# File 'lib/puppet/daemon.rb', line 103 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.
98 99 100 101 |
# File 'lib/puppet/daemon.rb', line 98 def restart Puppet::Application.restart! reexec 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.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/puppet/daemon.rb', line 109 def set_signal_traps [:INT, :TERM].each do |signal| Signal.trap(signal) do Puppet.notice "Caught #{signal}; exiting" stop end end # extended signals not supported under windows unless Puppet::Util::Platform.windows? signals = { :HUP => :restart, :USR1 => :reload, :USR2 => :reopen_logs } signals.each do |signal, method| Signal.trap(signal) do Puppet.notice "Caught #{signal}; storing #{method}" @signals << method end 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.
140 141 142 143 |
# File 'lib/puppet/daemon.rb', line 140 def start create_pidfile run_event_loop 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
130 131 132 133 134 135 136 137 138 |
# File 'lib/puppet/daemon.rb', line 130 def stop(args = { :exit => true }) Puppet::Application.stop! remove_pidfile Puppet::Util::Log.close_all exit if args[:exit] end |