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])
62 God::EventHandler.load
65 # Override abort to exit without executing the at_exit hook
76 LOG.datetime_format = "%Y-%m-%d %H:%M:%S "
78 LOG_BUFFER_SIZE_DEFAULT = 1000
79 PID_FILE_DIRECTORY_DEFAULT = '/var/run/god'
80 DRB_PORT_DEFAULT = 17165
81 DRB_ALLOW_DEFAULT = ['127.0.0.1']
92 attr_accessor :inited,
104 abort "God.init must be called before any Watches"
109 # yield to the config file
110 yield self if block_given?
113 def self.internal_init
115 return if self.inited
120 self.pending_watches = []
122 self.contact_groups = {}
125 self.log_buffer_size ||= LOG_BUFFER_SIZE_DEFAULT
126 self.pid_file_directory ||= PID_FILE_DIRECTORY_DEFAULT
127 self.port ||= DRB_PORT_DEFAULT
128 self.allow ||= DRB_ALLOW_DEFAULT
129 LOG.level = Logger::INFO
131 # init has been executed
138 # Instantiate a new, empty Watch object and pass it to the mandatory
139 # block. The attributes of the watch will be set by the configuration
141 def self.watch(&block)
142 self.task(Watch, &block)
145 # Instantiate a new, empty Task object and pass it to the mandatory
146 # block. The attributes of the task will be set by the configuration
148 def self.task(klass = Task)
154 # do the post-configuration
157 # if running, completely remove the watch (if necessary) to
158 # prepare for the reload
159 existing_watch = self.watches[t.name]
160 if self.running && existing_watch
161 self.unwatch(existing_watch)
164 # ensure the new watch has a unique name
165 if self.watches[t.name] || self.groups[t.name]
166 abort "Task name '#{t.name}' already used for a Task or Group"
169 # ensure watch is internally valid
170 t.valid? || abort("Task '#{t.name}' is not valid (see above)")
172 # add to list of watches
173 self.watches[t.name] = t
175 # add to pending watches
176 self.pending_watches << t
178 # add to group if specified
180 # ensure group name hasn't been used for a watch already
181 if self.watches[t.group]
182 abort "Group name '#{t.group}' already used for a Task"
185 self.groups[t.group] ||= []
186 self.groups[t.group] << t
193 def self.unwatch(watch)
200 # remove from watches
201 self.watches.delete(watch.name)
205 self.groups[watch.group].delete(watch)
209 def self.contact(kind)
212 # create the condition
214 c = Contact.generate(kind)
215 rescue NoSuchContactError => e
219 # send to block so config can set attributes
220 yield(c) if block_given?
222 # call prepare on the contact
225 # ensure the new contact has a unique name
226 if self.contacts[c.name] || self.contact_groups[c.name]
227 abort "Contact name '#{c.name}' already used for a Contact or Contact Group"
230 # abort if the Contact is invalid, the Contact will have printed
231 # out its own error messages by now
232 unless Contact.valid?(c) && c.valid?
233 abort "Exiting on invalid contact"
236 # add to list of contacts
237 self.contacts[c.name] = c
239 # add to contact group if specified
241 # ensure group name hasn't been used for a contact already
242 if self.contacts[c.group]
243 abort "Contact Group name '#{c.group}' already used for a Contact"
246 self.contact_groups[c.group] ||= []
247 self.contact_groups[c.group] << c
251 def self.control(name, command)
252 # get the list of watches
253 watches = Array(self.watches[name] || self.groups[name])
259 when "start", "monitor"
260 watches.each { |w| jobs << Thread.new { w.monitor } }
262 watches.each { |w| jobs << Thread.new { w.move(:restart) } }
264 watches.each { |w| jobs << Thread.new { w.unmonitor.action(:stop) } }
266 watches.each { |w| jobs << Thread.new { w.unmonitor } }
268 raise InvalidCommandError.new
271 jobs.each { |j| j.join }
277 self.watches.sort.each do |name, w|
279 w.unmonitor if w.state
280 w.action(:stop) if w.alive?
285 return true unless self.watches.map { |name, w| w.alive? }.any?
298 self.watches.map do |name, w|
299 info[name] = {:state => w.state}
304 def self.running_log(watch_name, since)
305 unless self.watches[watch_name]
306 raise NoSuchWatchError.new
309 LOG.watch_log_since(watch_name, since)
312 def self.running_load(code)
314 self.pending_watches.each { |w| w.monitor if w.autostart? }
315 watches = self.pending_watches.dup
316 self.pending_watches.clear
321 Dir[glob].each do |f|
328 unless test(?d, self.pid_file_directory)
330 FileUtils.mkdir_p(self.pid_file_directory)
331 rescue Errno::EACCES => e
332 abort "Failed to create pid file directory: #{e.message}"
338 unless test(?w, self.pid_file_directory)
339 abort "The pid file directory (#{self.pid_file_directory}) is not writable by #{Etc.getlogin}"
349 self.server = Server.new(self.host, self.port, self.allow)
351 # start event handler system
352 EventHandler.start if EventHandler.loaded?
354 # start the timer system
357 # start monitoring any watches set to autostart
358 self.watches.values.each { |w| w.monitor if w.autostart? }
360 # clear pending watches
361 self.pending_watches.clear
366 # join the timer thread so we don't exit