1 $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
16 require 'god/system/process'
17 require 'god/dependency_graph'
18 require 'god/timeline'
19 require 'god/configurable'
23 require 'god/behavior'
24 require 'god/behaviors/clean_pid_file'
25 require 'god/behaviors/notify_when_flapping'
27 require 'god/condition'
28 require 'god/conditions/process_running'
29 require 'god/conditions/process_exits'
30 require 'god/conditions/tries'
31 require 'god/conditions/memory_usage'
32 require 'god/conditions/cpu_usage'
33 require 'god/conditions/always'
34 require 'god/conditions/lambda'
35 require 'god/conditions/degrading_lambda'
36 require 'god/conditions/flapping'
37 require 'god/conditions/http_response_code'
38 require 'god/conditions/disk_usage'
39 require 'god/conditions/complex'
42 require 'god/contacts/email'
52 require 'god/event_handler'
53 require 'god/registry'
58 require 'god/cli/version'
59 require 'god/cli/command'
61 $:.unshift File.join(File.dirname(__FILE__), *%w[.. ext god])
63 # App wide logging system
65 LOG.datetime_format = "%Y-%m-%d %H:%M:%S "
67 def applog(watch, level, text)
68 LOG.log(watch, level, text)
71 # The $run global determines whether god should be started when the
72 # program would normally end. This should be set to true if when god
73 # should be started (e.g. `god -c <config file>`) and false otherwise
77 GOD_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
79 # Ensure that Syslog is open
86 # Return the binding of god's root level
91 # Load the event handler system
92 God::EventHandler.load
95 alias_method :abort_orig, :abort
99 applog(nil, :error, text) if text
103 alias_method :exit_orig, :exit
112 def safe_attr_accessor(*args)
114 define_method((arg.to_s + "=").intern) do |other|
115 if !self.running && self.inited
116 abort "God.#{arg} must be set before any Tasks are defined"
119 if self.running && self.inited
120 applog(nil, :warn, "God.#{arg} can't be set while god is running")
124 instance_variable_set(('@' + arg.to_s).intern, other)
127 define_method(arg) do
128 instance_variable_get(('@' + arg.to_s).intern)
137 LOG_BUFFER_SIZE_DEFAULT = 1000
138 PID_FILE_DIRECTORY_DEFAULT = '/var/run/god'
139 DRB_PORT_DEFAULT = 17165
140 DRB_ALLOW_DEFAULT = ['127.0.0.1']
141 LOG_LEVEL_DEFAULT = :info
145 safe_attr_accessor :pid,
154 attr_accessor :inited,
157 :pending_watch_states,
165 # initialize class instance variables
170 self.log_buffer_size = nil
171 self.pid_file_directory = nil
174 # Initialize internal data.
177 def self.internal_init
179 return if self.inited
184 self.pending_watches = []
185 self.pending_watch_states = {}
187 self.contact_groups = {}
190 self.log_buffer_size ||= LOG_BUFFER_SIZE_DEFAULT
191 self.pid_file_directory ||= PID_FILE_DIRECTORY_DEFAULT
192 self.port ||= DRB_PORT_DEFAULT
193 self.allow ||= DRB_ALLOW_DEFAULT
194 self.log_level ||= LOG_LEVEL_DEFAULT
197 log_level_map = {:debug => Logger::DEBUG,
198 :info => Logger::INFO,
199 :fatal => Logger::FATAL}
200 LOG.level = log_level_map[self.log_level]
202 # init has been executed
209 # Instantiate a new, empty Watch object and pass it to the mandatory
210 # block. The attributes of the watch will be set by the configuration
213 # Aborts on duplicate watch name
215 # conflicting group name
218 def self.watch(&block)
219 self.task(Watch, &block)
222 # Instantiate a new, empty Task object and yield it to the mandatory
223 # block. The attributes of the task will be set by the configuration
226 # Aborts on duplicate task name
228 # conflicting group name
231 def self.task(klass = Task)
237 # do the post-configuration
240 # if running, completely remove the watch (if necessary) to
241 # prepare for the reload
242 existing_watch = self.watches[t.name]
243 if self.running && existing_watch
244 self.pending_watch_states[existing_watch.name] = existing_watch.state
245 self.unwatch(existing_watch)
248 # ensure the new watch has a unique name
249 if self.watches[t.name] || self.groups[t.name]
250 abort "Task name '#{t.name}' already used for a Task or Group"
253 # ensure watch is internally valid
254 t.valid? || abort("Task '#{t.name}' is not valid (see above)")
256 # add to list of watches
257 self.watches[t.name] = t
259 # add to pending watches
260 self.pending_watches << t
262 # add to group if specified
264 # ensure group name hasn't been used for a watch already
265 if self.watches[t.group]
266 abort "Group name '#{t.group}' already used for a Task"
269 self.groups[t.group] ||= []
270 self.groups[t.group] << t
277 if self.running && existing_watch
278 applog(t, :info, "#{t.name} Reloaded config")
280 applog(t, :info, "#{t.name} Loaded config")
284 # Unmonitor and remove the given watch from god.
285 # +watch+ is the Watch to remove
288 def self.unwatch(watch)
290 watch.unmonitor unless watch.state == :unmonitored
295 # remove from watches
296 self.watches.delete(watch.name)
300 self.groups[watch.group].delete(watch)
303 applog(watch, :info, "#{watch.name} unwatched")
306 # Instantiate a new Contact of the given kind and send it to the block.
307 # Then prepare, validate, and record the Contact.
308 # +kind+ is the contact class specifier
310 # Aborts on invalid kind
311 # duplicate contact name
313 # conflicting group name
316 def self.contact(kind)
321 c = Contact.generate(kind)
322 rescue NoSuchContactError => e
326 # send to block so config can set attributes
327 yield(c) if block_given?
329 # call prepare on the contact
332 # remove existing contacts of same name
333 existing_contact = self.contacts[c.name]
334 if self.running && existing_contact
335 self.uncontact(existing_contact)
338 # ensure the new contact has a unique name
339 if self.contacts[c.name] || self.contact_groups[c.name]
340 abort "Contact name '#{c.name}' already used for a Contact or Contact Group"
343 # abort if the Contact is invalid, the Contact will have printed
344 # out its own error messages by now
345 unless Contact.valid?(c) && c.valid?
346 abort "Exiting on invalid contact"
349 # add to list of contacts
350 self.contacts[c.name] = c
352 # add to contact group if specified
354 # ensure group name hasn't been used for a contact already
355 if self.contacts[c.group]
356 abort "Contact Group name '#{c.group}' already used for a Contact"
359 self.contact_groups[c.group] ||= []
360 self.contact_groups[c.group] << c
364 # Remove the given contact from god.
365 # +contact+ is the Contact to remove
368 def self.uncontact(contact)
369 self.contacts.delete(contact.name)
371 self.contact_groups[contact.group].delete(contact)
375 # Control the lifecycle of the given task(s).
376 # +name+ is the name of a task/group (String)
377 # +command+ is the command to run (String)
385 # Returns String[]:task_names
386 def self.control(name, command)
387 # get the list of items
388 items = Array(self.watches[name] || self.groups[name]).dup
394 when "start", "monitor"
395 items.each { |w| jobs << Thread.new { w.monitor if w.state != :up } }
397 items.each { |w| jobs << Thread.new { w.move(:restart) } }
399 items.each { |w| jobs << Thread.new { w.unmonitor.action(:stop) if w.state != :unmonitored } }
401 items.each { |w| jobs << Thread.new { w.unmonitor if w.state != :unmonitored } }
403 items.each { |w| self.unwatch(w) }
405 raise InvalidCommandError.new
408 jobs.each { |j| j.join }
410 items.map { |x| x.name }
413 # Unmonitor and stop all tasks.
415 # Returns true on success
416 # false if all tasks could not be stopped within 10 seconds
418 self.watches.sort.each do |name, w|
420 w.unmonitor if w.state != :unmonitored
421 w.action(:stop) if w.alive?
426 return true unless self.watches.map { |name, w| w.alive? }.any?
433 # Force the termination of god.
434 # * Clean up pid file if one exists
436 # * Hard exit using exit!
438 # Never returns because the process will no longer exist!
440 FileUtils.rm_f(self.pid) if self.pid
441 self.server.stop if self.server
445 # Gather the status of each task.
449 # # => { 'mongrel' => :up, 'nginx' => :up }
451 # Returns { String:task_name => Symbol:status, ... }
454 self.watches.map do |name, w|
455 info[name] = {:state => w.state}
460 # Log lines for the given task since the specified time.
461 # +watch_name+ is the name of the task (may be abbreviated)
462 # +since+ is the Time since which to report log lines
464 # Raises God::NoSuchWatchError if no tasks matched
466 # Returns String:joined_log_lines
467 def self.running_log(watch_name, since)
468 matches = pattern_match(watch_name, self.watches.keys)
471 raise NoSuchWatchError.new
474 LOG.watch_log_since(matches.first, since)
477 # Load a config file into a running god instance. Rescues any exceptions
478 # that the config may raise and reports these back to the caller.
479 # +code+ is a String containing the config file
480 # +filename+ is the filename of the config file
482 # Returns [String[]:task_names, String:errors]
483 def self.running_load(code, filename)
491 eval(code, root_binding, filename)
492 self.pending_watches.each do |w|
493 if previous_state = self.pending_watch_states[w.name]
494 w.monitor unless previous_state == :unmonitored
496 w.monitor if w.autostart?
499 watches = self.pending_watches.dup
500 self.pending_watches.clear
501 self.pending_watch_states.clear
502 rescue Exception => e
503 # don't ever let running_load take down god
504 errors << LOG.finish_capture
506 unless e.instance_of?(SystemExit)
507 errors << e.message << "\n"
508 errors << e.backtrace.join("\n")
512 names = watches.map { |x| x.name }
516 # Load the given file(s) according to the given glob.
517 # +glob+ is the glob-enabled path to load
521 Dir[glob].each do |f|
528 unless test(?d, self.pid_file_directory)
530 FileUtils.mkdir_p(self.pid_file_directory)
531 rescue Errno::EACCES => e
532 abort "Failed to create pid file directory: #{e.message}"
538 unless test(?w, self.pid_file_directory)
539 abort "The pid file directory (#{self.pid_file_directory}) is not writable by #{Etc.getlogin}"
543 # Initialize and startup the machinery that makes god work.
552 self.server = Socket.new(self.port)
554 # start event handler system
555 EventHandler.start if EventHandler.loaded?
557 # start the timer system
560 # start monitoring any watches set to autostart
561 self.watches.values.each { |w| w.monitor if w.autostart? }
563 # clear pending watches
564 self.pending_watches.clear
569 # join the timer thread so we don't exit
573 # To be called on program exit to start god
582 # Match a shortened pattern against a list of String candidates.
583 # The pattern is expanded into a regular expression by
584 # inserting .* between each character.
585 # +pattern+ is the String containing the abbreviation
586 # +list+ is the Array of Strings to match against
590 # list = %w{ foo bar bars }
592 # God.pattern_match(list, pattern)
593 # # => ['bar', 'bars']
595 # Returns String[]:matched_elements
596 def self.pattern_match(pattern, list)
597 regex = pattern.split('').join('.*')
599 list.select do |item|
600 item =~ Regexp.new(regex)
605 # Runs immediately before the program exits. If $run is true,
606 # start god, if $run is false, exit normally.