Skip to content

Commit

Permalink
Deps: switch from Unicorn to Puma
Browse files Browse the repository at this point in the history
**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/
  • Loading branch information
mockdeep committed May 1, 2021
1 parent 72bd2a3 commit 9e720ba
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Style/Documentation:
Style/DoubleNegation:
Enabled: false

Style/MissingElse:
Enabled: false

Style/NumericLiterals:
Enabled: false

Expand Down
1 change: 0 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@ Style/MissingElse:
- 'app/fever_api/write_mark_item.rb'
- 'app/helpers/url_helpers.rb'
- 'app/repositories/story_repository.rb'
- 'config/unicorn.rb'
- 'spec/support/coverage.rb'

# Offense count: 1
Expand Down
7 changes: 2 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ ruby_version_file = File.expand_path(".ruby-version", __dir__)
ruby File.read(ruby_version_file).chomp if File.readable?(ruby_version_file)
source "https://rubygems.org"

group :production do
gem "pg"
gem "unicorn"
end

group :development do
gem "rubocop", require: false
gem "rubocop-rails", require: false
Expand Down Expand Up @@ -37,6 +32,8 @@ gem "httparty"
gem "i18n"
gem "loofah"
gem "nokogiri"
gem "pg"
gem "puma"
gem "rack-protection"
gem "racksh"
gem "rack-ssl"
Expand Down
10 changes: 4 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ GEM
multi_xml (>= 0.5.2)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
kgio (2.11.3)
loofah (2.9.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand All @@ -70,6 +69,7 @@ GEM
multi_xml (0.6.0)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
nio4r (2.5.7)
nokogiri (1.11.3)
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
Expand All @@ -85,6 +85,8 @@ GEM
byebug (~> 11.0)
pry (~> 0.13.0)
public_suffix (4.0.6)
puma (5.2.2)
nio4r (~> 2.0)
racc (1.5.2)
rack (2.2.3)
rack-protection (2.1.0)
Expand All @@ -97,7 +99,6 @@ GEM
rack (>= 1.0)
rack-test (>= 0.5)
rainbow (3.0.0)
raindrops (0.19.1)
rake (13.0.3)
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
Expand Down Expand Up @@ -191,9 +192,6 @@ GEM
uglifier (4.2.0)
execjs (>= 0.3.0, < 3)
unicode-display_width (2.0.0)
unicorn (6.0.0)
kgio (~> 2.6)
raindrops (~> 0.7)
will_paginate (3.3.0)
xpath (3.2.0)
nokogiri (~> 1.8)
Expand All @@ -218,6 +216,7 @@ DEPENDENCIES
nokogiri
pg
pry-byebug
puma
rack-protection
rack-ssl
rack-test
Expand All @@ -241,7 +240,6 @@ DEPENDENCIES
thread
timecop
uglifier
unicorn
will_paginate

RUBY VERSION
Expand Down
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
web: bundle exec puma -p $PORT -C ./config/puma.rb
console: bundle exec racksh
20 changes: 14 additions & 6 deletions config/unicorn.rb → config/puma.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
worker_processes 1
timeout 30
preload_app true
workers workers Integer(ENV.fetch("WEB_CONCURRENCY", 1))
threads_count = Integer(ENV.fetch("MAX_THREADS", 2))
threads threads_count, threads_count

rackup DefaultRackup
port ENV.fetch("PORT", 3000)
environment ENV.fetch("RACK_ENV", "development")

worker_timeout Integer(ENV.fetch("PUMA_WORKER_TIMEOUT", 25))
worker_shutdown_timeout Integer(ENV.fetch("PUMA_WORKER_SHUTDOWN_TIMEOUT", 25))
preload_app!

@delayed_job_pid = nil

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

after_fork do |_server, _worker|
on_worker_boot do
if defined?(ActiveRecord::Base)
env = ENV["RACK_ENV"] || "development"
config = YAML.safe_load(ERB.new(File.read("config/database.yml")).result)[env]
ActiveRecord::Base.establish_connection(config)
end
end

after_worker_exit do |_server, _worker, _status|
on_worker_shutdown do
Process.kill("QUIT", @delayed_job_pid) if !ENV["RACK_ENV"] || ENV["RACK_ENV"] == "development"
end
4 changes: 2 additions & 2 deletions docker/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
nodaemon=true
loglevel=debug

[program:unicorn]
command=bash -c 'bundle exec rake db:migrate && bundle exec unicorn -p $PORT -c ./config/unicorn.rb'
[program:puma]
command=bash -c 'bundle exec rake db:migrate && bundle exec puma -p $PORT -C ./config/puma.rb'
directory=/app
autostart=true
autorestart=true
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

require "./app"

Capybara.server = :webrick
Capybara.server = :puma, { Silent: true }

RSpec.configure do |config|
config.include Rack::Test::Methods
Expand Down

0 comments on commit 9e720ba

Please sign in to comment.