Skip to content

Instantly share code, notes, and snippets.

@tigris
Forked from shapeshed/unicorn
Created August 3, 2012 12:27
Show Gist options
  • Save tigris/3247118 to your computer and use it in GitHub Desktop.
Save tigris/3247118 to your computer and use it in GitHub Desktop.
Unicorn / Monit setup
default['unicorn']['before_fork'] = %Q(
defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect!
old_pid = '#{node['unicorn']['pid']}.oldbin'
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill('QUIT', File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
puts 'Old master alerady dead'
end
end
)
default['unicorn']['after_fork'] = %q(
defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection
child_pid = server.config[:pid].sub('.pid', ".#{worker.nr}.pid")
system("echo #{Process.pid} > #{child_pid}")
)
##
# Unicorn config for <%= @identifier %>
# Managed by Chef - Local Changes will be obliterated
##
user "<%= @user %>"<%= @group ? %Q(, "#{@group}") : '' %>
timeout <%= @worker_timeout %>
preload_app <%= @preload_app %>
worker_processes <%= @worker_processes %>
pid '<%= @pid %>'
<% @listen.each do |listen| %>
listen "<%= listen[0] %>"<%= listen[1] ? %Q(, "#{listen[1]}") : '' %>
<% end %>
<% if @working_directory %>
working_directory '<%= @working_directory %>'
<% end %>
<% if @before_exec %>
before_exec do |server|
<%= @before_exec %>
end
<% end %>
<% if @before_fork %>
before_fork do |server, worker|
<%= @before_fork %>
end
<% end %>
<% if @after_fork %>
after_fork do |server, worker|
<%= @after_fork %>
end
<% end %>
<% if @stderr_path %>
stderr_path '<%= @stderr_path %>'
<% end %>
<% if @stdout_path %>
stdout_path '<%= @stdout_path %>'
<%- end %>
#!/bin/bash
# /etc/init.d/unicorn
# To install: update-rc.d unicorn defaults
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Unicorn server
# Description: Starts the unicorn server
### END INIT INFO
set -u
set -e
TIMEOUT=<%= @timeout %>
CMD="unicorn-<%= @environment %>"
PID="<%= @pid %>"
OLDPID="$PID.oldbin"
cd <%= @root %> || exit 1
sig() {
test -s $PID && kill -$1 `cat $PID`
}
workersig () {
workerpid=${PID/.pid/$2.pid}
test -s "$workerpid" && kill -$1 `cat $workerpid`
}
oldsig () {
test -s OLDPID && kill -$1 `cat OLDPID`
}
start() {
su -l -c '<%= @command %>' <%= @user %>
}
case $1 in
start)
sig 0 && echo >&2 "Already running" && exit 0
start
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
test -s $PID && rm $PID
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
test -s $PID && rm $PID
;;
reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting unicorn-<%= @environment %> instead"
start
;;
status)
test -s $PID && test -d "/proc/`cat $PID`" && echo running && exit 0
echo stopped && exit 3
;;
restart)
sig USR2 && sleep 2 && sig 0 && exit 0
echo >&2 "Couldn't restart, starting unicorn-<%= @environment %> instead"
start
;;
kill_worker)
workersig QUIT $2 && exit 0
echo >&2 "Worker not running"
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|rotate|force-stop|kill_worker>"
exit 1
;;
esac
check process unicorn_worker_0
with pidfile /path/to/your/app/shared/pids/unicorn.0.pid
start program = "/bin/cat /dev/null"
stop program = "/etc/init.d/unicorn kill_worker 0"
if mem is greater than 175.0 MB for 1 cycles then restart
if cpu is greater than 22% for 2 cycles then alert
if cpu is greater than 25% for 1 cycles then restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment