Skip to content

Instantly share code, notes, and snippets.

@rodrigodealer
Forked from shapeshed/unicorn
Last active October 13, 2015 07:28
Show Gist options
  • Save rodrigodealer/4160565 to your computer and use it in GitHub Desktop.
Save rodrigodealer/4160565 to your computer and use it in GitHub Desktop.
Unicorn / Monit setup
RAILS_ENV=production
RAILS_ROOT=/home/deploy/apps/app/current
USER=deploy
START_CMD="/home/deploy/.rbenv/shims/bundle exec unicorn -c config/unicorn.rb -E production -D"
UNICORN_CONFIG=$RAILS_ROOT/config/unicorn.rb
PID="/home/deploy/shared/pids/unicorn.pid"
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs mysql
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: unicorn initscript
# Description: Unicorn is an HTTP server for Rack application
### END INIT INFO
# Author: Troex Nevelin <[email protected]>
# based on http://gist.github.com/308216 by http://github.com/mguterl
# A sample /etc/unicorn/my_app.conf, only RAILS_ROOT is required, other are optional
#
## RAILS_ENV=production
## RAILS_ROOT=/var/apps/www/my_app/current
## PID=$RAILS_ROOT/tmp/pids/unicorn.pid
## START_CMD="bundle exec unicorn"
## USER="www-data"
## RESTART_SLEEP=5
## HANDLE_DELAYED_JOB=true
# A recommended /etc/unicorn/my_app.unicorn.rb
#
## APP_ROOT = ENV["RAILS_ROOT"]
## RAILS_ENV = ENV["RAILS_ENV"]
##
## pid "#{APP_ROOT}/tmp/pids/unicorn.pid"
## listen "#{APP_ROOT}/tmp/sockets/unicorn.sock"
## stderr_path "#{APP_ROOT}/log/unicorn_error.log"
##
## working_directory "#{APP_ROOT}"
## worker_processes 1
set -e
sig () {
test -s "$PID" && kill -$1 `cat "$PID"`
}
oldsig () {
test -s "$OLD_PID" && kill -$1 `cat "$OLD_PID"`
}
cmd () {
case $1 in
start)
sig 0 && echo >&2 "Already running" && return
echo "Starting"
$CMD
;;
stop)
sig QUIT && echo "Stopping" && return
echo >&2 "Not running"
;;
force-stop)
sig TERM && echo "Forcing a stop" && return
echo >&2 "Not running"
;;
restart|reload)
sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && return
echo >&2 "Couldn't reload, starting '$CMD' instead"
$CMD
;;
upgrade)
sig USR2 && exit 0
# echo >&2 "Couldn't upgrade, starting '$CMD' instead"
$CMD
;;
rotate)
sig USR1 && echo rotated logs OK && return
echo >&2 "Couldn't rotate logs" && return
;;
status)
sig 0 && echo >&2 "Already running" && return
echo >&2 "Not running" && return
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|reload|status|upgrade|rotate|force-stop>"
return
;;
esac
}
setup () {
echo -n "$RAILS_ROOT: "
cd $RAILS_ROOT || exit 1
if [ -z "$RAILS_ENV" ]; then
RAILS_ENV=development
fi
if [ -z "$PID" ]; then
PID=$RAILS_ROOT/tmp/pids/unicorn.pid
fi
if [ -z "$RESTART_SLEEP" ]; then
RESTART_SLEEP=5
fi
export PID
export OLD_PID="$PID.oldbin"
export RAILS_ROOT
export RESTART_SLEEP
if [ -z "$START_CMD" ]; then
START_CMD="bundle exec unicorn"
fi
CMD="$START_CMD -c $UNICORN_CONFIG -E $RAILS_ENV -D"
if [ "$USER" != `whoami` ]; then
CMD="sudo -u $USER -- env RAILS_ROOT=$RAILS_ROOT $CMD"
fi
export CMD
#echo $CMD
}
handle_delayed_job () {
# $1 contains command
if [ "$HANDLE_DELAYED_JOB" != "true" ]; then
return
fi
case $1 in
start|stop|restart|reload|status)
CMD="env RAILS_ENV=$RAILS_ENV bundle exec ./script/delayed_job $1"
if [ "$USER" != `whoami` ]; then
CMD="sudo -u $USER -- env $CMD"
fi
$CMD
;;
esac
}
start_stop () {
# either run the start/stop/reload/etc command for every config under /etc/unicorn
# or just do it for a specific one
# $1 contains the start/stop/etc command
# $2 if it exists, should be the specific config we want to act on
if [ -n "$2" ]; then
if [ -f "/etc/unicorn/$2.conf" ]; then
. /etc/unicorn/$2.conf
export UNICORN_CONFIG="/etc/unicorn/$2.unicorn.rb"
setup
cmd $1
handle_delayed_job $1
else
echo >&2 "/etc/unicorn/$2.conf: not found"
fi
else
for CONFIG in /etc/unicorn/*.conf; do
# import the variables
export UNICORN_CONFIG=`echo ${CONFIG} | sed 's/conf/unicorn.rb/'`
. $CONFIG
setup
# run the start/stop/etc command
cmd $1
handle_delayed_job $1
# clean enviroment
unset PID
unset OLD_PID
unset RAILS_ROOT
unset RAILS_ENV
unset CMD
unset USER
unset START_CMD
unset UNICORN_CONFIG
done
fi
}
ARGS="$1 $2"
start_stop $ARGS
APP_ROOT = File.expand_path(File.dirname(File.dirname(__FILE__)))
if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm')
begin
rvm_path = File.dirname(File.dirname(ENV['MY_RUBY_HOME']))
rvm_lib_path = File.join(rvm_path, 'lib')
$LOAD_PATH.unshift rvm_lib_path
require 'rvm'
RVM.use_from_path! APP_ROOT
rescue LoadError
raise "RVM ruby lib is currently unavailable."
end
end
ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', File.dirname(__FILE__))
require 'bundler/setup'
worker_processes 1
working_directory APP_ROOT
preload_app true
timeout 30
listen APP_ROOT + "/tmp/sockets/unicorn.sock", :backlog => 64
pid APP_ROOT + "/tmp/pids/unicorn.pid"
stderr_path APP_ROOT + "/log/unicorn.stderr.log"
stdout_path APP_ROOT + "/log/unicorn.stdout.log"
before_fork do |server, worker|
defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect!
old_pid = Rails.root + '/tmp/pids/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
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection
child_pid = server.config[:pid].sub('.pid', ".#{worker.nr}.pid")
system("echo #{Process.pid} > #{child_pid}")
end
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