Sha256: 66abb2768d04d86036de3c096249b23d940e99f3fee596f1765922a7784cf63c

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

module God
  module Conditions
    
    # Condition Symbol :process_exits
    # Type: Event
    # 
    # Trigger when a process exits.
    #
    # Paramaters
    #   Required
    #     +pid_file+ is the pid file of the process in question. Automatically
    #                populated for Watches.
    #
    # Examples
    #
    # Trigger if process exits (from a Watch):
    #
    #   on.condition(:process_exits)
    #
    # Trigger if process exits:
    #
    #   on.condition(:process_exits) do |c|
    #     c.pid_file = "/var/run/mongrel.3000.pid"
    #   end
    class ProcessExits < EventCondition
      def initialize
        self.info = "process exited"
      end
      
      def valid?
        valid = true
        valid &= complain("Attribute 'pid_file' must be specified", self) if self.watch.pid_file.nil?
        valid
      end
    
      def register
        pid = File.read(self.watch.pid_file).strip.to_i
        
        begin
          EventHandler.register(pid, :proc_exit) do |extra|
            self.info = "process exited #{extra.inspect}"
            Hub.trigger(self)
          end
        rescue StandardError
          raise EventRegistrationFailedError.new
        end
      end
      
      def deregister
        if File.exist?(self.watch.pid_file)
          pid = File.read(self.watch.pid_file).strip.to_i
          EventHandler.deregister(pid, :proc_exit)
        else
          applog(self.watch, :error, "#{self.watch.name} could not deregister: no such PID file #{self.watch.pid_file} (#{self.base_name})")
        end
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
god-0.6.0 lib/god/conditions/process_exits.rb