1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
##
# god configuration file for beanstalk rake task.
#
# run with:  god -c /path/to/god.conf

# Production server
MERB_ROOT = "/var/www/apps/mysite.com/current"
rake = "/usr/bin/rake"

beanstalk_start_command = "cd #{MERB_ROOT} && #{rake} beanstalk:worker MERB_ENV=production"

# HACK Find the actual PID of the beanstalk:worker rake task
#      by looking at the process list.
#
#      The "cd ...; rake" command actually spawns two processes, and we want
#      the one running the beanstalk task.
beanstalk_stop_command  = "kill -9 `ps axf | grep beanstalk:worker | grep -v grep | ruby -e 'puts STDIN.read.split.first'`"

God.watch do |w|

  w.name = "beanstalk-worker"
  w.interval = 30.seconds # default
  w.start = beanstalk_start_command
  w.stop = beanstalk_stop_command
  w.restart = [beanstalk_stop_command, beanstalk_start_command].join(" && ")
  w.start_grace = 10.seconds
  w.restart_grace = 10.seconds

  w.uid = "deploy"
  w.gid = "deploy"

  w.behavior(:clean_pid_file)

  w.start_if do |start|
    start.condition(:process_running) do |c|
      c.interval = 30.seconds
      c.running = false
    end
  end

  w.restart_if do |restart|
    restart.condition(:memory_usage) do |c|
      c.above = 150.megabytes
      c.times = [3, 5] # 3 out of 5 intervals
    end

    restart.condition(:cpu_usage) do |c|
      c.above = 50.percent
      c.times = 5
    end
  end

  # lifecycle
  w.lifecycle do |on|
    on.condition(:flapping) do |c|
      c.to_state = [:start, :restart]
      c.times = 5
      c.within = 5.minute
      c.transition = :unmonitored
      c.retry_in = 10.minutes
      c.retry_times = 5
      c.retry_within = 2.hours
    end
  end
end