File: process_running.rb

package info (click to toggle)
ruby-god 0.13.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 832 kB
  • sloc: ruby: 6,641; ansic: 237; makefile: 3
file content (63 lines) | stat: -rw-r--r-- 1,838 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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"])
          end
          false
        end
      end
    end
  end
end