5 def initialize(command, options, args)
14 # connect to drb unix socket
16 @server = DRbObject.new(nil, God::Socket.socket(@options[:port]))
18 # ping server to ensure that it is responsive
21 rescue DRb::DRbConnError
22 puts "The server is not available (or you do not have permissions to access it)"
28 if %w{load status log quit terminate}.include?(@command)
30 send("#{@command}_command")
31 elsif %w{start stop restart monitor unmonitor remove}.include?(@command)
34 elsif @command == 'check'
37 puts "Command '#{@command}' is not valid. Run 'god --help' for usage"
45 puts "Sending '#{@command}' command"
48 unless File.exist?(file)
49 abort "File not found: #{file}"
52 names, errors = *@server.running_load(File.read(file), File.expand_path(file))
56 puts 'The following tasks were affected:'
69 watches = @server.status
70 watches.keys.sort.each do |name|
71 state = watches[name][:state]
72 puts "#{name}: #{state}"
78 Signal.trap('INT') { exit }
82 puts "You must specify a Task or Group name"
88 print @server.running_log(name, t)
92 rescue God::NoSuchWatchError
94 rescue DRb::DRbConnError
95 puts "The server went away"
102 abort 'Could not stop god'
103 rescue DRb::DRbConnError
108 def terminate_command
109 t = Thread.new { loop { STDOUT.print('.'); STDOUT.flush; sleep(1) } }
112 puts 'Stopped all watches'
115 puts 'Could not stop all watches within 10 seconds'
120 abort 'Could not stop god'
121 rescue DRb::DRbConnError
129 event_system = God::EventHandler.event_system
130 puts "using event system: #{event_system}"
132 if God::EventHandler.loaded?
133 puts "starting event handler"
134 God::EventHandler.start
136 puts "[fail] event system did not load"
140 puts 'forking off new process'
146 puts "forked process with pid = #{pid}"
148 God::EventHandler.register(pid, :proc_exit) do
149 puts "[ok] process exit event received"
155 puts "killing process"
157 ::Process.kill('KILL', pid)
160 puts e.backtrace.join("\n")
166 puts "[fail] never received process exit event"
170 def lifecycle_command
171 # get the name of the watch/group
174 puts "Sending '#{@command}' command"
176 t = Thread.new { loop { sleep(1); STDOUT.print('.'); STDOUT.flush; sleep(1) } }
179 watches = @server.control(name, @command)
183 unless watches.empty?
184 puts 'The following watches were affected:'
189 puts 'No matching task or group'