fork/exec string command for self-daemonizing process, but wait for process to exit
[god.git] / lib / god / conditions / process_exits.rb
blobef361a4aabe10f19e54570477fe7a66c3e035b03
1 module God
2   module Conditions
3     
4     class ProcessExits < EventCondition
5       def initialize
6         self.info = "process exited"
7       end
8       
9       def valid?
10         valid = true
11         valid &= complain("Attribute 'pid_file' must be specified", self) if self.watch.pid_file.nil?
12         valid
13       end
14     
15       def register
16         pid = File.read(self.watch.pid_file).strip.to_i
17         
18         begin
19           EventHandler.register(pid, :proc_exit) do
20             Hub.trigger(self)
21           end
22         rescue StandardError
23           raise EventRegistrationFailedError.new
24         end
25       end
26       
27       def deregister
28         if File.exist?(self.watch.pid_file)
29           pid = File.read(self.watch.pid_file).strip.to_i
30           EventHandler.deregister(pid, :proc_exit)
31         else
32           LOG.log(self.watch, :error, "#{self.watch.name} could not deregister: no such PID file #{self.watch.pid_file} (#{self.base_name})")
33         end
34       end
35     end
36     
37   end
38 end