3 $
:.unshift File
.join(File
.dirname(__FILE__
), *%w
[.. lib
])
10 options
= {:daemonize => true, :port => 17165}
12 opts
= OptionParser
.new
do |opts
|
16 god -c <config file> [-p <port> | -b] [-P <file>] [-l <file>] [-D]
19 god <command> <argument> [-p <port>]
20 god <command> [-p <port>]
22 god -V (must be run as root to be accurate on Linux)
25 start <task or group name> start task or group
26 restart <task or group name> restart task or group
27 stop <task or group name> stop task or group
28 monitor <task or group name> monitor task or group
29 unmonitor <task or group name> unmonitor task or group
30 load <file> load a config into a running god
31 log <task name> show realtime log for given task
32 status show status of each task
34 terminate stop god and all tasks
39 opts
.on("-cCONFIG", "--config-file CONFIG", "Configuration file") do |x
|
43 opts
.on("-pPORT", "--port PORT", "Communications port (default 17165)") do |x
|
47 opts
.on("-b", "--auto-bind", "Auto-bind to an unused port number") do
51 opts
.on("-PFILE", "--pid FILE", "Where to write the PID file") do |x
|
55 opts
.on("-lFILE", "--log FILE", "Where to write the log file") do |x
|
59 opts
.on("-D", "--no-daemonize", "Don't daemonize") do
60 options
[:daemonize] = false
63 opts
.on("-v", "--version", "Print the version number and exit") do
64 options
[:version] = true
67 opts
.on("-V", "Print extended version and build information") do
78 puts
"Version #{God::VERSION}"
83 puts
"Version: #{God::VERSION}"
85 puts
"Events: " + God
::EventHandler.event_system
88 elsif command
= ARGV[0]
91 # a command was specified
93 # connect to remote drb
95 server
= DRbObject
.new
nil, "druby://127.0.0.1:#{options[:port]}"
99 rescue DRb
::DRbConnError
100 puts
"The server is not available (or you do not have permissions to access it)"
104 puts e
.backtrace
.join("\n")
111 puts
"Sending '#{command}' command"
113 unless File
.exist
?(file
)
114 abort
"File not found: #{file}"
117 watches
= server
.running_load(File
.read(file
), File
.expand_path(file
))
120 puts
'The following watches were affected:'
124 elsif command
== 'status'
126 watches
= server
.status
127 watches
.keys
.sort
.each
do |name
|
128 state
= watches
[name
][:state]
129 puts
"#{name}: #{state}"
133 puts e
.backtrace
.join("\n")
135 elsif command
== 'log'
137 Signal
.trap('INT') { exit
!(0) }
141 print server
.running_log(name
, t
)
145 rescue God
::NoSuchWatchError
147 rescue DRb
::DRbConnError
148 puts
"The server went away"
151 puts e
.backtrace
.join("\n")
153 elsif command
== 'quit'
156 abort
'Could not stop god'
157 rescue DRb
::DRbConnError
160 elsif command
== 'terminate'
162 t
= Thread
.new
{ loop { STDOUT.print('.'); STDOUT.flush
; sleep(1) } }
165 puts
'Stopped all watches'
168 puts
'Could not stop all watches within 10 seconds'
172 puts e
.backtrace
.join("\n")
177 abort
'Could not stop god'
178 rescue DRb
::DRbConnError
182 # get the name of the watch/group
186 puts
"Sending '#{command}' command"
188 t
= Thread
.new
{ loop { sleep(1); STDOUT.print('.'); STDOUT.flush
; sleep(1) } }
191 watches
= server
.control(name
, command
)
195 puts
'The following watches were affected:'
199 rescue God
::InvalidCommandError
200 t
.kill
rescue nil; STDOUT.puts
201 abort
"Command '#{command}' is not valid. Run 'god --help' for usage"
204 puts e
.backtrace
.join("\n")
211 if !options
[:daemonize]
215 God
.port
= options
[:port]
219 unless File
.exist
?(options
[:config])
220 abort
"File not found: #{options[:config]}"
224 load File
.expand_path(options
[:config])
227 puts e
.backtrace
.join("\n")
228 abort
"There was an error in your configuration file (see above)"
232 # trap and ignore SIGHUP
233 Signal
.trap('HUP') {}
239 log_file
= options
[:log] || "/dev/null"
241 unless God
::EventHandler.loaded
?
243 puts
"***********************************************************************"
245 puts
"* Event conditions are not available for your installation of god."
246 puts
"* You may still use and write custom conditions using the poll system"
248 puts
"***********************************************************************"
252 # set port if requested
254 God
.port
= options
[:port]
259 unless File
.exist
?(options
[:config])
260 abort
"File not found: #{options[:config]}"
264 load File
.expand_path(options
[:config])
267 puts e
.backtrace
.join("\n")
268 abort
"There was an error in your configuration file (see above)"
272 # reset file descriptors
273 STDIN.reopen
"/dev/null"
274 STDOUT.reopen(log_file
, "a")
278 puts e
.backtrace
.join("\n")
279 abort
"There was a fatal system error while starting god (see above)"
284 File
.open(options
[:pid], 'w') { |f
| f
.write pid
}
294 rescue Exception
=> e
296 puts e
.backtrace
.join("\n")