prevent carry-over conditions
[god.git] / lib / god / conditions / process_exits.rb
bloba48cb5e97e72d0dbd8831c3bb1a5077ab2ba072a
1 module God
2   module Conditions
3     
4     # Condition Symbol :process_exits
5     # Type: Event
6     # 
7     # Trigger when a process exits.
8     #
9     # Paramaters
10     #   Required
11     #     +pid_file+ is the pid file of the process in question. Automatically
12     #                populated for Watches.
13     #
14     # Examples
15     #
16     # Trigger if process exits (from a Watch):
17     #
18     #   on.condition(:process_exits)
19     #
20     # Trigger if process exits:
21     #
22     #   on.condition(:process_exits) do |c|
23     #     c.pid_file = "/var/run/mongrel.3000.pid"
24     #   end
25     class ProcessExits < EventCondition
26       def initialize
27         self.info = "process exited"
28       end
29       
30       def valid?
31         true
32       end
33       
34       def register
35         pid = self.watch.pid
36         
37         begin
38           EventHandler.register(pid, :proc_exit) do |extra|
39             formatted_extra = extra.size > 0 ? " #{extra.inspect}" : ""
40             self.info = "process #{pid} exited#{formatted_extra}"
41             Hub.trigger(self)
42           end
43           
44           msg = "#{self.watch.name} registered 'proc_exit' event for pid #{pid}"
45           applog(self.watch, :info, msg)
46         rescue StandardError
47           raise EventRegistrationFailedError.new
48         end
49       end
50       
51       def deregister
52         pid = self.watch.pid
53         if pid
54           EventHandler.deregister(pid, :proc_exit)
55           
56           msg = "#{self.watch.name} deregistered 'proc_exit' event for pid #{pid}"
57           applog(self.watch, :info, msg)
58         else
59           applog(self.watch, :error, "#{self.watch.name} could not deregister: no cached PID or PID file #{self.watch.pid_file} (#{self.base_name})")
60         end
61       end
62     end
63     
64   end
65 end