From 71dfbd93f4f1bc4ae443c66bb10fe85a36781367 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Tue, 5 Apr 2016 05:33:43 +0200 Subject: [PATCH] Bundle unicorn for production deployments. --- Gemfile | 2 +- Gemfile.lock | 7 +++++++ config/unicorn.rb | 24 ++++++++++++++++++++++++ run_unicorn.md | 23 +++++++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 config/unicorn.rb create mode 100644 run_unicorn.md diff --git a/Gemfile b/Gemfile index bb983b0..04208a2 100644 --- a/Gemfile +++ b/Gemfile @@ -30,7 +30,7 @@ gem 'sdoc', '~> 0.4.0', group: :doc # gem 'bcrypt', '~> 3.1.7' # Use Unicorn as the app server -# gem 'unicorn' +gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index b2a7569..c878053 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,6 +60,7 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (1.8.3) + kgio (2.10.0) loofah (2.0.3) nokogiri (>= 1.5.9) mail (2.6.4) @@ -97,6 +98,7 @@ GEM activesupport (= 4.2.6) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) + raindrops (0.16.0) rake (11.1.2) rdoc (4.2.2) json (~> 1.4) @@ -123,6 +125,10 @@ GEM thread_safe (~> 0.1) uglifier (3.0.0) execjs (>= 0.3.0, < 3) + unicorn (5.0.1) + kgio (~> 2.6) + rack + raindrops (~> 0.7) web-console (2.3.0) activemodel (>= 4.0) binding_of_caller (>= 0.7.2) @@ -147,6 +153,7 @@ DEPENDENCIES tilt (~> 2.0) turbolinks uglifier (>= 1.3.0) + unicorn web-console (~> 2.0) BUNDLED WITH diff --git a/config/unicorn.rb b/config/unicorn.rb new file mode 100644 index 0000000..25c6a37 --- /dev/null +++ b/config/unicorn.rb @@ -0,0 +1,24 @@ +# config/unicorn.rb +worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3) +timeout 15 +preload_app true + +before_fork do |server, worker| + Signal.trap 'TERM' do + puts 'Unicorn master intercepting TERM and sending myself QUIT instead' + Process.kill 'QUIT', Process.pid + end + + defined?(ActiveRecord::Base) and + ActiveRecord::Base.connection.disconnect! +end + +after_fork do |server, worker| + Signal.trap 'TERM' do + puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT' + end + + defined?(ActiveRecord::Base) and + ActiveRecord::Base.establish_connection +end +# vim: set ts=2 sw=2: diff --git a/run_unicorn.md b/run_unicorn.md new file mode 100644 index 0000000..1650fa6 --- /dev/null +++ b/run_unicorn.md @@ -0,0 +1,23 @@ +# Some notes about unicorn + +First off, it only servers the rails application, not the static assets. +You need to have another webserver for this. + +## SECRET_KEY + +By default rails reads the secret key from the environment variable +`SECRET_KEY_BASE`. It is possible to create a key with `rake secret` + +## Start unicorn + + SECRET_KEY_BASE="fa27f9d1cdfeb8590377366cb89c973b6084a20b60a4e60b551563cb1119156a0f172212f54fc096e182bf5310b6ca8cb050814c5908c8523cb191d1a054ca29" bundle exec unicorn -c config/unicorn.rb -E production + +## Precompile assets + + RAILS_ENV=production bundle exec rake assets:precompile + +## Urls + + * [heroku:Deploying Rails Applications with Unicorn](https://devcenter.heroku.com/articles/rails-unicorn) + * [DigitalOcean:How To Deploy a Rails App with Unicorn and Nginx on Ubuntu 14.04](https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-unicorn-and-nginx-on-ubuntu-14-04) +