Class: God::Conditions::MemoryUsage
- Inherits:
-
PollCondition
- Object
- Behavior
- God::Condition
- PollCondition
- God::Conditions::MemoryUsage
- Defined in:
- lib/god/conditions/memory_usage.rb
Overview
Condition Symbol :memory_usage Type: Poll
Trigger when the resident memory of a process is above a specified limit.
Paramaters
Required
+pid_file+ is the pid file of the process in question. Automatically
populated for Watches.
+above+ is the amount of resident memory (in kilobytes) above which
the condition should trigger. You can also use the sugar
methods #kilobytes, #megabytes, and #gigabytes to clarify
this amount (see examples).
Examples
Trigger if the process is using more than 100 megabytes of resident memory (from a Watch):
on.condition(:memory_usage) do |c|
c.above = 100.megabytes
end
Non-Watch Tasks must specify a PID file:
on.condition(:memory_usage) do |c|
c.above = 100.megabytes
c.pid_file = "/var/run/mongrel.3000.pid"
end
Instance Attribute Summary collapse
-
#above ⇒ Object
Returns the value of attribute above.
-
#pid_file ⇒ Object
Returns the value of attribute pid_file.
-
#times ⇒ Object
Returns the value of attribute times.
Attributes inherited from PollCondition
Attributes inherited from God::Condition
#info, #notify, #phase, #transition
Attributes inherited from Behavior
Instance Method Summary collapse
-
#initialize ⇒ MemoryUsage
constructor
A new instance of MemoryUsage.
- #pid ⇒ Object
- #prepare ⇒ Object
- #reset ⇒ Object
- #test ⇒ Object
- #valid? ⇒ Boolean
Methods inherited from PollCondition
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
Constructor Details
#initialize ⇒ MemoryUsage
Returns a new instance of MemoryUsage.
36 37 38 39 40 |
# File 'lib/god/conditions/memory_usage.rb', line 36 def initialize super self.above = nil self.times = [1, 1] end |
Instance Attribute Details
#above ⇒ Object
Returns the value of attribute above.
34 35 36 |
# File 'lib/god/conditions/memory_usage.rb', line 34 def above @above end |
#pid_file ⇒ Object
Returns the value of attribute pid_file.
34 35 36 |
# File 'lib/god/conditions/memory_usage.rb', line 34 def pid_file @pid_file end |
#times ⇒ Object
Returns the value of attribute times.
34 35 36 |
# File 'lib/god/conditions/memory_usage.rb', line 34 def times @times end |
Instance Method Details
#pid ⇒ Object
54 55 56 |
# File 'lib/god/conditions/memory_usage.rb', line 54 def pid self.pid_file ? File.read(self.pid_file).strip.to_i : self.watch.pid end |
#prepare ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/god/conditions/memory_usage.rb', line 42 def prepare if self.times.kind_of?(Integer) self.times = [self.times, self.times] end @timeline = Timeline.new(self.times[1]) end |
#reset ⇒ Object
50 51 52 |
# File 'lib/god/conditions/memory_usage.rb', line 50 def reset @timeline.clear end |
#test ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/god/conditions/memory_usage.rb', line 65 def test process = System::Process.new(self.pid) @timeline.push(process.memory) self.info = [] history = "[" + @timeline.map { |x| "#{x > self.above ? '*' : ''}#{x}kb" }.join(", ") + "]" if @timeline.select { |x| x > self.above }.size >= self.times.first self.info = "memory out of bounds #{history}" return true else return false end end |
#valid? ⇒ Boolean
58 59 60 61 62 63 |
# File 'lib/god/conditions/memory_usage.rb', line 58 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 'above' must be specified", self) if self.above.nil? valid end |