module God module Conditions # Condition Symbol :process_running # Type: Poll # # Trigger when a process is running or not running depending on attributes. # # Paramaters # Required # +pid_file+ is the pid file of the process in question. Automatically # populated for Watches. # +running" specifies whether you want to trigger if the process is # running (true) or whether it is not running (false) # # Examples # # Trigger if process IS NOT running (from a Watch): # # on.condition(:process_running) do |c| # c.running = false # end # # Trigger if process IS running (from a Watch): # # on.condition(:process_running) do |c| # c.running = true # end # # Non-Watch Tasks must specify a PID file: # # on.condition(:process_running) do |c| # c.running = false # c.pid_file = "/var/run/mongrel.3000.pid" # end class ProcessRunning < PollCondition attr_accessor :running def valid? valid = true valid &= complain("Attribute 'pid_file' must be specified", self) if self.watch.pid_file.nil? valid &= complain("Attribute 'running' must be specified", self) if self.running.nil? valid end def test self.info = [] unless File.exist?(self.watch.pid_file) self.info << "#{self.watch.name} #{self.class.name}: no such pid file: #{self.watch.pid_file}" return !self.running end pid = File.read(self.watch.pid_file).strip active = System::Process.new(pid).exists? if (self.running && active) self.info << "process is running" true elsif (!self.running && !active) self.info << "process is not running" true else if self.running self.info << "process is not running" else self.info << "process is running" end false end end end end end