From d201c966cf671059dc63ba36e022087942f362bb Mon Sep 17 00:00:00 2001 From: Robert Fletcher Date: Sat, 1 May 2021 10:52:17 -0700 Subject: [PATCH] Deps: switch from Unicorn to Puma **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/ --- .rubocop.yml | 3 +++ .rubocop_todo.yml | 1 - Gemfile | 7 ++----- Gemfile.lock | 10 ++++------ Procfile | 2 +- config/{unicorn.rb => puma.rb} | 12 ++++++------ docker/supervisord.conf | 4 ++-- spec/spec_helper.rb | 2 +- 8 files changed, 19 insertions(+), 22 deletions(-) rename config/{unicorn.rb => puma.rb} (80%) diff --git a/.rubocop.yml b/.rubocop.yml index a725a457d..7516f6ba1 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -33,6 +33,9 @@ Style/Documentation: Style/DoubleNegation: Enabled: false +Style/MissingElse: + Enabled: false + Style/NumericLiterals: Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e521faa0d..02a1b540a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 diff --git a/Gemfile b/Gemfile index 59402bc57..5e9b686a8 100644 --- a/Gemfile +++ b/Gemfile @@ -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 @@ -37,6 +32,8 @@ gem "httparty" gem "i18n" gem "loofah" gem "nokogiri" +gem "pg" +gem "puma" gem "rack-protection" gem "racksh" gem "rack-ssl" diff --git a/Gemfile.lock b/Gemfile.lock index 3885d478e..6f30d4374 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -218,6 +216,7 @@ DEPENDENCIES nokogiri pg pry-byebug + puma rack-protection rack-ssl rack-test @@ -241,7 +240,6 @@ DEPENDENCIES thread timecop uglifier - unicorn will_paginate RUBY VERSION diff --git a/Procfile b/Procfile index dfa2ae6e9..cceb33ddf 100644 --- a/Procfile +++ b/Procfile @@ -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 diff --git a/config/unicorn.rb b/config/puma.rb similarity index 80% rename from config/unicorn.rb rename to config/puma.rb index 5b139c6bd..c316b384f 100644 --- a/config/unicorn.rb +++ b/config/puma.rb @@ -1,10 +1,10 @@ -worker_processes 1 -timeout 30 -preload_app true +workers 1 +worker_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) @@ -14,7 +14,7 @@ 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] @@ -22,6 +22,6 @@ 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 diff --git a/docker/supervisord.conf b/docker/supervisord.conf index 61f5b300d..12c76be16 100644 --- a/docker/supervisord.conf +++ b/docker/supervisord.conf @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b2b783241..8c57bddd5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,7 +15,7 @@ require "./app" -Capybara.server = :webrick +Capybara.server = :puma, { Silent: true } RSpec.configure do |config| config.include Rack::Test::Methods