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'
13 require 'god/condition'
14 require 'god/conditions/timeline'
15 require 'god/conditions/process_running'
16 require 'god/conditions/process_exits'
17 require 'god/conditions/tries'
18 require 'god/conditions/memory_usage'
19 require 'god/conditions/cpu_usage'
20 require 'god/conditions/always'
22 require 'god/reporter'
30 require 'god/event_handler'
31 require 'god/registry'
36 $:.unshift File.join(File.dirname(__FILE__), *%w[.. ext god])
44 God::EventHandler.load
50 attr_accessor :inited, :running, :host, :port
56 attr_accessor :watches, :groups
67 # yield to the config file
68 yield self if block_given?
71 self.server = Server.new(self.host, self.port)
73 # init has been executed
80 # Where pid files created by god will go by default
81 def self.pid_file_directory
82 @pid_file_directory ||= '/var/run/god'
85 def self.pid_file_directory=(value)
86 @pid_file_directory = value
89 # Instantiate a new, empty Watch object and pass it to the mandatory
90 # block. The attributes of the watch will be set by the configuration
98 # if running, completely remove the watch (if necessary) to
99 # prepare for the reload
100 existing_watch = self.watches[w.name]
101 if self.running && existing_watch
102 self.unwatch(existing_watch)
105 # ensure the new watch has a unique name
106 if self.watches[w.name] || self.groups[w.name]
107 abort "Watch name '#{w.name}' already used for a Watch or Group"
110 # add to list of watches
111 self.watches[w.name] = w
113 # add to group if specified
115 # ensure group name hasn't been used for a watch already
116 if self.watches[w.group]
117 abort "Group name '#{w.group}' already used for a Watch"
120 self.groups[w.group] ||= []
121 self.groups[w.group] << w
128 def self.unwatch(watch)
132 # remove from watches
133 self.watches.delete(watch.name)
137 self.groups[watch.group].delete(watch)
141 def self.control(name, command)
142 # get the list of watches
143 watches = Array(self.watches[name] || self.groups[name])
147 when "start", "monitor"
148 watches.each { |w| w.monitor }
150 watches.each { |w| w.move(:restart) }
152 watches.each { |w| w.unmonitor.action(:stop) }
154 watches.each { |w| w.unmonitor }
156 raise InvalidCommandError.new
163 # make sure there's something to do
164 if self.watches.nil? || self.watches.empty?
165 abort "You must specify at least one watch!"
168 # start event handler system
169 EventHandler.start if EventHandler.loaded?
171 # start the timer system
174 # start monitoring any watches set to autostart
175 self.watches.values.each { |w| w.monitor if w.autostart? }
180 # join the timer thread so we don't exit
189 Dir[glob].each do |f|