Skip to content

Commit f39015f

Browse files
authored
Deps: switch from Unicorn to Puma (#570)
**What** This switches the server we use from Unicorn to Puma **Why** * Puma is the blessed option [on Heroku][heroku]. * It [works with Capybara][capybara] out of the box. * Ruby 3 [removes `webrick`][ruby3] from the core libraries, so we would need to add it as a dependency for Capybara when upgrading. Might as well consolidate on a single server. **Notes** I also moved `pg` out of the `production` group. [heroku]: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#recommended-default-puma-process-and-thread-configuration [capybara]: https://github.com/teamcapybara/capybara#drivers [ruby3]: https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/
1 parent 76917d3 commit f39015f

File tree

8 files changed

+28
-23
lines changed

8 files changed

+28
-23
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Style/Documentation:
3333
Style/DoubleNegation:
3434
Enabled: false
3535

36+
Style/MissingElse:
37+
Enabled: false
38+
3639
Style/NumericLiterals:
3740
Enabled: false
3841

.rubocop_todo.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,6 @@ Style/MissingElse:
551551
- 'app/fever_api/write_mark_item.rb'
552552
- 'app/helpers/url_helpers.rb'
553553
- 'app/repositories/story_repository.rb'
554-
- 'config/unicorn.rb'
555554
- 'spec/support/coverage.rb'
556555

557556
# Offense count: 1

Gemfile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ ruby_version_file = File.expand_path(".ruby-version", __dir__)
22
ruby File.read(ruby_version_file).chomp if File.readable?(ruby_version_file)
33
source "https://rubygems.org"
44

5-
group :production do
6-
gem "pg"
7-
gem "unicorn"
8-
end
9-
105
group :development do
116
gem "rubocop", require: false
127
gem "rubocop-rails", require: false
@@ -37,6 +32,8 @@ gem "httparty"
3732
gem "i18n"
3833
gem "loofah"
3934
gem "nokogiri"
35+
gem "pg"
36+
gem "puma"
4037
gem "rack-protection"
4138
gem "racksh"
4239
gem "rack-ssl"

Gemfile.lock

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ GEM
5656
multi_xml (>= 0.5.2)
5757
i18n (1.8.11)
5858
concurrent-ruby (~> 1.0)
59-
kgio (2.11.4)
6059
loofah (2.13.0)
6160
crass (~> 1.0.2)
6261
nokogiri (>= 1.5.9)
@@ -72,6 +71,7 @@ GEM
7271
multi_xml (0.6.0)
7372
mustermann (1.1.1)
7473
ruby2_keywords (~> 0.0.1)
74+
nio4r (2.5.8)
7575
nokogiri (1.12.5)
7676
mini_portile2 (~> 2.6.1)
7777
racc (~> 1.4)
@@ -87,6 +87,8 @@ GEM
8787
byebug (~> 11.0)
8888
pry (~> 0.13.0)
8989
public_suffix (4.0.6)
90+
puma (5.5.2)
91+
nio4r (~> 2.0)
9092
racc (1.6.0)
9193
rack (2.2.3)
9294
rack-protection (2.1.0)
@@ -99,7 +101,6 @@ GEM
99101
rack (>= 1.0)
100102
rack-test (>= 0.5)
101103
rainbow (3.0.0)
102-
raindrops (0.20.0)
103104
rake (13.0.6)
104105
rb-fsevent (0.11.0)
105106
rb-inotify (0.10.1)
@@ -192,9 +193,6 @@ GEM
192193
uglifier (4.2.0)
193194
execjs (>= 0.3.0, < 3)
194195
unicode-display_width (2.1.0)
195-
unicorn (6.0.0)
196-
kgio (~> 2.6)
197-
raindrops (~> 0.7)
198196
will_paginate (3.3.1)
199197
xpath (3.2.0)
200198
nokogiri (~> 1.8)
@@ -219,6 +217,7 @@ DEPENDENCIES
219217
nokogiri
220218
pg
221219
pry-byebug
220+
puma
222221
rack-protection
223222
rack-ssl
224223
rack-test
@@ -242,11 +241,10 @@ DEPENDENCIES
242241
thread
243242
timecop
244243
uglifier
245-
unicorn
246244
will_paginate
247245

248246
RUBY VERSION
249247
ruby 2.7.5
250248

251249
BUNDLED WITH
252-
2.2.12
250+
2.2.33

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
1+
web: bundle exec puma -p $PORT -C ./config/puma.rb
22
console: bundle exec racksh
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
worker_processes 1
2-
timeout 30
3-
preload_app true
1+
workers workers Integer(ENV.fetch("WEB_CONCURRENCY", 1))
2+
threads_count = Integer(ENV.fetch("MAX_THREADS", 2))
3+
threads threads_count, threads_count
4+
5+
rackup DefaultRackup
6+
port ENV.fetch("PORT", 3000)
7+
environment ENV.fetch("RACK_ENV", "development")
8+
9+
worker_timeout Integer(ENV.fetch("PUMA_WORKER_TIMEOUT", 25))
10+
worker_shutdown_timeout Integer(ENV.fetch("PUMA_WORKER_SHUTDOWN_TIMEOUT", 25))
11+
preload_app!
412

513
@delayed_job_pid = nil
614

7-
before_fork do |_server, _worker|
15+
before_fork do
816
# the following is highly recommended for Rails + "preload_app true"
917
# as there's no need for the master process to hold a connection
1018
ActiveRecord::Base.connection.disconnect! if defined?(ActiveRecord::Base)
@@ -14,14 +22,14 @@
1422
sleep 1
1523
end
1624

17-
after_fork do |_server, _worker|
25+
on_worker_boot do
1826
if defined?(ActiveRecord::Base)
1927
env = ENV["RACK_ENV"] || "development"
2028
config = YAML.safe_load(ERB.new(File.read("config/database.yml")).result)[env]
2129
ActiveRecord::Base.establish_connection(config)
2230
end
2331
end
2432

25-
after_worker_exit do |_server, _worker, _status|
33+
on_worker_shutdown do
2634
Process.kill("QUIT", @delayed_job_pid) if !ENV["RACK_ENV"] || ENV["RACK_ENV"] == "development"
2735
end

docker/supervisord.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
nodaemon=true
33
loglevel=debug
44

5-
[program:unicorn]
6-
command=bash -c 'bundle exec rake db:migrate && bundle exec unicorn -p $PORT -c ./config/unicorn.rb'
5+
[program:puma]
6+
command=bash -c 'bundle exec rake db:migrate && bundle exec puma -p $PORT -C ./config/puma.rb'
77
directory=/app
88
autostart=true
99
autorestart=true

spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
require "./app"
1717

18-
Capybara.server = :webrick
18+
Capybara.server = :puma, { Silent: true }
1919

2020
RSpec.configure do |config|
2121
config.include Rack::Test::Methods

0 commit comments

Comments
 (0)