1 $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
8 require 'god/system/process'
10 require 'god/dependency_graph'
12 require 'god/behavior'
13 require 'god/behaviors/clean_pid_file'
14 require 'god/behaviors/notify_when_flapping'
16 require 'god/condition'
17 require 'god/conditions/timeline'
18 require 'god/conditions/process_running'
19 require 'god/conditions/process_exits'
20 require 'god/conditions/tries'
21 require 'god/conditions/memory_usage'
22 require 'god/conditions/cpu_usage'
23 require 'god/conditions/always'
24 require 'god/conditions/lambda'
25 require 'god/conditions/degrading_lambda'
27 require 'god/reporter'
35 require 'god/event_handler'
36 require 'god/registry'
41 $:.unshift File.join(File.dirname(__FILE__), *%w[.. ext god])
49 God::EventHandler.load
55 attr_accessor :inited, :running, :pending_watches, :host, :port
61 attr_accessor :watches, :groups
71 self.pending_watches = []
73 # yield to the config file
74 yield self if block_given?
77 self.server = Server.new(self.host, self.port)
79 # init has been executed
86 # Where pid files created by god will go by default
87 def self.pid_file_directory
88 @pid_file_directory ||= '/var/run/god'
91 def self.pid_file_directory=(value)
92 @pid_file_directory = value
95 # Instantiate a new, empty Watch object and pass it to the mandatory
96 # block. The attributes of the watch will be set by the configuration
104 # if running, completely remove the watch (if necessary) to
105 # prepare for the reload
106 existing_watch = self.watches[w.name]
107 if self.running && existing_watch
108 self.unwatch(existing_watch)
111 # ensure the new watch has a unique name
112 if self.watches[w.name] || self.groups[w.name]
113 abort "Watch name '#{w.name}' already used for a Watch or Group"
116 # add to list of watches
117 self.watches[w.name] = w
119 # add to pending watches
120 self.pending_watches << w
122 # add to group if specified
124 # ensure group name hasn't been used for a watch already
125 if self.watches[w.group]
126 abort "Group name '#{w.group}' already used for a Watch"
129 self.groups[w.group] ||= []
130 self.groups[w.group] << w
137 def self.unwatch(watch)
144 # remove from watches
145 self.watches.delete(watch.name)
149 self.groups[watch.group].delete(watch)
153 def self.control(name, command)
154 # get the list of watches
155 watches = Array(self.watches[name] || self.groups[name])
159 when "start", "monitor"
160 watches.each { |w| w.monitor }
162 watches.each { |w| w.move(:restart) }
164 watches.each { |w| w.unmonitor.action(:stop) }
166 watches.each { |w| w.unmonitor }
168 raise InvalidCommandError.new
174 def self.running_load(code)
176 self.pending_watches.each { |w| w.monitor if w.autostart? }
177 watches = self.pending_watches.dup
178 self.pending_watches.clear
183 Dir[glob].each do |f|
189 # make sure there's something to do
190 if self.watches.nil? || self.watches.empty?
191 abort "You must specify at least one watch!"
194 # start event handler system
195 EventHandler.start if EventHandler.loaded?
197 # start the timer system
200 # start monitoring any watches set to autostart
201 self.watches.values.each { |w| w.monitor if w.autostart? }
203 # clear pending watches
204 self.pending_watches.clear
209 # join the timer thread so we don't exit