Class: WindowsDaemon
- Includes:
- Windows::Handle, Windows::Process, Windows::Synchronize
- Defined in:
- ext/windows/service/daemon.rb
Constant Summary collapse
- LOG_FILE =
File.(File.join(Dir::COMMON_APPDATA, 'PuppetLabs', 'puppet', 'var', 'log', 'windows.log'))
- LEVELS =
[:debug, :info, :notice, :err]
Instance Method Summary collapse
- #log(msg, level) ⇒ Object
-
#log_exception(e) ⇒ Object
Interrogation handler is just for debug.
- #report_windows_event(type, id, message) ⇒ Object
- #service_init ⇒ Object
- #service_main(*argsv) ⇒ Object
- #service_pause ⇒ Object
- #service_resume ⇒ Object
- #service_shutdown ⇒ Object
- #service_stop ⇒ Object
Instance Method Details
#log(msg, level) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'ext/windows/service/daemon.rb', line 126 def log(msg, level) if LEVELS.index(level) >= @loglevel if (@LOG_TO_FILE) File.open(LOG_FILE, 'a') { |f| f.puts("#{Time.now} Puppet (#{level}): #{msg}") } end case level when :debug report_windows_event(Win32::EventLog::INFO,0x01,msg.to_s) when :info report_windows_event(Win32::EventLog::INFO,0x01,msg.to_s) when :notice report_windows_event(Win32::EventLog::INFO,0x01,msg.to_s) when :err report_windows_event(Win32::EventLog::ERR,0x03,msg.to_s) else report_windows_event(Win32::EventLog::WARN,0x02,msg.to_s) end end end |
#log_exception(e) ⇒ Object
Interrogation handler is just for debug. Can be commented out or removed entirely. def service_interrogate
log_debug('Service is being interrogated')
end
121 122 123 124 |
# File 'ext/windows/service/daemon.rb', line 121 def log_exception(e) log_err(e.) log_err(e.backtrace.join("\n")) end |
#report_windows_event(type, id, message) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'ext/windows/service/daemon.rb', line 147 def report_windows_event(type,id,) begin eventlog = nil eventlog = Win32::EventLog.open("Application") eventlog.report_event( :source => "Puppet", :event_type => type, # Win32::EventLog::INFO or WARN, ERROR :event_id => id, # 0x01 or 0x02, 0x03 etc. :data => # "the message" ) rescue Exception => e # Ignore all errors ensure if (!eventlog.nil?) eventlog.close end end end |
#service_init ⇒ Object
26 27 |
# File 'ext/windows/service/daemon.rb', line 26 def service_init end |
#service_main(*argsv) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'ext/windows/service/daemon.rb', line 29 def service_main(*argsv) argsv = (argsv << ARGV).flatten.compact args = argsv.join(' ') @loglevel = LEVELS.index(argsv.index('--debug') ? :debug : :notice) @LOG_TO_FILE = (argsv.index('--logtofile') ? true : false) if (@LOG_TO_FILE) FileUtils.mkdir_p(File.dirname(LOG_FILE)) args = args.gsub("--logtofile","") end basedir = File.(File.join(File.dirname(__FILE__), '..')) # The puppet installer registers a 'Puppet' event source. For the moment events will be logged with this key, but # it may be a good idea to split the Service and Puppet events later so it's easier to read in the windows Event Log. # # Example code to register an event source; # eventlogdll = File.expand_path(File.join(basedir, 'puppet', 'ext', 'windows', 'eventlog', 'puppetres.dll')) # if (File.exists?(eventlogdll)) # Win32::EventLog.add_event_source( # 'source' => "Application", # 'key_name' => "Puppet Agent", # 'category_count' => 3, # 'event_message_file' => eventlogdll, # 'category_message_file' => eventlogdll # ) # end puppet = File.join(basedir, 'bin', 'puppet.bat') unless File.exists?(puppet) log_err("File not found: '#{puppet}'") return end log_debug("Using '#{puppet}'") log_notice('Service started') while running? do begin runinterval = %x{ "#{puppet}" agent --configprint runinterval }.to_i if runinterval == 0 runinterval = 1800 log_err("Failed to determine runinterval, defaulting to #{runinterval} seconds") end rescue Exception => e log_exception(e) runinterval = 1800 end if state == RUNNING or state == IDLE log_notice("Executing agent with arguments: #{args}") pid = Process.create(:command_line => "\"#{puppet}\" agent --onetime #{args}", :creation_flags => Process::CREATE_NEW_CONSOLE).process_id log_debug("Process created: #{pid}") else log_debug("Service is paused. Not invoking Puppet agent") end log_debug("Service waiting for #{runinterval} seconds") sleep(runinterval) log_debug('Service woken up') end log_notice('Service stopped') rescue Exception => e log_exception(e) end |
#service_pause ⇒ Object
101 102 103 104 105 106 |
# File 'ext/windows/service/daemon.rb', line 101 def service_pause # The service will not stay in a paused stated, instead it will go back into a running state after a short period of time. This is an issue in the Win32-Service ruby code # Raised bug https://github.com/djberg96/win32-service/issues/11 and is fixed in version 0.8.3. # Because the Pause feature is so rarely used, there is no point in creating a workaround until puppet uses 0.8.3. log_notice('Service pausing. The service will not stay paused. See Puppet Issue PUP-1471 for more information') end |
#service_resume ⇒ Object
108 109 110 |
# File 'ext/windows/service/daemon.rb', line 108 def service_resume log_notice('Service resuming') end |
#service_shutdown ⇒ Object
112 113 114 |
# File 'ext/windows/service/daemon.rb', line 112 def service_shutdown log_notice('Host shutting down') end |
#service_stop ⇒ Object
96 97 98 99 |
# File 'ext/windows/service/daemon.rb', line 96 def service_stop log_notice('Service stopping') Thread.main.wakeup end |