1 $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
8 require 'god/system/process'
10 require 'god/behavior'
11 require 'god/behaviors/clean_pid_file'
12 require 'god/behaviors/notify_when_flapping'
14 require 'god/condition'
15 require 'god/conditions/timeline'
16 require 'god/conditions/process_running'
17 require 'god/conditions/process_exits'
18 require 'god/conditions/tries'
19 require 'god/conditions/memory_usage'
20 require 'god/conditions/cpu_usage'
21 require 'god/conditions/always'
23 require 'god/reporter'
31 require 'god/event_handler'
32 require 'god/registry'
37 $:.unshift File.join(File.dirname(__FILE__), *%w[.. ext god])
45 God::EventHandler.load
51 attr_accessor :inited, :running, :pending_watches, :host, :port
57 attr_accessor :watches, :groups
67 self.pending_watches = []
69 # yield to the config file
70 yield self if block_given?
73 self.server = Server.new(self.host, self.port)
75 # init has been executed
82 # Where pid files created by god will go by default
83 def self.pid_file_directory
84 @pid_file_directory ||= '/var/run/god'
87 def self.pid_file_directory=(value)
88 @pid_file_directory = value
91 # Instantiate a new, empty Watch object and pass it to the mandatory
92 # block. The attributes of the watch will be set by the configuration
100 # if running, completely remove the watch (if necessary) to
101 # prepare for the reload
102 existing_watch = self.watches[w.name]
103 if self.running && existing_watch
104 self.unwatch(existing_watch)
107 # ensure the new watch has a unique name
108 if self.watches[w.name] || self.groups[w.name]
109 abort "Watch name '#{w.name}' already used for a Watch or Group"
112 # add to list of watches
113 self.watches[w.name] = w
115 # add to pending watches
116 self.pending_watches << w
118 # add to group if specified
120 # ensure group name hasn't been used for a watch already
121 if self.watches[w.group]
122 abort "Group name '#{w.group}' already used for a Watch"
125 self.groups[w.group] ||= []
126 self.groups[w.group] << w
133 def self.unwatch(watch)
140 # remove from watches
141 self.watches.delete(watch.name)
145 self.groups[watch.group].delete(watch)
149 def self.control(name, command)
150 # get the list of watches
151 watches = Array(self.watches[name] || self.groups[name])
155 when "start", "monitor"
156 watches.each { |w| w.monitor }
158 watches.each { |w| w.move(:restart) }
160 watches.each { |w| w.unmonitor.action(:stop) }
162 watches.each { |w| w.unmonitor }
164 raise InvalidCommandError.new
170 def self.running_load(code)
172 self.pending_watches.each { |w| w.monitor if w.autostart? }
173 watches = self.pending_watches.dup
174 self.pending_watches.clear
179 Dir[glob].each do |f|
185 # make sure there's something to do
186 if self.watches.nil? || self.watches.empty?
187 abort "You must specify at least one watch!"
190 # start event handler system
191 EventHandler.start if EventHandler.loaded?
193 # start the timer system
196 # start monitoring any watches set to autostart
197 self.watches.values.each { |w| w.monitor if w.autostart? }
199 # clear pending watches
200 self.pending_watches.clear
205 # join the timer thread so we don't exit