Sha256: 73f8fab2afddb11b9d2242774c45d814f9e7ef40e20b6b4eb5a39258f8a362ed

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

module God
  module Conditions
    # Trigger when a process is running or not running depending on attributes.
    #
    # Examples
    #
    #   # Trigger if process IS NOT running.
    #   on.condition(:process_running) do |c|
    #     c.running = false
    #   end
    #
    #   # Trigger if process IS running.
    #   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
      # Public: The Boolean specifying whether you want to trigger if the
      # process is running (true) or if it is not running (false).
      attr_accessor :running

      # Public: The String PID file location of the process in question.
      # Automatically populated for Watches.
      attr_accessor :pid_file

      def pid
        self.pid_file ? File.read(self.pid_file).strip.to_i : self.watch.pid
      end

      def valid?
        valid = true
        valid &= complain("Attribute 'pid_file' must be specified", self) if self.pid_file.nil? && self.watch.pid_file.nil?
        valid &= complain("Attribute 'running' must be specified", self) if self.running.nil?
        valid
      end

      def test
        self.info = []

        pid = self.pid
        active = pid && System::Process.new(pid).exists?

        if (self.running && active)
          self.info.concat(["process is running"])
          true
        elsif (!self.running && !active)
          self.info.concat(["process is not running"])
          true
        else
          if self.running
            self.info.concat(["process is not running"])
          else
            self.info.concat(["process is running"])
          end
          false
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
god-0.12.1 lib/god/conditions/process_running.rb
god-0.12.0 lib/god/conditions/process_running.rb