Permalink
Browse files

Setup Production configurations with Unicorn and Postgresql

  • Loading branch information...
yoosee committed Mar 9, 2016
1 parent bca240e commit 5555287e12eea79a323fdcb3851bdec50a1fc2ca
Showing with 88 additions and 2 deletions.
  1. +4 −1 Gemfile
  2. +9 −0 Gemfile.lock
  3. +8 −1 config/database.yml
  4. +29 −0 config/unicorn.rb
  5. +38 −0 lib/tasks/unicorn.rake
View
@@ -7,6 +7,8 @@ gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
gem 'unicorn'
gem 'bootstrap-sass' #, '3.2.0.0'
gem 'sprockets'
@@ -63,6 +65,7 @@ group :development do
end
group :production do
# gem 'pg'
gem 'pg'
gem 'therubyracer', platforms: :ruby
# gem 'rails_12factor'
end
View
@@ -86,6 +86,7 @@ GEM
jquery-ui-rails (5.0.5)
railties (>= 3.2.16)
json (1.8.3)
kgio (2.10.0)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.3)
@@ -103,6 +104,7 @@ GEM
cocaine (~> 0.5.5)
mime-types
mimemagic (= 0.3.0)
pg (0.18.4)
posix-spawn (0.3.11)
quiet_assets (1.1.0)
railties (>= 3.1, < 5.0)
@@ -134,6 +136,7 @@ GEM
activesupport (= 4.2.2)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
raindrops (0.16.0)
rake (10.5.0)
rdoc (4.2.2)
json (~> 1.4)
@@ -167,6 +170,10 @@ GEM
uglifier (2.7.2)
execjs (>= 0.3.0)
json (>= 1.8.0)
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)
@@ -191,6 +198,7 @@ DEPENDENCIES
jquery-ui-rails
nokogiri
paperclip (~> 4.3)
pg
quiet_assets
rails (= 4.2.2)
rails_layout
@@ -202,5 +210,6 @@ DEPENDENCIES
sqlite3
turbolinks
uglifier (>= 1.3.0)
unicorn
web-console (~> 2.0)
will_paginate
View
@@ -22,4 +22,11 @@ test:
production:
<<: *default
database: db/production.sqlite3
# database: db/production.sqlite3
adapter: postgresql
encoding: unicode
database: textfi
pool: 5
username: <%= ENV['RAILS_PGSQL_USER'] || 'textfi' %>
password: <%= ENV['RAILS_PGSQL_PASSWORD'] %>
# database: db/production.sqlite3
View
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
#
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true
listen "/var/tmp/unicorn.sock"
pid "/var/tmp/unicorn.pid"
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
stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])
stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])
View
@@ -0,0 +1,38 @@
namespace :unicorn do
desc "Start unicorn for #{ENV['RAILS_ENV']} env."
task(:start) {
config = Rails.root.join('config', 'unicorn.rb')
sh "bundle exec unicorn_rails -c #{config} -E #{ENV['RAILS_ENV']} -D"
}
desc "Stop unicorn"
task(:stop) { unicorn_signal :QUIT }
desc "Restart unicorn with USR2"
task(:restart) { unicorn_signal :USR2 }
desc "Increment number of worker processes"
task(:increment) { unicorn_signal :TTIN }
desc "Decrement number of worker processes"
task(:decrement) { unicorn_signal :TTOU }
desc "Unicorn pstree (depents on pstree command)"
task(:pstree) do
sh "pstree '#{unicorn_pid}'"
end
def unicorn_signal signal
Process.kill signal, unicorn_pid
end
def unicorn_pid
begin
File.read("/var/tmp/unicorn.pid").to_i
rescue Errno::ENOENT
raise "Unicorn doesn't seem to be running or PID file doesn't exist"
end
end
end

0 comments on commit 5555287

Please sign in to comment.