Last active
July 19, 2019 00:16
-
-
Save albertogg/3837632 to your computer and use it in GitHub Desktop.
unicorn init.d script with unicorn.rb configuration and terminal stdout for development.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add this in the environments/development.rb to view the output of the server directly from the terminal | |
# Config Logger. | |
config.logger = Logger.new(STDOUT) | |
config.logger.level = Logger.const_get( | |
ENV['LOG_LEVEL'] ? ENV['LOG_LEVEL'].upcase : 'DEBUG' | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set environment to development unless something else is specified | |
env = ENV["RAILS_ENV"] || "development" | |
listen "/tmp/unicorn.sock", :backlog => 512 | |
preload_app true | |
timeout 30 | |
# Production specific settings | |
if env == "production" | |
worker_processes 2 | |
# set the unpriviledge users for the unicorn workers | |
user "deploy_user", "deploy_user_group" | |
# Help ensure your application will always spawn in the symlinked | |
# "current" directory that Capistrano sets up. | |
working_directory "/home/deployer/apps/my_site/current" | |
# # feel free to point this anywhere accessible on the filesystem | |
# user 'deployer' | |
shared_path = "/home/deployer/apps/my_site/shared" | |
pid "#{shared_path}/pids/unicorn.pid" | |
stderr_path "#{shared_path}/log/unicorn.stderr.log" | |
stdout_path "#{shared_path}/log/unicorn.stdout.log" | |
# Productionlocal specific settings | |
elsif env == "productionlocal" | |
worker_processes 2 | |
working_directory "/path/to/production/local" | |
elsif env == "development" | |
worker_processes 1 | |
working_directory "/path/to/development" | |
end | |
before_fork do |server, worker| | |
# This option works in together with preload_app true setting | |
# What is does is prevent the master process from holding | |
# the database connection | |
if defined?(ActiveRecord::Base) and | |
ActiveRecord::Base.connection.disconnect! | |
end | |
# Before forking, kill the master process that belongs to the .oldbin PID. | |
# This enables 0 downtime deploys. | |
old_pid = "#{shared_path}/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 | |
# someone else did our job for us | |
end | |
end | |
end | |
after_fork do |server, worker| | |
# Here we are establishing the connection after forking worker | |
# processes | |
if defined?(ActiveRecord::Base) and | |
ActiveRecord::Base.establish_connection | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
# | |
# init.d script for single or multiple unicorn installations. Expects at least one .conf | |
# file in /etc/unicorn | |
# | |
# Modified by [email protected] http://github.com/jaygooby | |
# based on http://gist.github.com/308216 by http://github.com/mguterl | |
# | |
# Modified by albertogg. | |
# | |
## A sample /etc/unicorn/my_app.conf | |
## | |
RAILS_ENV=production | |
RAILS_ROOT=/home/deploy/app/cacao-project/current | |
# | |
# 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. | |
# | |
# 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 /etc/unicorn/my_app.conf | |
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" && exit 0 | |
echo "Starting" | |
$CMD | |
;; | |
stop) | |
sig QUIT && echo "Stopping" && exit 0 | |
echo >&2 "Not running" | |
;; | |
force-stop) | |
sig TERM && echo "Forcing a stop" && exit 0 | |
echo >&2 "Not running" | |
;; | |
restart|reload) | |
sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && exit 0 | |
echo >&2 "Couldn't reload, starting '$CMD' instead" | |
$CMD | |
;; | |
upgrade) | |
sig USR2 && echo Upgraded && exit 0 | |
echo >&2 "Couldn't upgrade, starting '$CMD' instead" | |
$CMD | |
;; | |
rotate) | |
sig USR1 && echo rotated logs OK && exit 0 | |
echo >&2 "Couldn't rotate logs" && exit 1 | |
;; | |
*) | |
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>" | |
exit 1 | |
;; | |
esac | |
} | |
setup () { | |
echo -n "$RAILS_ROOT: " | |
cd $RAILS_ROOT || exit 1 | |
export PID=/path/to/shared/pids/unicorn.pid | |
export OLD_PID="$PID.oldbin" | |
cd $RAILS_ROOT | |
BUNDLER="`which bundler`" | |
DAEMON="`which unicorn_rails`" | |
CMD="$BUNDLER exec $DAEMON -c config/unicorn.rb -E $RAILS_ENV -D" | |
} | |
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 [ $2 ]; then | |
. $2 | |
setup | |
cmd $1 | |
else | |
setup | |
# run the start/stop/etc command | |
cmd $1 | |
done | |
fi | |
} | |
ARGS="$1 $2" | |
start_stop $ARGS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment