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']
144 safe_attr_accessor :pid,
152 attr_accessor :inited,
155 :pending_watch_states,
163 # initialize class instance variables
168 self.log_buffer_size = nil
169 self.pid_file_directory = nil
171 # Initialize internal data.
174 def self.internal_init
176 return if self.inited
181 self.pending_watches = []
182 self.pending_watch_states = {}
184 self.contact_groups = {}
187 self.log_buffer_size ||= LOG_BUFFER_SIZE_DEFAULT
188 self.pid_file_directory ||= PID_FILE_DIRECTORY_DEFAULT
189 self.port ||= DRB_PORT_DEFAULT
190 self.allow ||= DRB_ALLOW_DEFAULT
191 LOG.level = Logger::INFO
193 # init has been executed
200 # Instantiate a new, empty Watch object and pass it to the mandatory
201 # block. The attributes of the watch will be set by the configuration
204 # Aborts on duplicate watch name
206 # conflicting group name
209 def self.watch(&block)
210 self.task(Watch, &block)
213 # Instantiate a new, empty Task object and yield it to the mandatory
214 # block. The attributes of the task will be set by the configuration
217 # Aborts on duplicate task name
219 # conflicting group name
222 def self.task(klass = Task)
228 # do the post-configuration
231 # if running, completely remove the watch (if necessary) to
232 # prepare for the reload
233 existing_watch = self.watches[t.name]
234 if self.running && existing_watch
235 self.pending_watch_states[existing_watch.name] = existing_watch.state
236 self.unwatch(existing_watch)
239 # ensure the new watch has a unique name
240 if self.watches[t.name] || self.groups[t.name]
241 abort "Task name '#{t.name}' already used for a Task or Group"
244 # ensure watch is internally valid
245 t.valid? || abort("Task '#{t.name}' is not valid (see above)")
247 # add to list of watches
248 self.watches[t.name] = t
250 # add to pending watches
251 self.pending_watches << t
253 # add to group if specified
255 # ensure group name hasn't been used for a watch already
256 if self.watches[t.group]
257 abort "Group name '#{t.group}' already used for a Task"
260 self.groups[t.group] ||= []
261 self.groups[t.group] << t
268 if self.running && existing_watch
269 applog(t, :info, "#{t.name} Reloaded config")
271 applog(t, :info, "#{t.name} Loaded config")
275 # Unmonitor and remove the given watch from god.
276 # +watch+ is the Watch to remove
279 def self.unwatch(watch)
281 watch.unmonitor unless watch.state == :unmonitored
286 # remove from watches
287 self.watches.delete(watch.name)
291 self.groups[watch.group].delete(watch)
294 applog(watch, :info, "#{watch.name} unwatched")
297 # Instantiate a new Contact of the given kind and send it to the block.
298 # Then prepare, validate, and record the Contact.
299 # +kind+ is the contact class specifier
301 # Aborts on invalid kind
302 # duplicate contact name
304 # conflicting group name
307 def self.contact(kind)
312 c = Contact.generate(kind)
313 rescue NoSuchContactError => e
317 # send to block so config can set attributes
318 yield(c) if block_given?
320 # call prepare on the contact
323 # remove existing contacts of same name
324 existing_contact = self.contacts[c.name]
325 if self.running && existing_contact
326 self.uncontact(existing_contact)
329 # ensure the new contact has a unique name
330 if self.contacts[c.name] || self.contact_groups[c.name]
331 abort "Contact name '#{c.name}' already used for a Contact or Contact Group"
334 # abort if the Contact is invalid, the Contact will have printed
335 # out its own error messages by now
336 unless Contact.valid?(c) && c.valid?
337 abort "Exiting on invalid contact"
340 # add to list of contacts
341 self.contacts[c.name] = c
343 # add to contact group if specified
345 # ensure group name hasn't been used for a contact already
346 if self.contacts[c.group]
347 abort "Contact Group name '#{c.group}' already used for a Contact"
350 self.contact_groups[c.group] ||= []
351 self.contact_groups[c.group] << c
355 # Remove the given contact from god.
356 # +contact+ is the Contact to remove
359 def self.uncontact(contact)
360 self.contacts.delete(contact.name)
362 self.contact_groups[contact.group].delete(contact)
366 # Control the lifecycle of the given task(s).
367 # +name+ is the name of a task/group (String)
368 # +command+ is the command to run (String)
376 # Returns String[]:task_names
377 def self.control(name, command)
378 # get the list of items
379 items = Array(self.watches[name] || self.groups[name]).dup
385 when "start", "monitor"
386 items.each { |w| jobs << Thread.new { w.monitor if w.state != :up } }
388 items.each { |w| jobs << Thread.new { w.move(:restart) } }
390 items.each { |w| jobs << Thread.new { w.unmonitor.action(:stop) if w.state != :unmonitored } }
392 items.each { |w| jobs << Thread.new { w.unmonitor if w.state != :unmonitored } }
394 items.each { |w| self.unwatch(w) }
396 raise InvalidCommandError.new
399 jobs.each { |j| j.join }
401 items.map { |x| x.name }
404 # Unmonitor and stop all tasks.
406 # Returns true on success
407 # false if all tasks could not be stopped within 10 seconds
409 self.watches.sort.each do |name, w|
411 w.unmonitor if w.state != :unmonitored
412 w.action(:stop) if w.alive?
417 return true unless self.watches.map { |name, w| w.alive? }.any?
424 # Force the termination of god.
425 # * Clean up pid file if one exists
427 # * Hard exit using exit!
429 # Never returns because the process will no longer exist!
431 FileUtils.rm_f(self.pid) if self.pid
432 self.server.stop if self.server
436 # Gather the status of each task.
440 # # => { 'mongrel' => :up, 'nginx' => :up }
442 # Returns { String:task_name => Symbol:status, ... }
445 self.watches.map do |name, w|
446 info[name] = {:state => w.state}
451 # Log lines for the given task since the specified time.
452 # +watch_name+ is the name of the task (may be abbreviated)
453 # +since+ is the Time since which to report log lines
455 # Raises God::NoSuchWatchError if no tasks matched
457 # Returns String:joined_log_lines
458 def self.running_log(watch_name, since)
459 matches = pattern_match(watch_name, self.watches.keys)
462 raise NoSuchWatchError.new
465 LOG.watch_log_since(matches.first, since)
468 # Load a config file into a running god instance. Rescues any exceptions
469 # that the config may raise and reports these back to the caller.
470 # +code+ is a String containing the config file
471 # +filename+ is the filename of the config file
473 # Returns [String[]:task_names, String:errors]
474 def self.running_load(code, filename)
482 eval(code, root_binding, filename)
483 self.pending_watches.each do |w|
484 if previous_state = self.pending_watch_states[w.name]
485 w.monitor unless previous_state == :unmonitored
487 w.monitor if w.autostart?
490 watches = self.pending_watches.dup
491 self.pending_watches.clear
492 self.pending_watch_states.clear
493 rescue Exception => e
494 # don't ever let running_load take down god
495 errors << LOG.finish_capture
497 unless e.instance_of?(SystemExit)
498 errors << e.message << "\n"
499 errors << e.backtrace.join("\n")
503 names = watches.map { |x| x.name }
507 # Load the given file(s) according to the given glob.
508 # +glob+ is the glob-enabled path to load
512 Dir[glob].each do |f|
519 unless test(?d, self.pid_file_directory)
521 FileUtils.mkdir_p(self.pid_file_directory)
522 rescue Errno::EACCES => e
523 abort "Failed to create pid file directory: #{e.message}"
529 unless test(?w, self.pid_file_directory)
530 abort "The pid file directory (#{self.pid_file_directory}) is not writable by #{Etc.getlogin}"
534 # Initialize and startup the machinery that makes god work.
543 self.server = Socket.new(self.port)
545 # start event handler system
546 EventHandler.start if EventHandler.loaded?
548 # start the timer system
551 # start monitoring any watches set to autostart
552 self.watches.values.each { |w| w.monitor if w.autostart? }
554 # clear pending watches
555 self.pending_watches.clear
560 # join the timer thread so we don't exit
564 # To be called on program exit to start god
573 # Match a shortened pattern against a list of String candidates.
574 # The pattern is expanded into a regular expression by
575 # inserting .* between each character.
576 # +pattern+ is the String containing the abbreviation
577 # +list+ is the Array of Strings to match against
581 # list = %w{ foo bar bars }
583 # God.pattern_match(list, pattern)
584 # # => ['bar', 'bars']
586 # Returns String[]:matched_elements
587 def self.pattern_match(pattern, list)
588 regex = pattern.split('').join('.*')
590 list.select do |item|
591 item =~ Regexp.new(regex)
596 # Runs immediately before the program exits. If $run is true,
597 # start god, if $run is false, exit normally.