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://127.0.0.1:#{options[:port]}"
82 rescue DRb
::DRbConnError
83 puts
"The server is not available (or you do not have permissions to access it)"
87 puts e
.backtrace
.join("\n")
94 puts
"Sending '#{command}' command"
96 code
= File
.read(file
)
98 watches
= server
.running_load(code
)
101 puts
'The following watches were affected:'
107 elsif command
== 'status'
109 elsif command
== 'log'
111 Signal
.trap('INT') { exit
!(0) }
115 print server
.running_log(name
, t
)
119 rescue God
::NoSuchWatchError
121 rescue DRb
::DRbConnError
122 puts
"The server went away"
125 puts e
.backtrace
.join("\n")
129 elsif command
== 'terminate'
131 puts
'Stopped all watches'
133 puts
'Could not stop all watches within 10 seconds'
138 abort
'Could not stop god'
139 rescue DRb
::DRbConnError
144 # get the name of the watch/group
148 puts
"Sending '#{command}' command"
151 watches
= server
.control(name
, command
)
154 puts
'The following watches were affected:'
158 rescue God
::InvalidCommandError
159 abort
"Command '#{command}' is not valid. Run 'god --help' for usage"
166 if !options
[:daemonize]
170 God
.port
= options
[:port]
173 load File
.expand_path(options
[:config])
179 log_file
= options
[:log] || "/dev/null"
181 STDIN.reopen
"/dev/null"
182 STDOUT.reopen(log_file
, "a")
187 unless God
::EventHandler.loaded
?
189 puts
"***********************************************************************"
191 puts
"* Event conditions are not available for your installation of god."
192 puts
"* You may still use and write custom conditions using the poll system"
194 puts
"***********************************************************************"
198 puts
"Resetting file descriptors"
200 puts
"Loading config"
203 God
.port
= options
[:port]
206 load File
.expand_path(options
[:config])
208 Signal
.trap('HUP') {}
210 File
.open('god.log', 'a') { |f
| f
.puts e
.message
+ "\n" + e
.backtrace
}
211 abort
"!!! ERROR - See god.log !!!"
216 File
.open(options
[:pid], 'w') { |f
| f
.write pid
}