Skip to content

Instantly share code, notes, and snippets.

@c4nc
Last active November 8, 2016 11:03
Show Gist options
  • Save c4nc/696925a6c55d20ebf0e0 to your computer and use it in GitHub Desktop.
Save c4nc/696925a6c55d20ebf0e0 to your computer and use it in GitHub Desktop.
Unicorn conf 0 downtime centos 6
#!/bin/bash
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $all
# Required-Stop: $network $local_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the unicorns at boot
# Description: Enable at boot time.
#
### END INIT INFO
# This is /etc/init.d/unicorn init.d script for single or multiple unicorn installations.
# Expects at least one .conf file in /etc/unicorn/
#
## A sample /etc/unicorn/my_app.conf
##
## RAILS_ENV=production
## RAILS_ROOT=/var/apps/www/my_app/current
## UNICORN="/usr/local/rvm/wrappers/my_app/unicorn_rails"
#
# This configures a unicorn master for your app at /var/apps/www/my_app/current running in
# production mode. It will read config/unicorn.rb for further set up.
#
## To get the UNICORN script run:
##
## rvm alias create my_app 2.0.0
## rvm wrapper 2.0.0 --no-links unicorn_rails
#
# This way it will allow changing the alias when new ruby is used without changing anything else.
#
# You should ensure different ports or sockets are set in each config/unicorn.rb if
# you are running more than one master concurrently.
#
# If you call this script without any config parameters, it will attempt to run the
# init command for all your unicorn configurations listed in /etc/unicorn/*.conf
#
# /etc/init.d/unicorn start # starts all unicorns
#
# If you specify a particular config, it will only operate on that one
#
# /etc/init.d/unicorn start my_app
__sig()
{
typeset __pid
[[ -s "$2" ]] &&
__pid="$(cat "$2")" &&
[[ -n "${__pid}" ]] &&
kill -$1 "${__pid}" >/dev/null 2>&1 ||
return $?
}
sig()
{
__sig "$1" "$PID" || return $?
}
oldsig()
{
__sig "$1" "$OLD_PID" || return $?
}
run()
{
echo -n "$1 - "
shift
if
"$@"
then
echo "OK"
else
typeset result=$?
echo "Failed!" >&2
return $result
fi
}
prefix_command_with_su_fix_quoting()
{
typeset -a __temp
__temp=()
while
(( $# ))
do
__temp+=( "'$1'" )
shift
done
CMD=( su - "${__owner}" -c "cd '$RAILS_ROOT' && ${__temp[*]}" )
}
setup ()
{
echo "$RAILS_ROOT: "
cd $RAILS_ROOT || return $?
export PID=$RAILS_ROOT/tmp/pids/unicorn.pid
export OLD_PID="$PID.oldbin"
export RAILS_ENV=production
CMD=( "$UNICORN" -c "${RAILS_ROOT}/config/unicorn.rb" -E $RAILS_ENV -D )
typeset __owner="$(stat -c "%U" "${RAILS_ROOT}")"
if
[[ "${USER:=$(whoami)}" == "${__owner}" ]]
then
true # it's all fine we run as owner of the app
elif
(( UID == 0 ))
then
prefix_command_with_su_fix_quoting "${CMD[@]}"
else
echo "ERROR: running not as owner(${__owner}) of '$RAILS_ROOT' and not as root($USER), prefix with 'sudo' and try again!"
return 2
fi
}
cmd_start()
{
if sig 0
then echo "Already started"
else run "Starting" "${CMD[@]}" || return $?
fi
}
wait_pid_kill()
{
typeset __count=$1
while
(( __count > 0 )) &&
sig 0
do
: $(( __count-- ))
sleep 1s
done
sig 0 || return $?
}
cmd_stop()
{
run "Stopping" sig QUIT
if
wait_pid_kill 5
then
run "Force stop" sig TERM
if wait_pid_kill 3
then return 1
fi
fi
}
cmd_restart()
{
cmd_stop && cmd_start || return $?
}
cmd_reload()
{
run "Reloading" sig USR2 &&
wait_pid_kill 5 &&
oldsig QUIT ||
oldsig TERM ||
cmd_restart ||
return $?
}
cmd_rotate()
{
run "Rotate" sig USR1 ||
cmd_start ||
return $?
}
cmd()
{
setup || return $?
case "$1" in
start|stop|restart|reload|rotate)
cmd_$1 || return $?
;;
upgrade)
cmd_reload || return $?
;;
*)
echo "Usage: $0 <start|stop|restart|reload|upgrade|rotate>" >&2
return 1
;;
esac
}
# 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
start_stop ()
{
if
[[ -n "$2" ]]
then
. "/etc/unicorn/$2.conf" || return $?
cmd "$1" || return $?
else
for CONFIG in /etc/unicorn/*.conf
do
. "$CONFIG" || return $?
cmd "$1" || return $?
done
fi
}
start_stop "$@"
rails_env = ENV['RAILS_ENV'] || 'production'
application_name = "xxxx"
# Help ensure your application will always spawn in the symlinked
# "current" directory that Capistrano sets up.
working_directory "/xxxx/#{application_name}/current" # available in 0.94.0+
worker_processes 2
# Load rails+github.git into the master before forking workers
# for super-fast worker spawn times
preload_app true
# Restart any workers that haven't responded in 30 seconds
timeout 30
# Listen on a Unix data socket
listen "/tmp/#{application_name}.sock", :backlog => 2048
pid "#{working_directory}/shared/pids/unicorn.pid"
# By default, the Unicorn logger will write to stderr.
# Additionally, ome applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
stderr_path "#{working_directory}/shared/log/unicorn.stderr.log"
stdout_path "#{working_directory}/shared/log/unicorn.stdout.log"
##
# REE
# http://www.rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end
before_fork do |server, worker|
##
# When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
# immediately start loading up a new version of itself (loaded with a new
# version of our app). When this new Unicorn is completely loaded
# it will begin spawning workers. The first worker spawned will check to
# see if an .oldbin pidfile exists. If so, this means we've just booted up
# a new Unicorn and need to tell the old one that it can now die. To do so
# we send it a QUIT.
#
# Using this method we get 0 downtime deploys.
if defined? ActiveRecord::Base
ActiveRecord::Base.connection.disconnect!
end
old_pid = "#{server.config[: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
# someone else did our job for us
end
end
end
after_fork do |server, worker|
##
# Unicorn master loads the app then forks off workers - because of the way
# Unix forking works, we need to make sure we aren't using any of the parent's
# sockets, e.g. db connection
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
child_pid = server.config[:pid].sub(".pid", ".#{worker.nr}.pid")
system("echo #{Process.pid} > #{child_pid}")
##
# Unicorn master is started as root, which is fine, but let's
# drop the workers to git:git
begin
uid, gid = Process.euid, Process.egid
user, group = 'deploy', 'deploy'
target_uid = Etc.getpwnam(user).uid
target_gid = Etc.getgrnam(group).gid
worker.tmp.chown(target_uid, target_gid)
if uid != target_uid || gid != target_gid
Process.initgroups(user, target_gid)
Process::GID.change_privilege(target_gid)
Process::UID.change_privilege(target_uid)
end
rescue => e
if rails_env == 'development'
STDERR.puts "couldn't change user, oh well"
else
raise e
end
end
end
before_exec do |server|
ENV['BUNDLE_GEMFILE'] = "#{working_directory}/current/Gemfile"
end
#/etc/unicorn/appxxx.conf
RAILS_ROOT=/var/www/appxxx/current
RAILS_ENV=production
UNICORN=/usr/local/rvm/wrappers/ruby-2.3.1/unicorn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment