Skip to content

Instantly share code, notes, and snippets.

@yesnik
Last active June 24, 2016 13:31
Show Gist options
  • Save yesnik/b6a95bfa90cf89602747b7f4f091a489 to your computer and use it in GitHub Desktop.
Save yesnik/b6a95bfa90cf89602747b7f4f091a489 to your computer and use it in GitHub Desktop.
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
UNICORN_GROUP=${UNICORN_GROUP:-${UNICORN_USER}}
UNICORN_CONFIGFILE=${UNICORN_CONFIGFILE:-/etc/unicorn/${RC_SVCNAME}.conf.rb}
command=${UNICORN_BINARY:-/usr/bin/unicorn_rails}
command_args="${UNICORN_ARGS:--E production} -D -c ${UNICORN_CONFIGFILE}"
pidfile=${UNICORN_PIDFILE:-/var/run/${RC_SVCNAME}/unicorn.pid}
#start_stop_daemon_args="--user ${UNICORN_USER} --env GEM_HOME=/home/dk/.gem/ruby/1.9"
start_stop_daemon_args="--user ${UNICORN_USER}"
stopsig="SIGQUIT"
extra_started_commands="reload addworker delworker faststop upgrade"
description="Unicorn application server"
description_reload="Reload configuration"
description_addworker="Increment worker processes by one"
description_delworker="Decrement worker processes by one"
description_faststop="Kills all workers immediately"
description_upgrade="Upgrade the running binary"
start_pre() {
if [ -z "${UNICORN_USER}" ]; then
einfo "Please clarify username for unicorn application"
return 1
fi
checkpath -d -m 0750 -o "${UNICORN_USER}:${UNICORN_GROUP}" "${pidfile%/*}"
}
reload() {
ebegin "Refreshing ${RC_SVCNAME} configuration"
kill -HUP $(cat ${pidfile}) &>/dev/null
eend $? "Failed to reload ${RC_SVCNAME}"
}
addworker() {
ebegin "Increment worker processes ${RC_SVCNAME}"
kill -TTIN $(cat ${pidfile}) &>/dev/null
eend $? "Failed to increment ${RC_SVCNAME}"
}
delworker() {
ebegin "Decrement worker processes ${RC_SVCNAME}"
kill -TTOU $(cat ${pidfile}) &>/dev/null
eend $? "Failed to decrement ${RC_SVCNAME}"
}
faststop() {
ebegin "Quick stopping ${RC_SVCNAME}"
start-stop-daemon \
--stop --signal SIGTERM \
--pidfile "${pidfile}"
eend $? "Failed to stop ${RC_SVCNAME}"
mark_service_stopped
}
upgrade() {
ebegin "Upgrade the running ${RC_SVCNAME}"
kill -USR2 $(cat ${pidfile}) &>/dev/null
eend $? "Failed to upgrade ${RC_SVCNAME}"
}
# Examples: https://github.com/defunkt/unicorn/blob/v4.3.1/examples/unicorn.conf.rb
#
# Set the current app's path for later reference. Rails.root isn't available at
# this point, so we have to point up a directory.
app_path = File.expand_path(File.dirname(__FILE__) + '/..')
# The number of worker processes you have here should equal the number of CPU
# cores your server has.
worker_processes (ENV['RAILS_ENV'] == 'production' ? 4 : 1)
# You can listen on a port or a socket. Listening on a socket is good in a
# production environment, but listening on a port can be useful for local
# debugging purposes.
# listen app_path + '/tmp/unicorn.sock', backlog: 64
listen 8887, :tcp_nopush => true
# For development, you may want to listen on port 3000 so that you can make sure
# your unicorn.rb file is soundly configured.
listen(3000, backlog: 64) if ENV['RAILS_ENV'] == 'development'
# After the timeout is exhausted, the unicorn worker will be killed and a new
# one brought up in its place. Adjust this to your application's needs. The
# default timeout is 60. Anything under 3 seconds won't work properly.
timeout 50
# Set the working directory of this unicorn instance.
working_directory app_path
# Set the location of the unicorn pid file. This should match what we put in the
# unicorn init script later.
pid app_path + '/tmp/unicorn.pid'
# You should define your stderr and stdout here. If you don't, stderr defaults
# to /dev/null and you'll lose any error logging when in daemon mode.
stderr_path app_path + '/log/unicorn.log'
stdout_path app_path + '/log/unicorn.log'
# Load the app up before forking.
preload_app true
# Garbage collection settings.
GC.respond_to?(:copy_on_write_friendly=) &&
GC.copy_on_write_friendly = true
# If using ActiveRecord, disconnect (from the database) before forking.
before_fork do |server, worker|
defined?(ActiveRecord::Base) &&
ActiveRecord::Base.connection.disconnect!
end
# After forking, restore your ActiveRecord connection.
after_fork do |server, worker|
defined?(ActiveRecord::Base) &&
ActiveRecord::Base.establish_connection
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment