Using God to Watch Unicorn and Sidekiq

Load config

1
2
3
4
5
6
7
8
9
10
11
12
13
RAILS_ENV  = ENV['RAILS_ENV']  = 'production'
RAILS_ROOT = ENV['RAILS_ROOT'] = '/path/to/your_app/current'
PID_DIR    = '/path/to/your_app/shared/pids'
BIN_PATH   = "/usr/local/rvm/gems/ruby-2.0.0-p247/bin"
UID = 'root'
GID = 'root'

God.log_file  = "#{RAILS_ROOT}/log/god.log"
God.log_level = :info

%w(unicorn sidekiq).each do |config|
  God.load "#{RAILS_ROOT}/config/god/#{config}.god"
end

Watch Unicorn

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
God.watch do |w|
  w.dir      = "#{RAILS_ROOT}"
  w.name     = "unicorn"
  w.interval = 30.seconds

  w.start   = "cd #{RAILS_ROOT} && #{BIN_PATH}/bundle exec unicorn_rails -c #{RAILS_ROOT}/config/unicorn.rb -E #{RAILS_ENV} -D"
  w.stop    = "kill -QUIT `cat #{PID_DIR}/unicorn.pid`"
  w.restart = "kill -USR2 `cat #{PID_DIR}/unicorn.pid`"

  w.start_grace = 10.seconds
  w.restart_grace = 10.seconds

  w.uid = UID
  w.gid = GID

  w.pid_file = "#{PID_DIR}/unicorn.pid"
  w.behavior(:clean_pid_file)

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

  w.restart_if do |restart|
    restart.condition(:memory_usage) do |c|
      c.above = 300.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

  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

Watch Sidekiq

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
God.watch do |w|
  w.dir      = "#{RAILS_ROOT}"
  w.name     = "sidekiq"
  w.interval = 30.seconds

  w.start = "cd #{RAILS_ROOT}; nohup #{BIN_PATH}/bundle exec sidekiq -e production -C #{RAILS_ROOT}/config/sidekiq.yml -i 0 -P #{PID_DIR}/sidekiq.pid >> #{RAILS_ROOT}/log/sidekiq.log 2>&1 &"
  w.stop  = "if [ -d #{RAILS_ROOT} ] && [ -f #{PID_DIR}/sidekiq.pid ] && kill -0 `cat #{PID_DIR}/sidekiq.pid`> /dev/null 2>&1; then cd #{RAILS_ROOT} && #{BIN_PATH}/bundle exec sidekiqctl stop #{PID_DIR}/sidekiq.pid 10 ; else echo 'Sidekiq is not running'; fi"

  w.start_grace = 10.seconds
  w.restart_grace = 10.seconds

  w.uid = UID
  w.gid = GID

  w.pid_file = "#{PID_DIR}/sidekiq.pid"
  w.behavior(:clean_pid_file)

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

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