Class: God::Conditions::ProcessRunning

Inherits:
PollCondition show all
Defined in:
lib/god/conditions/process_running.rb

Overview

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

Instance Attribute Summary collapse

Attributes inherited from PollCondition

#interval

Attributes inherited from God::Condition

#info, #notify, #phase, #transition

Attributes inherited from Behavior

#watch

Instance Method Summary collapse

Methods inherited from PollCondition

#after, #before

Methods inherited from God::Condition

#friendly_name, generate, valid?

Methods inherited from Behavior

#after_restart, #after_start, #after_stop, #before_restart, #before_start, #before_stop, #friendly_name, generate

Methods included from God::Configurable

#base_name, complain, #complain, #friendly_name, #prepare, #reset

Instance Attribute Details

#pid_fileObject

Returns the value of attribute pid_file.



37
38
39
# File 'lib/god/conditions/process_running.rb', line 37

def pid_file
  @pid_file
end

#runningObject

Returns the value of attribute running.



37
38
39
# File 'lib/god/conditions/process_running.rb', line 37

def running
  @running
end

Instance Method Details

#pidObject



39
40
41
# File 'lib/god/conditions/process_running.rb', line 39

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

#testObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/god/conditions/process_running.rb', line 50

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

#valid?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/god/conditions/process_running.rb', line 43

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