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 stop <watch or group name>
18 monitor <watch or group name>
19 unmonitor <watch or group name>
28 opts
.on("-cCONFIG", "--config-file CONFIG", "Configuration file") do |x
|
32 opts
.on("-pPORT", "--port PORT", "Communications port") do |x
|
36 opts
.on("-PFILE", "--pid FILE", "Where to write the PID file") do |x
|
40 opts
.on("-lFILE", "--log FILE", "Where to write the log file") do |x
|
44 opts
.on("-D", "--no-daemonize", "Don't daemonize") do
45 options
[:daemonize] = false
48 opts
.on("-v", "--version", "Print the version number and exit") do
49 options
[:version] = true
52 opts
.on("-V", "Print extended version and build information") do
61 puts
"Version #{God::VERSION}"
66 puts
"Version: #{God::VERSION}"
68 puts
"Events: " + God
::EventHandler.event_system
71 elsif command
= ARGV[0]
74 # a command was specified
76 # connect to remote drb
78 server
= DRbObject
.new
nil, "druby://localhost:#{options[:port]}"
83 puts
"Sending '#{command}' command"
85 code
= File
.read(file
)
87 watches
= server
.running_load(code
)
90 puts
'The following watches were affected:'
96 elsif command
== 'status'
98 elsif command
== 'log'
100 Signal
.trap('INT') { exit
!(0) }
104 print server
.running_log(name
, t
)
108 rescue God
::NoSuchWatchError
110 rescue DRb
::DRbConnError
111 puts
"The server went away"
114 puts e
.backtrace
.join("\n")
118 elsif command
== 'terminate'
120 puts
'Stopped all watches'
122 puts
'Could not stop all watches within 10 seconds'
127 abort
'Could not stop god'
128 rescue DRb
::DRbConnError
133 # get the name of the watch/group
137 puts
"Sending '#{command}' command"
140 watches
= server
.control(name
, command
)
143 puts
'The following watches were affected:'
147 rescue God
::InvalidCommandError
148 abort
"Command '#{command}' is not valid. Run 'god --help' for usage"
155 if !options
[:daemonize]
159 God
.port
= options
[:port]
162 load File
.expand_path(options
[:config])
168 log_file
= options
[:log] || "/dev/null"
170 STDIN.reopen
"/dev/null"
171 STDOUT.reopen(log_file
, "a")
176 unless God
::EventHandler.loaded
?
178 puts
"***********************************************************************"
180 puts
"* Event conditions are not available for your installation of god."
181 puts
"* You may still use and write custom conditions using the poll system"
183 puts
"***********************************************************************"
187 puts
"Resetting file descriptors"
189 puts
"Loading config"
192 God
.port
= options
[:port]
195 load File
.expand_path(options
[:config])
197 Signal
.trap('HUP') {}
199 File
.open('god.log', 'a') { |f
| f
.puts e
.message
+ "\n" + e
.backtrace
}
200 abort
"!!! ERROR - See god.log !!!"
205 File
.open(options
[:pid], 'w') { |f
| f
.write pid
}