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'
37 require 'god/contacts/email'
39 require 'god/reporter'
48 require 'god/event_handler'
49 require 'god/registry'
54 $:.unshift File.join(File.dirname(__FILE__), *%w[.. ext god])
57 LOG.datetime_format = "%Y-%m-%d %H:%M:%S "
59 GOD_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
72 def with_stdout_captured
84 God::EventHandler.load
87 alias_method :abort_orig, :abort
91 LOG.log(nil, :error, text) if text
92 text ? abort_orig(text) : exit(1)
95 alias_method :exit_orig, :exit
104 def safe_attr_accessor(*args)
106 define_method((arg.to_s + "=").intern) do |other|
107 if !self.running && self.inited
108 abort "God.#{arg} must be set before any Tasks are defined"
111 if self.running && self.inited
112 LOG.log(nil, :warn, "God.#{arg} can't be set while god is running")
116 instance_variable_set(('@' + arg.to_s).intern, other)
119 define_method(arg) do
120 instance_variable_get(('@' + arg.to_s).intern)
129 LOG_BUFFER_SIZE_DEFAULT = 1000
130 PID_FILE_DIRECTORY_DEFAULT = '/var/run/god'
131 DRB_PORT_DEFAULT = 17165
132 DRB_ALLOW_DEFAULT = ['127.0.0.1']
136 safe_attr_accessor :host,
143 attr_accessor :inited,
146 :pending_watch_states,
156 yield self if block_given?
159 def self.internal_init
161 return if self.inited
166 self.pending_watches = []
167 self.pending_watch_states = {}
169 self.contact_groups = {}
172 self.log_buffer_size ||= LOG_BUFFER_SIZE_DEFAULT
173 self.pid_file_directory ||= PID_FILE_DIRECTORY_DEFAULT
174 self.port ||= DRB_PORT_DEFAULT
175 self.allow ||= DRB_ALLOW_DEFAULT
176 LOG.level = Logger::INFO
178 # init has been executed
185 # Instantiate a new, empty Watch object and pass it to the mandatory
186 # block. The attributes of the watch will be set by the configuration
188 def self.watch(&block)
189 self.task(Watch, &block)
192 # Instantiate a new, empty Task object and pass it to the mandatory
193 # block. The attributes of the task will be set by the configuration
195 def self.task(klass = Task)
201 # do the post-configuration
204 # if running, completely remove the watch (if necessary) to
205 # prepare for the reload
206 existing_watch = self.watches[t.name]
207 if self.running && existing_watch
208 self.pending_watch_states[existing_watch.name] = existing_watch.state
209 self.unwatch(existing_watch)
212 # ensure the new watch has a unique name
213 if self.watches[t.name] || self.groups[t.name]
214 abort "Task name '#{t.name}' already used for a Task or Group"
217 # ensure watch is internally valid
218 t.valid? || abort("Task '#{t.name}' is not valid (see above)")
220 # add to list of watches
221 self.watches[t.name] = t
223 # add to pending watches
224 self.pending_watches << t
226 # add to group if specified
228 # ensure group name hasn't been used for a watch already
229 if self.watches[t.group]
230 abort "Group name '#{t.group}' already used for a Task"
233 self.groups[t.group] ||= []
234 self.groups[t.group] << t
241 if self.running && existing_watch
242 LOG.log(t, :info, "#{t.name} Reloaded config")
244 LOG.log(t, :info, "#{t.name} Loaded config")
248 def self.unwatch(watch)
250 watch.unmonitor unless watch.state == :unmonitored
255 # remove from watches
256 self.watches.delete(watch.name)
260 self.groups[watch.group].delete(watch)
264 def self.contact(kind)
267 # create the condition
269 c = Contact.generate(kind)
270 rescue NoSuchContactError => e
274 # send to block so config can set attributes
275 yield(c) if block_given?
277 # call prepare on the contact
280 # ensure the new contact has a unique name
281 if self.contacts[c.name] || self.contact_groups[c.name]
282 abort "Contact name '#{c.name}' already used for a Contact or Contact Group"
285 # abort if the Contact is invalid, the Contact will have printed
286 # out its own error messages by now
287 unless Contact.valid?(c) && c.valid?
288 abort "Exiting on invalid contact"
291 # add to list of contacts
292 self.contacts[c.name] = c
294 # add to contact group if specified
296 # ensure group name hasn't been used for a contact already
297 if self.contacts[c.group]
298 abort "Contact Group name '#{c.group}' already used for a Contact"
301 self.contact_groups[c.group] ||= []
302 self.contact_groups[c.group] << c
306 def self.control(name, command)
307 # get the list of watches
308 watches = Array(self.watches[name] || self.groups[name])
314 when "start", "monitor"
315 watches.each { |w| jobs << Thread.new { w.monitor if w.state != :up } }
317 watches.each { |w| jobs << Thread.new { w.move(:restart) } }
319 watches.each { |w| jobs << Thread.new { w.unmonitor.action(:stop) if w.state != :unmonitored } }
321 watches.each { |w| jobs << Thread.new { w.unmonitor if w.state != :unmonitored } }
323 raise InvalidCommandError.new
326 jobs.each { |j| j.join }
328 watches.map { |x| x.name }
332 self.watches.sort.each do |name, w|
334 w.unmonitor if w.state != :unmonitored
335 w.action(:stop) if w.alive?
340 return true unless self.watches.map { |name, w| w.alive? }.any?
353 self.watches.map do |name, w|
354 info[name] = {:state => w.state}
359 def self.running_log(watch_name, since)
360 unless self.watches[watch_name]
361 raise NoSuchWatchError.new
364 LOG.watch_log_since(watch_name, since)
367 def self.running_load(code, filename)
374 CONFIG_FILE.replace(filename)
375 eval(code, nil, filename)
376 self.pending_watches.each do |w|
377 if previous_state = self.pending_watch_states[w.name]
378 w.monitor unless previous_state == :unmonitored
380 w.monitor if w.autostart?
383 watches = self.pending_watches.dup
384 self.pending_watches.clear
385 self.pending_watch_states.clear
386 rescue Exception => e
387 # don't ever let running_load take down god
388 errors << LOG.finish_capture
390 unless e.instance_of?(SystemExit)
391 errors << e.message << "\n"
392 errors << e.backtrace.join("\n")
396 names = watches.map { |x| x.name }
401 Dir[glob].each do |f|
408 unless test(?d, self.pid_file_directory)
410 FileUtils.mkdir_p(self.pid_file_directory)
411 rescue Errno::EACCES => e
412 abort "Failed to create pid file directory: #{e.message}"
418 unless test(?w, self.pid_file_directory)
419 abort "The pid file directory (#{self.pid_file_directory}) is not writable by #{Etc.getlogin}"
429 self.server = Server.new(self.host, self.port, self.allow)
431 # start event handler system
432 EventHandler.start if EventHandler.loaded?
434 # start the timer system
437 # start monitoring any watches set to autostart
438 self.watches.values.each { |w| w.monitor if w.autostart? }
440 # clear pending watches
441 self.pending_watches.clear
446 # join the timer thread so we don't exit