Class: Heaven

Inherits:
Object
  • Object
show all
Defined in:
lib/heaven/heaven.rb

Class Method Summary collapse

Class Method Details

.defaults(options = nil) ⇒ Object

Set the default options for all subsequent watch calls

Available options are:

start

start command;mandatory

stop

stop command; mandatory

restart

restart command; optional, if none is specified then god will execute a stop and a start

pid_file

the full path of the pid file the service uses

log_file

the full path of the log file where all the daemon’s output will be dumped

uid

the user this service needs to be run as

gid

the group this service needs to be run as

interval

interval to poll the service and check against the conditions (default: 15s)

grace

wait interval while starting or restarting the service before beginning to monitor it again (default: 15s)

behaviors

god behaviors to enforce for this service, see god documentation (default: [:clean_pid_file])

check_flapping

check to see if the process keeps being restarted ad-infinitum and stop trying to restart it

check_memory

check memory usage (default: true)

check_cpu

check cpu usage (default: true)

max_memory

restart the service if its memory usage exceeds this much on [max_times] consecutive polls (default: 250.megabytes)

max_cpu

restart the service if its cpu usage exceeds this much on [max_times] consecutive polls (default: 50.percent)

max_times

see the above (default: 5)



28
29
30
31
32
33
34
# File 'lib/heaven/heaven.rb', line 28

def defaults(options=nil)
  if options.nil?
	@defaults || {}
  else
	@defaults = options
  end
end

.watch_daemon(name, options = {}) ⇒ Object

Watch a run-of-the-mill UNIX daemon

It takes all the options available to defaults and:

script

name of the daemon’s command script (it must take options stop, start and restart)



40
41
42
43
44
45
46
47
48
49
# File 'lib/heaven/heaven.rb', line 40

def watch_daemon(name, options = {})
  script = options.delete(:script)
  if script
	options[:start] = "#{script} start"
	options[:stop] = "#{script} stop"
	options[:restart] = "#{script} restart"
  end
	
  GodWatcher.new(name, defaults.merge(options)).watch
end

.watch_mongrel(name, options = {}) ⇒ Object

Watch a mongrel Rails app

It takes all the options available to defaults and:

env

environment to run under

root

application’s root dir (e.g. /var/rails/myapp/current)

pid_prefix

the prefix of the pid file (takes something like tmp/pids/yourapp and looks for pids like tmp/pids/yourapp.12345.pid)

log_prefix

the prefix of the mongrel log file (takes something like log/yourapp and looks for logs like log/yourapp.12345.log)

ports

a list of ports the mongrels run on



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/heaven/heaven.rb', line 60

def watch_mongrel(name, options = {})
  env = options.delete(:env)
  root = options.delete(:root)
  pid_prefix = options.delete(:pid_prefix)
  log_prefix = options.delete(:log_prefix)
  ports = options.delete(:ports)

  ports.each do |port|
	mongrel_hash = { 
	  :start => "mongrel_rails start -d -e #{env} -c #{root} -p #{port} -P #{root}/#{pid_prefix}.#{port}.pid -l #{root}/#{log_prefix}.#{port}.log",
	  :stop  => "mongrel_rails stop               -c #{root} -p #{port} -P #{root}/#{pid_prefix}.#{port}.pid",
	  :pid_file => "/var/rails/lessmemories/current/log/mongrel.#{port}.pid"
	}
	GodWatcher.new("#{name}-#{env}-#{port}", defaults.merge(mongrel_hash).merge(options)).watch
  end
end

.watch_url(name, options = {}) ⇒ Object

Watch a URL

It takes all the options available to defaults and:

url

the url to watch

url_timeout

how long to wait for a status

Note that the start and stop options are ignored, this just restarts the service



85
86
87
88
89
90
91
92
93
94
# File 'lib/heaven/heaven.rb', line 85

def watch_url(name, options = {})
  options[:check_cpu] = false
  options[:check_memory] = false
  options[:check_flapping] = false
  options[:check_url] = true

  options[:start] = ""
  options[:stop]  = ""
  GodWatcher.new(name, defaults.merge(options)).watch
end