added MemoryUsage and factored out ProcessCondition
[god.git] / lib / god / conditions / memory_usage.rb
bloba5b4bc1584fd0bb2327eef4574dd731cc423a2dc
1 module God
2   module Conditions
3     
4     class MemoryUsage < ProcessCondition
5       attr_accessor :above, :times
6     
7       def initialize
8         super
9         self.above = nil
10         self.times = [1, 1]
11       end
12       
13       def prepare
14         p self.times.class
15         
16         if self.times.kind_of?(Integer)
17           self.times = [self.times, self.times]
18         end
19         
20         @timeline = Timeline.new(self.times[1])
21       end
22     
23       def valid?
24         valid = super
25         valid = complain("You must specify the 'above' attribute for :memory_usage") if self.above.nil?
26         valid
27       end
28     
29       def test
30         return false unless super
31         pid = File.open(self.pid_file).read.strip
32         process = System::Process.new(pid)
33         @timeline.push(process.memory)
34         if @timeline.select { |x| x > self.above }.size < self.times.first
35           return true
36         else
37           @timeline.clear
38           return false
39         end
40       end
41     end
42     
43   end
44 end