Skip to content

Commit 7adf6ca

Browse files
committed
Add unicorn
1 parent dc41520 commit 7adf6ca

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

Capfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require "capistrano/bundler"
88
require "capistrano/rails"
99
require "capistrano/sidekiq"
1010
require "whenever/capistrano"
11+
require "capistrano3/unicorn"
1112

1213
# Load the SCM plugin appropriate to your project:
1314
#

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ gem 'skim'
5656
gem 'slim-rails'
5757
gem 'therubyracer'
5858
gem 'thinking-sphinx'
59+
gem 'unicorn'
5960
gem 'whenever'
6061

6162
group :production do
@@ -64,6 +65,7 @@ group :production do
6465
gem 'capistrano-rails', require: false
6566
gem 'capistrano-rvm', require: false
6667
gem 'capistrano-sidekiq', require: false
68+
gem 'capistrano3-unicorn', require: false
6769
end
6870

6971
group :development, :test do

Gemfile.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ GEM
7878
capistrano-sidekiq (1.0.2)
7979
capistrano (>= 3.9.0)
8080
sidekiq (>= 3.4)
81+
capistrano3-unicorn (0.2.1)
82+
capistrano (~> 3.1, >= 3.1.0)
8183
capybara (3.0.2)
8284
addressable
8385
mini_mime (>= 0.1.3)
@@ -152,6 +154,7 @@ GEM
152154
rspec (>= 2.0, < 4.0)
153155
jsonapi-renderer (0.2.0)
154156
jwt (1.5.6)
157+
kgio (2.11.2)
155158
launchy (2.4.3)
156159
addressable (~> 2.3)
157160
libv8 (3.16.14.19)
@@ -247,6 +250,7 @@ GEM
247250
method_source
248251
rake (>= 0.8.7)
249252
thor (>= 0.18.1, < 2.0)
253+
raindrops (0.19.0)
250254
rake (12.3.1)
251255
rb-fsevent (0.10.3)
252256
rb-inotify (0.9.10)
@@ -351,6 +355,9 @@ GEM
351355
thread_safe (~> 0.1)
352356
uglifier (4.1.9)
353357
execjs (>= 0.3.0, < 3)
358+
unicorn (5.4.1)
359+
kgio (~> 2.6)
360+
raindrops (~> 0.7)
354361
warden (1.2.7)
355362
rack (>= 1.0)
356363
web-console (3.6.1)
@@ -379,6 +386,7 @@ DEPENDENCIES
379386
capistrano-rails
380387
capistrano-rvm
381388
capistrano-sidekiq
389+
capistrano3-unicorn
382390
capybara (>= 2.15, < 4.0)
383391
carrierwave
384392
chromedriver-helper
@@ -420,6 +428,7 @@ DEPENDENCIES
420428
turbolinks (~> 5)
421429
tzinfo-data
422430
uglifier (>= 1.3.0)
431+
unicorn
423432
web-console (>= 3.3.0)
424433
whenever
425434

config/deploy.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
desc "Restart application"
3232
task :restart do
3333
on roles(:app), in: :sequence, wait: 5 do
34-
execute :touch, release_path.join("tmp/restart.txt")
34+
# execute :touch, release_path.join("tmp/restart.txt")
35+
invoke 'unicorn:restart'
3536
end
3637
end
3738

config/unicorn/production.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# paths
2+
app_path = "/home/deployer/qna"
3+
working_directory "#{app_path}/current"
4+
pid "#{app_path}/current/tmp/pids/unicorn.pid"
5+
6+
# listen
7+
listen "/tmp/unicorn.qna.sock", backlog: 64
8+
9+
# logging
10+
stderr_path "log/unicorn.stderr.log"
11+
stdout_path "log/unicorn.stdout.log"
12+
13+
# workers
14+
worker_processes 2
15+
16+
# use correct Gemfile on restarts
17+
before_exec do |server|
18+
ENV['BUNDLE_GEMFILE'] = "#{app_path}/current/Gemfile"
19+
end
20+
21+
# preload
22+
preload_app true
23+
24+
before_fork do |server, worker|
25+
# the following is highly recomended for Rails + "preload_app true"
26+
# as there's no need for the master process to hold a connection
27+
if defined?(ActiveRecord::Base)
28+
ActiveRecord::Base.connection.disconnect!
29+
end
30+
31+
# Before forking, kill the master process that belongs to the .oldbin PID.
32+
# This enables 0 downtime deploys.
33+
old_pid = "#{server.config[:pid]}.oldbin"
34+
if File.exists?(old_pid) && server.pid != old_pid
35+
begin
36+
Process.kill("QUIT", File.read(old_pid).to_i)
37+
rescue Errno::ENOENT, Errno::ESRCH
38+
# someone else did our job for us
39+
end
40+
end
41+
end
42+
43+
after_fork do |server, worker|
44+
if defined?(ActiveRecord::Base)
45+
ActiveRecord::Base.establish_connection
46+
end
47+
end

0 commit comments

Comments
 (0)