Class: God::Conditions::MemoryUsage

Inherits:
PollCondition show all
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.

Parameters 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

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

Constructor Details

#initialize ⇒ MemoryUsage

Returns a new instance of MemoryUsage.



37
38
39
40
41
# File 'lib/god/conditions/memory_usage.rb', line 37

def initialize
  super
  self.above = nil
  self.times = [1, 1]
end

Instance Attribute Details

#above ⇒ Object

Returns the value of attribute above.



35
36
37
# File 'lib/god/conditions/memory_usage.rb', line 35

def above
  @above
end

#pid_file ⇒ Object

Returns the value of attribute pid_file.



35
36
37
# File 'lib/god/conditions/memory_usage.rb', line 35

def pid_file
  @pid_file
end

#times ⇒ Object

Returns the value of attribute times.



35
36
37
# File 'lib/god/conditions/memory_usage.rb', line 35

def times
  @times
end

Instance Method Details

#pid ⇒ Object



53
54
55
# File 'lib/god/conditions/memory_usage.rb', line 53

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

#prepare ⇒ Object



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

def prepare
  self.times = [times, times] if times.is_a?(Integer)

  @timeline = Timeline.new(times[1])
end

#reset ⇒ Object



49
50
51
# File 'lib/god/conditions/memory_usage.rb', line 49

def reset
  @timeline.clear
end

#test ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/god/conditions/memory_usage.rb', line 64

def test
  process = System::Process.new(pid)
  @timeline.push(process.memory)
  self.info = []

  history = @timeline.map { |x| "#{x > above ? '*' : ''}#{x}kb" }.join(', ')

  if @timeline.count { |x| x > above } >= times.first
    self.info = "memory out of bounds [#{history}]"
    true
  else
    false
  end
end

#valid? ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'lib/god/conditions/memory_usage.rb', line 57

def valid?
  valid = true
  valid &= complain("Attribute 'pid_file' must be specified", self) if pid_file.nil? && watch.pid_file.nil?
  valid &= complain("Attribute 'above' must be specified", self) if above.nil?
  valid
end