Class: God::Conditions::ProcessExits

Inherits:
EventCondition show all
Defined in:
lib/god/conditions/process_exits.rb

Overview

Trigger when a process exits.

+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 (non-Watch).
on.condition(:process_exits) do |c|
  c.pid_file = "/var/run/mongrel.3000.pid"
end

Instance Attribute Summary collapse

Attributes inherited from God::Condition

#info, #notify, #phase, #transition

Attributes inherited from Behavior

#watch

Instance Method Summary collapse

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

Constructor Details

#initializeProcessExits

Returns a new instance of ProcessExits.



24
25
26
27
# File 'lib/god/conditions/process_exits.rb', line 24

def initialize
  super
  self.info = 'process exited'
end

Instance Attribute Details

#pid_fileObject

The String PID file location of the process in question. Automatically populated for Watches.



22
23
24
# File 'lib/god/conditions/process_exits.rb', line 22

def pid_file
  @pid_file
end

Instance Method Details

#deregisterObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/god/conditions/process_exits.rb', line 54

def deregister
  pid = self.pid
  if pid
    EventHandler.deregister(pid, :proc_exit)

    msg = "#{watch.name} deregistered 'proc_exit' event for pid #{pid}"
    applog(watch, :info, msg)
  else
    pid_file_location = pid_file || watch.pid_file
    applog(watch, :error, "#{watch.name} could not deregister: no cached PID or PID file #{pid_file_location} (#{base_name})")
  end
end

#pidObject



33
34
35
# File 'lib/god/conditions/process_exits.rb', line 33

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

#registerObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/god/conditions/process_exits.rb', line 37

def register
  pid = self.pid

  begin
    EventHandler.register(pid, :proc_exit) do |extra|
      formatted_extra = extra.empty? ? '' : " #{extra.inspect}"
      self.info = "process #{pid} exited#{formatted_extra}"
      watch.trigger(self)
    end

    msg = "#{watch.name} registered 'proc_exit' event for pid #{pid}"
    applog(watch, :info, msg)
  rescue StandardError
    raise EventRegistrationFailedError
  end
end

#valid?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/god/conditions/process_exits.rb', line 29

def valid?
  true
end