Initial commit
[god.git] / pkg / god-0.1.0 / lib / god / conditions / process_not_running.rb
blobac33a98dbb0fd7e0b362a39de11b0910bb80951c
1 module God
2   class ProcessNotRunning < Condition
3     attr_accessor :pid_file, :clean
4     
5     def initialize
6       self.pid_file = nil
7       self.clean = true
8     end
9     
10     def valid?
11       valid = true
12       valid = complain("You must specify the 'pid_file' attribute for :process_not_running") if self.pid_file.nil?
13       valid
14     end
15     
16     def test
17       return false unless File.exist?(self.pid_file)
18       pid = File.open(self.pid_file).read.strip
19       process_running?(pid)
20     end
21     
22     def after
23       if self.clean
24         File.delete(self.pid_file) rescue nil
25       end
26     end
27     
28     private
29     
30     def process_running?(pid)
31       cmd_name = RUBY_PLATFORM =~ /solaris/i ? "args" : "command"
32       ps_output = `ps -o #{cmd_name}= -p #{pid}`
33       !ps_output.strip.empty?
34     end 
35   end
36 end