Module: WatchDogger
- Defined in:
- lib/watchdogger.rb
Overview
:nodoc:
Class Method Summary collapse
-
.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object
By default, camelize converts strings to UpperCamelCase.
-
.check_daemon ⇒ Object
Check for running daemon.
-
.check_process(pid) ⇒ Object
Returns true if the system thinks that the given process is still alive.
-
.daemon ⇒ Object
Run as a daemon.
-
.init_system(options) ⇒ Object
Initializes the watchdog system, sets up the log.
-
.shutdown_daemon ⇒ Object
Shutdown the given daemon.
-
.watch_loop ⇒ Object
This is the main loop of the watcher.
Class Method Details
.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object
By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to “:lower” then camelize produces lowerCamelCase.
camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces
Examples
"active_record".camelize #=> "ActiveRecord"
"active_record".camelize(:lower) #=> "activeRecord"
"active_record/errors".camelize #=> "ActiveRecord::Errors"
"active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
91 92 93 94 95 96 97 |
# File 'lib/watchdogger.rb', line 91 def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) # :nodoc: if first_letter_in_uppercase lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } else lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1] end end |
.check_daemon ⇒ Object
Check for running daemon. Returns true if the system thinks that the daemon is still running.
106 107 108 109 |
# File 'lib/watchdogger.rb', line 106 def check_daemon return false unless(File.exists?(@pidfile)) check_process(get_pid) end |
.check_process(pid) ⇒ Object
Returns true if the system thinks that the given process is still alive
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/watchdogger.rb', line 112 def check_process(pid) begin Process.kill(0, pid) dog_log.debug('Watchdogger') { "Process #{pid} is alive" } true rescue Errno::EPERM dog_log.debug('Watchdogger') { "No permissions for process #{pid} - seems to be running."} true rescue Errno::ESRCH dog_log.debug('Watchdogger') { "Found stale process for #{pid}." } false rescue Exception => e dog_log.info('Watchdogger') { "Could not find out if process #{pid} still runs (#{e.}). Hoping for the best..." } false end end |
.daemon ⇒ Object
Run as a daemon
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/watchdogger.rb', line 68 def daemon raise(RuntimeError, "Daemon still running") if(check_daemon) raise(ArgumentError, "Not daemonizing without a logfile") unless(@logfile && @logfile.upcase != 'STDOUT' && @logfile.upcase != 'STDERR') # Test the file opening before going daemon, so we know that # it should usually work File.open(@pidfile, 'w') { |io| io << 'starting' } daemonize # Now write the pid for real File.open(@pidfile, 'w') { |io| io << Process.pid.to_s } dog_log.info('Watchdogger') { "Running as daemon with pid #{Process.pid}" } watch_loop end |
.init_system(options) ⇒ Object
Initializes the watchdog system, sets up the log. In addition to the configured Watchers and WatcherActions, the system can take the following arguments:
[*log_level*] The log level for the system log. This will apply to all log messages
[*logfile*] The log file for the system. Defaults to STDOUT
[*interval*] The watch interval in seconds. Defaults to 60
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 |
# File 'lib/watchdogger.rb', line 30 def init_system() # First setup the logging options @log_level = .get_value(:log_level) @logfile = .get_value(:logfile) DogLog.setup(@logfile, @log_level) # Now setup the actions actions = .get_value(:actions, false) raise(ArgumentError, "Actions not configured correctly.") unless(actions.is_a?(Hash)) actions.each do |act_name, | WatcherAction.register(act_name, ) end # Setupup the watchers watchers = .get_value(:watchers, false) raise(ArgumentError, "Watchers not configured correctly.") unless(watchers.is_a?(Hash)) watchers.each do |watch_name, | Watcher.register(watch_name, ) end dog_log.info('Watchdogger') { 'System Initialized' } @watch_interval = .get_value(:interval, 60).to_i @pidfile = .get_value(:pidfile) || File.join(Etc.getpwuid.dir, '.watchdogger.pid') @pidfile = File.(@pidfile) end |
.shutdown_daemon ⇒ Object
Shutdown the given daemon
100 101 102 |
# File 'lib/watchdogger.rb', line 100 def shutdown_daemon Process.kill('TERM', get_pid) end |
.watch_loop ⇒ Object
This is the main loop of the watcher
58 59 60 61 62 63 64 65 |
# File 'lib/watchdogger.rb', line 58 def watch_loop signal_traps dog_log.info('Watchdogger') { "Starting watch loop with interval #{@watch_interval}"} loop do Watcher.watch_all! sleep(@watch_interval) end end |