From 604f2de5a739adfdde370421efaeac6e408f8a7d Mon Sep 17 00:00:00 2001 From: Abner Oliveira Date: Fri, 20 May 2016 15:08:56 -0300 Subject: [PATCH] adjusts for script unicorn --- .gitignore | 1 - ambiente.sh | 0 config/unicorn.default.rb | 16 +++++++++++++--- config/unicorn.rb | 41 +++++++++++++++++++++++++++++++++++++++++ start.sh | 31 +++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 4 deletions(-) create mode 100755 ambiente.sh create mode 100644 config/unicorn.rb create mode 100755 start.sh diff --git a/.gitignore b/.gitignore index fbfb620..430bb02 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,6 @@ config/deploy.rb config/deploy config/mongoid.yml config/newrelic.yml -config/unicorn.rb .rvmrc .idea *~ diff --git a/ambiente.sh b/ambiente.sh new file mode 100755 index 0000000..e69de29 --- /dev/null +++ b/ambiente.sh diff --git a/config/unicorn.default.rb b/config/unicorn.default.rb index b97ef26..a2d0b98 100644 --- a/config/unicorn.default.rb +++ b/config/unicorn.default.rb @@ -1,10 +1,12 @@ # http://michaelvanrooijen.com/articles/2011/06/01-more-concurrency-on-a-single-heroku-dyno-with-the-new-celadon-cedar-stack/ -worker_processes 3 # amount of unicorn workers to spin up +worker_processes ENV['ERRBIT_UNICORN_WORKER_PROCESSES'] # amount of unicorn workers to spin up timeout 30 # restarts workers that hang for 30 seconds preload_app true -listen ENV['PORT'] || 8080 -pid ENV['UNICORN_PID'] if ENV['UNICORN_PID'] + +listen "${ENV['ERRBIT_UNICORN_BIND_ADDRESS'}:${ENV['ERRBIT_UNICORN_PORT']}" + +pid ENV['ERRBIT_UNICORN_PID'] # Taken from github: https://github.com/blog/517-unicorn # Though everyone uses pretty miuch the same code @@ -19,6 +21,8 @@ before_fork do |server, _worker| # we send it a QUIT. # # Using this method we get 0 downtime deploys. + ActiveRecord::Base.connection.disconnect! if defined?(ActiveRecord::Base) + old_pid = "#{server.config[:pid]}.oldbin" if File.exist?(old_pid) && server.pid != old_pid @@ -29,3 +33,9 @@ before_fork do |server, _worker| end end end + + +after_fork do |server, worker| + ActiveRecord::Base.establish_connection if defined?(ActiveRecord::Base) +end + diff --git a/config/unicorn.rb b/config/unicorn.rb new file mode 100644 index 0000000..a2d0b98 --- /dev/null +++ b/config/unicorn.rb @@ -0,0 +1,41 @@ +# http://michaelvanrooijen.com/articles/2011/06/01-more-concurrency-on-a-single-heroku-dyno-with-the-new-celadon-cedar-stack/ + +worker_processes ENV['ERRBIT_UNICORN_WORKER_PROCESSES'] # amount of unicorn workers to spin up +timeout 30 # restarts workers that hang for 30 seconds +preload_app true + +listen "${ENV['ERRBIT_UNICORN_BIND_ADDRESS'}:${ENV['ERRBIT_UNICORN_PORT']}" + +pid ENV['ERRBIT_UNICORN_PID'] + +# Taken from github: https://github.com/blog/517-unicorn +# Though everyone uses pretty miuch the same code +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. + ActiveRecord::Base.connection.disconnect! if defined?(ActiveRecord::Base) + + + old_pid = "#{server.config[:pid]}.oldbin" + if File.exist?(old_pid) && server.pid != old_pid + begin + Process.kill("QUIT", File.read(old_pid).to_i) + rescue Errno::ENOENT, Errno::ESRCH + warn "Unicorn: master process already killed, no problem" + end + end +end + + +after_fork do |server, worker| + ActiveRecord::Base.establish_connection if defined?(ActiveRecord::Base) +end + diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..50b3a6e --- /dev/null +++ b/start.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# SCRIPT DE INICIALIZAÇÃO DO ERRBIT +# DEVE ser chamado este script com as variável de ambiente ERRBIT_CONF_PATH e ERRBIT_APP_PATH sendo definida s +# +# exemplo: +# +# ERRBIT_CONF_PATH=/opt/appconf/errbit/ /opt/websites/errbit/start.sh +# +# Em ERRBIT_CONF_PATH deve ser colocado um unicorn.rb que pode ser criado a partir exemplo em config/unicorn.default.rb +echo "Starting Errbit" + +echo "ERRBIT_CONF_PATH=$ERRBIT_CONF_PATH" + +if [ ! -d $ERRBIT_CONF_PATH ]; then + echo "ERRBIT_CONF_PATH não definido ou diretório $ERRBIT_CONF_PATH não encontrado"; + exit 1; +fi + +# default variable +export ERRBIT_UNICORN_BIND_ADDRESS=127.0.0.1 +export ERRBIT_UNICORN_PID=errbit_unicorn.pid +export ERRBIT_UNICORN_PORT=4040 +export ERRBIT_UNICORN_WORKER_PROCESSES=1 + +# ambiente.sh deve sobrescrever exportando as seguintes variáveis ERRBIT_UNICORN_PID, ERRBIT_UNICORN_PORT, ERRBIT_UNICORN_WORKER_PROCESSES + +if [ -f $ERRBIT_CONF_PATH/ambiente.sh ]; then + source $ERRBIT_CONF_PATH/ambiente.sh +fi + +unicorn -c $ERRBIT_CONF_PATH/conf/unicorn.rb -- libgit2 0.21.2