Skip to content

Instantly share code, notes, and snippets.

@rostamizadeh
Created July 12, 2012 22:19
Show Gist options
  • Save rostamizadeh/3101459 to your computer and use it in GitHub Desktop.
Save rostamizadeh/3101459 to your computer and use it in GitHub Desktop.
My Monit-Capistrano configuration for watching over Unicorn master and worker processes
namespace :monit do
desc "Monit unicorn configuration"
task :unicorn_service_tests, roles: :web do
run "mkdir -p #{shared_path}/config"
template "monit_unicorn.rb.erb", "/tmp/monit_unicorn"
run "#{sudo} mv /tmp/monit_unicorn /etc/monit/conf.d/unicorn_#{application}_#{rails_env}.conf"
unicorn_worker_pids.each do |val|
worker_template "monit_unicorn_worker.rb.erb", "/tmp/monit_unicorn_#{application}_#{rails_env}_#{val}_worker", val
run "#{sudo} mv /tmp/monit_unicorn_#{application}_#{rails_env}_#{val}_worker /etc/monit/conf.d/unicorn_#{application}_#{rails_env}_#{val}_worker.conf"
end
run "#{sudo} monit -g unicorn_#{application}_#{rails_env} reload "
end
after "monit:unmonitor", "monit:unicorn_service_tests"
%w[monitor unmonitor].each do |command|
desc "monit #{command} unicorn_#{application}_#{rails_env}"
task command, roles: :web do
#run at group level
run "#{sudo} monit -g unicorn_#{application}_#{rails_env} #{command} "
end
end
after "deploy", "monit:monitor"
after "deploy:setup", "monit:monitor"
before "deploy", "monit:unmonitor"
before "deploy:setup", "monit:unmonitor"
end
check process unicorn_<%= application %>_<%= rails_env %>
with pidfile "<%= shared_path %>/pids/unicorn.pid"
start program = "/etc/init.d/unicorn_<%= application %>_<%= rails_env %> start"
stop program = "/etc/init.d/unicorn_<%= application %>_<%= rails_env %> stop"
if mem is greater than 50.0 MB for 1 cycles then restart
if cpu is greater than 25% for 1 cycles then restart
group unicorn_<%= application %>_<%= rails_env %>
check process unicorn_<%= application %>_<%= rails_env %>_<%= val %>_worker
with pidfile "<%= shared_path %>/pids/unicorn.worker.<%= val %>.pid"
start program = "/bin/true"
stop program = "/etc/init.d/unicorn_<%= application %>_<%= rails_env %> kill_worker <%= val %>"
if mem is greater than 90.0 MB for 1 cycles then restart
if cpu is greater than 40% for 3 cycles then restart
group unicorn_<%= application %>_<%= rails_env %>
set :unicorn_workers, "1"
set :unicorn_worker_pids, (0...(unicorn_workers.to_i)).to_a
def worker_template(from, to, val)
erb = File.read(File.expand_path("../templates/#{from}", __FILE__))
put ERB.new(erb).result(binding), to
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment