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]}"
82 rescue DRb
::DRbConnError
83 puts
"The server is not available (or you do not have permissions to access it)"
90 puts
"Sending '#{command}' command"
92 code
= File
.read(file
)
94 watches
= server
.running_load(code
)
97 puts
'The following watches were affected:'
103 elsif command
== 'status'
105 elsif command
== 'log'
107 Signal
.trap('INT') { exit
!(0) }
111 print server
.running_log(name
, t
)
115 rescue God
::NoSuchWatchError
117 rescue DRb
::DRbConnError
118 puts
"The server went away"
121 puts e
.backtrace
.join("\n")
125 elsif command
== 'terminate'
127 puts
'Stopped all watches'
129 puts
'Could not stop all watches within 10 seconds'
134 abort
'Could not stop god'
135 rescue DRb
::DRbConnError
140 # get the name of the watch/group
144 puts
"Sending '#{command}' command"
147 watches
= server
.control(name
, command
)
150 puts
'The following watches were affected:'
154 rescue God
::InvalidCommandError
155 abort
"Command '#{command}' is not valid. Run 'god --help' for usage"
162 if !options
[:daemonize]
166 God
.port
= options
[:port]
169 load File
.expand_path(options
[:config])
175 log_file
= options
[:log] || "/dev/null"
177 STDIN.reopen
"/dev/null"
178 STDOUT.reopen(log_file
, "a")
183 unless God
::EventHandler.loaded
?
185 puts
"***********************************************************************"
187 puts
"* Event conditions are not available for your installation of god."
188 puts
"* You may still use and write custom conditions using the poll system"
190 puts
"***********************************************************************"
194 puts
"Resetting file descriptors"
196 puts
"Loading config"
199 God
.port
= options
[:port]
202 load File
.expand_path(options
[:config])
204 Signal
.trap('HUP') {}
206 File
.open('god.log', 'a') { |f
| f
.puts e
.message
+ "\n" + e
.backtrace
}
207 abort
"!!! ERROR - See god.log !!!"
212 File
.open(options
[:pid], 'w') { |f
| f
.write pid
}