1 $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
13 require 'god/system/process'
14 require 'god/dependency_graph'
15 require 'god/timeline'
16 require 'god/configurable'
20 require 'god/behavior'
21 require 'god/behaviors/clean_pid_file'
22 require 'god/behaviors/notify_when_flapping'
24 require 'god/condition'
25 require 'god/conditions/process_running'
26 require 'god/conditions/process_exits'
27 require 'god/conditions/tries'
28 require 'god/conditions/memory_usage'
29 require 'god/conditions/cpu_usage'
30 require 'god/conditions/always'
31 require 'god/conditions/lambda'
32 require 'god/conditions/degrading_lambda'
33 require 'god/conditions/flapping'
34 require 'god/conditions/http_response_code'
35 require 'god/conditions/disk_usage'
36 require 'god/conditions/complex'
39 require 'god/contacts/email'
49 require 'god/event_handler'
50 require 'god/registry'
55 require 'god/cli/version'
56 require 'god/cli/command'
58 $:.unshift File.join(File.dirname(__FILE__), *%w[.. ext god])
60 # App wide logging system
62 LOG.datetime_format = "%Y-%m-%d %H:%M:%S "
64 def applog(watch, level, text)
65 LOG.log(watch, level, text)
68 # The $run global determines whether god should be started when the
69 # program would normally end. This should be set to true if when god
70 # should be started (e.g. `god -c <config file>`) and false otherwise
74 GOD_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
76 # Ensure that Syslog is open
83 # Return the binding of god's root level
88 # Load the event handler system
89 God::EventHandler.load
92 alias_method :abort_orig, :abort
96 applog(nil, :error, text) if text
100 alias_method :exit_orig, :exit
109 def safe_attr_accessor(*args)
111 define_method((arg.to_s + "=").intern) do |other|
112 if !self.running && self.inited
113 abort "God.#{arg} must be set before any Tasks are defined"
116 if self.running && self.inited
117 applog(nil, :warn, "God.#{arg} can't be set while god is running")
121 instance_variable_set(('@' + arg.to_s).intern, other)
124 define_method(arg) do
125 instance_variable_get(('@' + arg.to_s).intern)
134 LOG_BUFFER_SIZE_DEFAULT = 1000
135 PID_FILE_DIRECTORY_DEFAULT = '/var/run/god'
136 DRB_PORT_DEFAULT = 17165
137 DRB_ALLOW_DEFAULT = ['127.0.0.1']
141 safe_attr_accessor :pid,
149 attr_accessor :inited,
152 :pending_watch_states,
160 # initialize class instance variables
165 self.log_buffer_size = nil
166 self.pid_file_directory = nil
168 # Initialize internal data.
171 def self.internal_init
173 return if self.inited
178 self.pending_watches = []
179 self.pending_watch_states = {}
181 self.contact_groups = {}
184 self.log_buffer_size ||= LOG_BUFFER_SIZE_DEFAULT
185 self.pid_file_directory ||= PID_FILE_DIRECTORY_DEFAULT
186 self.port ||= DRB_PORT_DEFAULT
187 self.allow ||= DRB_ALLOW_DEFAULT
188 LOG.level = Logger::INFO
190 # init has been executed
197 # Instantiate a new, empty Watch object and pass it to the mandatory
198 # block. The attributes of the watch will be set by the configuration
201 # Aborts on duplicate watch name
203 # conflicting group name
206 def self.watch(&block)
207 self.task(Watch, &block)
210 # Instantiate a new, empty Task object and yield it to the mandatory
211 # block. The attributes of the task will be set by the configuration
214 # Aborts on duplicate task name
216 # conflicting group name
219 def self.task(klass = Task)
225 # do the post-configuration
228 # if running, completely remove the watch (if necessary) to
229 # prepare for the reload
230 existing_watch = self.watches[t.name]
231 if self.running && existing_watch
232 self.pending_watch_states[existing_watch.name] = existing_watch.state
233 self.unwatch(existing_watch)
236 # ensure the new watch has a unique name
237 if self.watches[t.name] || self.groups[t.name]
238 abort "Task name '#{t.name}' already used for a Task or Group"
241 # ensure watch is internally valid
242 t.valid? || abort("Task '#{t.name}' is not valid (see above)")
244 # add to list of watches
245 self.watches[t.name] = t
247 # add to pending watches
248 self.pending_watches << t
250 # add to group if specified
252 # ensure group name hasn't been used for a watch already
253 if self.watches[t.group]
254 abort "Group name '#{t.group}' already used for a Task"
257 self.groups[t.group] ||= []
258 self.groups[t.group] << t
265 if self.running && existing_watch
266 applog(t, :info, "#{t.name} Reloaded config")
268 applog(t, :info, "#{t.name} Loaded config")
272 # Unmonitor and remove the given watch from god.
273 # +watch+ is the Watch to remove
276 def self.unwatch(watch)
278 watch.unmonitor unless watch.state == :unmonitored
283 # remove from watches
284 self.watches.delete(watch.name)
288 self.groups[watch.group].delete(watch)
292 # Instantiate a new Contact of the given kind and send it to the block.
293 # Then prepare, validate, and record the Contact.
294 # +kind+ is the contact class specifier
296 # Aborts on invalid kind
297 # duplicate contact name
299 # conflicting group name
302 def self.contact(kind)
307 c = Contact.generate(kind)
308 rescue NoSuchContactError => e
312 # send to block so config can set attributes
313 yield(c) if block_given?
315 # call prepare on the contact
318 # remove existing contacts of same name
319 existing_contact = self.contacts[c.name]
320 if self.running && existing_contact
321 self.uncontact(existing_contact)
324 # ensure the new contact has a unique name
325 if self.contacts[c.name] || self.contact_groups[c.name]
326 abort "Contact name '#{c.name}' already used for a Contact or Contact Group"
329 # abort if the Contact is invalid, the Contact will have printed
330 # out its own error messages by now
331 unless Contact.valid?(c) && c.valid?
332 abort "Exiting on invalid contact"
335 # add to list of contacts
336 self.contacts[c.name] = c
338 # add to contact group if specified
340 # ensure group name hasn't been used for a contact already
341 if self.contacts[c.group]
342 abort "Contact Group name '#{c.group}' already used for a Contact"
345 self.contact_groups[c.group] ||= []
346 self.contact_groups[c.group] << c
350 # Remove the given contact from god.
351 # +contact+ is the Contact to remove
354 def self.uncontact(contact)
355 self.contacts.delete(contact.name)
357 self.contact_groups[contact.group].delete(contact)
361 # Control the lifecycle of the given task(s).
362 # +name+ is the name of a task/group (String)
363 # +command+ is the command to run (String)
371 # Returns String[]:task_names
372 def self.control(name, command)
373 # get the list of watches
374 watches = Array(self.watches[name] || self.groups[name])
380 when "start", "monitor"
381 watches.each { |w| jobs << Thread.new { w.monitor if w.state != :up } }
383 watches.each { |w| jobs << Thread.new { w.move(:restart) } }
385 watches.each { |w| jobs << Thread.new { w.unmonitor.action(:stop) if w.state != :unmonitored } }
387 watches.each { |w| jobs << Thread.new { w.unmonitor if w.state != :unmonitored } }
389 watches.each { |w| jobs << Thread.new { self.unwatch(w) } }
391 raise InvalidCommandError.new
394 jobs.each { |j| j.join }
396 watches.map { |x| x.name }
399 # Unmonitor and stop all tasks.
401 # Returns true on success
402 # false if all tasks could not be stopped within 10 seconds
404 self.watches.sort.each do |name, w|
406 w.unmonitor if w.state != :unmonitored
407 w.action(:stop) if w.alive?
412 return true unless self.watches.map { |name, w| w.alive? }.any?
419 # Force the termination of god.
420 # * Clean up pid file if one exists
422 # * Hard exit using exit!
424 # Never returns because the process will no longer exist!
426 FileUtils.rm_f(self.pid) if self.pid
427 self.server.stop if self.server
431 # Gather the status of each task.
435 # # => { 'mongrel' => :up, 'nginx' => :up }
437 # Returns { String:task_name => Symbol:status, ... }
440 self.watches.map do |name, w|
441 info[name] = {:state => w.state}
446 # Log lines for the given task since the specified time.
447 # +watch_name+ is the name of the task (may be abbreviated)
448 # +since+ is the Time since which to report log lines
450 # Raises God::NoSuchWatchError if no tasks matched
452 # Returns String:joined_log_lines
453 def self.running_log(watch_name, since)
454 matches = pattern_match(watch_name, self.watches.keys)
457 raise NoSuchWatchError.new
460 LOG.watch_log_since(matches.first, since)
463 # Load a config file into a running god instance. Rescues any exceptions
464 # that the config may raise and reports these back to the caller.
465 # +code+ is a String containing the config file
466 # +filename+ is the filename of the config file
468 # Returns [String[]:task_names, String:errors]
469 def self.running_load(code, filename)
476 eval(code, root_binding, filename)
477 self.pending_watches.each do |w|
478 if previous_state = self.pending_watch_states[w.name]
479 w.monitor unless previous_state == :unmonitored
481 w.monitor if w.autostart?
484 watches = self.pending_watches.dup
485 self.pending_watches.clear
486 self.pending_watch_states.clear
487 rescue Exception => e
488 # don't ever let running_load take down god
489 errors << LOG.finish_capture
491 unless e.instance_of?(SystemExit)
492 errors << e.message << "\n"
493 errors << e.backtrace.join("\n")
497 names = watches.map { |x| x.name }
501 # Load the given file(s) according to the given glob.
502 # +glob+ is the glob-enabled path to load
506 Dir[glob].each do |f|
513 unless test(?d, self.pid_file_directory)
515 FileUtils.mkdir_p(self.pid_file_directory)
516 rescue Errno::EACCES => e
517 abort "Failed to create pid file directory: #{e.message}"
523 unless test(?w, self.pid_file_directory)
524 abort "The pid file directory (#{self.pid_file_directory}) is not writable by #{Etc.getlogin}"
528 # Initialize and startup the machinery that makes god work.
537 self.server = Socket.new(self.port)
539 # start event handler system
540 EventHandler.start if EventHandler.loaded?
542 # start the timer system
545 # start monitoring any watches set to autostart
546 self.watches.values.each { |w| w.monitor if w.autostart? }
548 # clear pending watches
549 self.pending_watches.clear
554 # join the timer thread so we don't exit
558 # To be called on program exit to start god
567 # Match a shortened pattern against a list of String candidates.
568 # The pattern is expanded into a regular expression by
569 # inserting .* between each character.
570 # +pattern+ is the String containing the abbreviation
571 # +list+ is the Array of Strings to match against
575 # list = %w{ foo bar bars }
577 # God.pattern_match(list, pattern)
578 # # => ['bar', 'bars']
580 # Returns String[]:matched_elements
581 def self.pattern_match(pattern, list)
582 regex = pattern.split('').join('.*')
584 list.select do |item|
585 item =~ Regexp.new(regex)
590 # Runs immediately before the program exits. If $run is true,
591 # start god, if $run is false, exit normally.