3 $
:.unshift File
.join(File
.dirname(__FILE__
), *%w
[.. lib
])
9 options
= {:daemonize => true, :port => 17165}
11 OptionParser
.new
do |opts
|
13 Usage: god [command] [options]
16 start <watch or group name>
17 restart <watch or group name>
18 stop <watch or group name>
19 monitor <watch or group name>
20 unmonitor <watch or group name>
29 opts
.on("-cCONFIG", "--config-file CONFIG", "Configuration file") do |x
|
33 opts
.on("-pPORT", "--port PORT", "Communications port") do |x
|
37 opts
.on("-PFILE", "--pid FILE", "Where to write the PID file") do |x
|
41 opts
.on("-lFILE", "--log FILE", "Where to write the log file") do |x
|
45 opts
.on("-D", "--no-daemonize", "Don't daemonize") do
46 options
[:daemonize] = false
49 opts
.on("-v", "--version", "Print the version number and exit") do
50 options
[:version] = true
53 opts
.on("-V", "Print extended version and build information") do
62 puts
"Version #{God::VERSION}"
67 puts
"Version: #{God::VERSION}"
69 puts
"Events: " + God
::EventHandler.event_system
72 elsif command
= ARGV[0]
75 # a command was specified
77 # connect to remote drb
79 server
= DRbObject
.new
nil, "druby://127.0.0.1:#{options[:port]}"
83 rescue DRb
::DRbConnError
84 puts
"The server is not available (or you do not have permissions to access it)"
88 puts e
.backtrace
.join("\n")
95 puts
"Sending '#{command}' command"
97 code
= File
.read(file
)
99 watches
= server
.running_load(code
)
102 puts
'The following watches were affected:'
108 elsif command
== 'status'
109 watches
= server
.status
110 watches
.keys
.sort
.each
do |name
|
111 state
= watches
[name
][:state]
112 puts
"#{name}: #{state}"
114 elsif command
== 'log'
116 Signal
.trap('INT') { exit
!(0) }
120 print server
.running_log(name
, t
)
124 rescue God
::NoSuchWatchError
126 rescue DRb
::DRbConnError
127 puts
"The server went away"
130 puts e
.backtrace
.join("\n")
134 elsif command
== 'terminate'
135 t
= Thread
.new
{ loop { STDOUT.print('.'); STDOUT.flush
; sleep(1) } }
138 puts
'Stopped all watches'
141 puts
'Could not stop all watches within 10 seconds'
146 abort
'Could not stop god'
147 rescue DRb
::DRbConnError
152 # get the name of the watch/group
156 puts
"Sending '#{command}' command"
158 t
= Thread
.new
{ loop { STDOUT.print('.'); STDOUT.flush
; sleep(1) } }
161 watches
= server
.control(name
, command
)
165 puts
'The following watches were affected:'
169 rescue God
::InvalidCommandError
170 abort
"Command '#{command}' is not valid. Run 'god --help' for usage"
177 if !options
[:daemonize]
181 God
.port
= options
[:port]
184 load File
.expand_path(options
[:config])
190 log_file
= options
[:log] || "/dev/null"
192 STDIN.reopen
"/dev/null"
193 STDOUT.reopen(log_file
, "a")
198 unless God
::EventHandler.loaded
?
200 puts
"***********************************************************************"
202 puts
"* Event conditions are not available for your installation of god."
203 puts
"* You may still use and write custom conditions using the poll system"
205 puts
"***********************************************************************"
209 puts
"Resetting file descriptors"
211 puts
"Loading config"
214 God
.port
= options
[:port]
217 load File
.expand_path(options
[:config])
219 Signal
.trap('HUP') {}
221 File
.open('god.log', 'a') { |f
| f
.puts e
.message
+ "\n" + e
.backtrace
}
222 abort
"!!! ERROR - See god.log !!!"
227 File
.open(options
[:pid], 'w') { |f
| f
.write pid
}