From 6c33481b6bbec1a9b3bb6becfccb400c5dc969b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Amo=C3=AAdo?= Date: Tue, 18 Apr 2017 13:29:00 -0300 Subject: [PATCH 1/3] Replace Thin for Puma MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lucas Amoêdo Signed-off-by: Marcelo Oliveira --- Gemfile | 5 +++-- Gemfile.lock | 11 +++-------- Procfile | 2 +- config/initializers/konacha.rb | 22 ---------------------- config/puma.rb | 15 +++++++++++++++ 5 files changed, 22 insertions(+), 33 deletions(-) create mode 100644 config/puma.rb diff --git a/Gemfile b/Gemfile index e2a648b0..c51fcb16 100644 --- a/Gemfile +++ b/Gemfile @@ -67,6 +67,9 @@ gem 'http_accept_language' # Routes for JS files gem 'js-routes', '~> 1.1.0' +# Puma server +gem 'puma' + group :test do # Easier test writing gem "shoulda-matchers", '~> 2.8.0' @@ -80,8 +83,6 @@ group :test do # Test coverage report gem 'codeclimate-test-reporter', require: nil - # For Konacha - gem 'thin' end # Startup script generation (server process manager) diff --git a/Gemfile.lock b/Gemfile.lock index 0aba83b9..5093e216 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -93,7 +93,6 @@ GEM mime-types (>= 1.16, < 3) nokogiri (~> 1.5) rails (>= 3, < 5) - daemons (1.2.3) dalli (2.7.4) database_cleaner (1.5.0) debug_inspector (0.0.2) @@ -109,7 +108,6 @@ GEM domain_name (0.5.24) unf (>= 0.0.5, < 1.0.0) erubis (2.7.0) - eventmachine (1.0.8) exception_notification (4.1.1) actionmailer (>= 3.0.4) activesupport (>= 3.0.4) @@ -216,6 +214,7 @@ GEM cliver (~> 0.3.1) multi_json (~> 1.0) websocket-driver (>= 0.2.0) + puma (3.8.2) rack (1.6.4) rack-test (0.6.3) rack (>= 1.0) @@ -302,10 +301,6 @@ GEM therubyracer (0.12.2) libv8 (~> 3.16.14.0) ref - thin (1.6.4) - daemons (~> 1.0, >= 1.0.9) - eventmachine (~> 1.0, >= 1.0.4) - rack (~> 1.0) thor (0.19.1) thread_safe (0.3.5) tilt (2.0.1) @@ -371,6 +366,7 @@ DEPENDENCIES mocha pg (~> 0.18.1) poltergeist (~> 1.7.0) + puma rails (= 4.2.4) rails-html-sanitizer (~> 1.0) railsstrap (~> 3.3.4) @@ -384,10 +380,9 @@ DEPENDENCIES sprockets sqlite3 therubyracer - thin turbolinks uglifier (>= 1.3.0) web-console (~> 2.0.0) BUNDLED WITH - 1.11.2 + 1.14.6 diff --git a/Procfile b/Procfile index e905ce60..528ca4bf 100644 --- a/Procfile +++ b/Procfile @@ -1 +1 @@ -web: bundle exec rails s -p $PORT -e $RAILS_ENV \ No newline at end of file +web: bundle exec puma -C config/puma.rb \ No newline at end of file diff --git a/config/initializers/konacha.rb b/config/initializers/konacha.rb index 91ccef7a..5a656791 100644 --- a/config/initializers/konacha.rb +++ b/config/initializers/konacha.rb @@ -7,26 +7,4 @@ config.stylesheets = %w(application) config.driver = :poltergeist end - - # Use thin to run Konacha tests. This is needed because the tests hang frequently in Travis using the default (WEBRick) - # We can't just do 'Capybara.server' in the configure block because it will also apply to anything else run by - # Capybara. So instead, override the Konacha.run method to change the server, and restore it after completion. - module Konacha - class << self - old_run = instance_method(:run) - - define_method(:run) do - prev_server = Capybara.server - begin - Capybara.server do |app, port| - require 'rack/handler/thin' - Rack::Handler::Thin.run(app, :Port => port) - end - old_run.bind(self).call - ensure - Capybara.server(&prev_server) - end - end - end - end end diff --git a/config/puma.rb b/config/puma.rb new file mode 100644 index 00000000..fee2a65e --- /dev/null +++ b/config/puma.rb @@ -0,0 +1,15 @@ +workers Integer(ENV['WEB_CONCURRENCY'] || 2) +threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5) +threads threads_count, threads_count + +preload_app! + +rackup DefaultRackup +port ENV['PORT'] || 3000 +environment ENV['RACK_ENV'] || 'development' + +on_worker_boot do + # Worker specific setup for Rails 4.1+ + # See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot + ActiveRecord::Base.establish_connection +end \ No newline at end of file From efe688b7a00f6d45e20669692f3173dc849664f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Amo=C3=AAdo?= Date: Tue, 18 Apr 2017 15:41:07 -0300 Subject: [PATCH 2/3] Override Rack::Handler default server MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lucas Amoêdo Signed-off-by: Marcelo Oliveira --- Gemfile | 6 +++++- Gemfile.lock | 7 +++++++ config/application.rb | 3 +++ config/initializers/konacha.rb | 22 ++++++++++++++++++++++ config/initializers/rack.rb | 0 lib/core_extensions/rack/handler.rb | 15 +++++++++++++++ 6 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 config/initializers/rack.rb create mode 100644 lib/core_extensions/rack/handler.rb diff --git a/Gemfile b/Gemfile index c51fcb16..8231da2a 100644 --- a/Gemfile +++ b/Gemfile @@ -67,10 +67,14 @@ gem 'http_accept_language' # Routes for JS files gem 'js-routes', '~> 1.1.0' -# Puma server +# Use Puma webserver for the application. gem 'puma' group :test do + + # Use Thin webserver for konacha tests. + gem 'thin' + # Easier test writing gem "shoulda-matchers", '~> 2.8.0' diff --git a/Gemfile.lock b/Gemfile.lock index 5093e216..32119b05 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -93,6 +93,7 @@ GEM mime-types (>= 1.16, < 3) nokogiri (~> 1.5) rails (>= 3, < 5) + daemons (1.2.3) dalli (2.7.4) database_cleaner (1.5.0) debug_inspector (0.0.2) @@ -108,6 +109,7 @@ GEM domain_name (0.5.24) unf (>= 0.0.5, < 1.0.0) erubis (2.7.0) + eventmachine (1.0.8) exception_notification (4.1.1) actionmailer (>= 3.0.4) activesupport (>= 3.0.4) @@ -301,6 +303,10 @@ GEM therubyracer (0.12.2) libv8 (~> 3.16.14.0) ref + thin (1.6.4) + daemons (~> 1.0, >= 1.0.9) + eventmachine (~> 1.0, >= 1.0.4) + rack (~> 1.0) thor (0.19.1) thread_safe (0.3.5) tilt (2.0.1) @@ -380,6 +386,7 @@ DEPENDENCIES sprockets sqlite3 therubyracer + thin turbolinks uglifier (>= 1.3.0) web-console (~> 2.0.0) diff --git a/config/application.rb b/config/application.rb index 7d9b0b4d..a1b5ef41 100644 --- a/config/application.rb +++ b/config/application.rb @@ -23,5 +23,8 @@ class Application < Rails::Application # Do not swallow errors in after_commit/after_rollback callbacks. config.active_record.raise_in_transactional_callbacks = true + + require "#{Rails.root}/lib/core_extensions/rack/handler" + end end diff --git a/config/initializers/konacha.rb b/config/initializers/konacha.rb index 5a656791..c2c8f862 100644 --- a/config/initializers/konacha.rb +++ b/config/initializers/konacha.rb @@ -7,4 +7,26 @@ config.stylesheets = %w(application) config.driver = :poltergeist end + + # Use Thin to run Konacha tests. This is needed because the tests hang frequently in Travis using the default (WEBRick) + # We can't just do 'Capybara.server' in the configure block because it will also apply to anything else run by + # Capybara. So instead, override the Konacha.run method to change the server, and restore it after completion. + module Konacha + class << self + old_run = instance_method(:run) + + define_method(:run) do + prev_server = Capybara.server + begin + Capybara.server do |app, port| + require 'rack/handler/thin' + Rack::Handler::Thin.run(app, :Port => port) + end + old_run.bind(self).call + ensure + Capybara.server(&prev_server) + end + end + end + end end diff --git a/config/initializers/rack.rb b/config/initializers/rack.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/core_extensions/rack/handler.rb b/lib/core_extensions/rack/handler.rb new file mode 100644 index 00000000..38c30a99 --- /dev/null +++ b/lib/core_extensions/rack/handler.rb @@ -0,0 +1,15 @@ +# Monkey Patch for Rack to choose Puma as the default webserver. +# Without this, Thin takes precedence over Puma. +# Removing Thin would make this unnecessary. However, Thin is needed for Konacha. + +module Rack + require 'rack/handler/puma' + + module Handler + + def self.default argument + return Rack::Handler::Puma + end + + end +end \ No newline at end of file From 6dd507672bc055a7f0c46dd9ddee84e286aa0d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Amo=C3=AAdo?= Date: Tue, 18 Apr 2017 16:14:20 -0300 Subject: [PATCH 3/3] Create test for Rack::Handler module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lucas Amoêdo Signed-off-by: Marcelo Oliveira --- spec/lib/core_extensions/rack/handler_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 spec/lib/core_extensions/rack/handler_spec.rb diff --git a/spec/lib/core_extensions/rack/handler_spec.rb b/spec/lib/core_extensions/rack/handler_spec.rb new file mode 100644 index 00000000..d2fcdd15 --- /dev/null +++ b/spec/lib/core_extensions/rack/handler_spec.rb @@ -0,0 +1,12 @@ +require 'rails_helper' +require "#{Rails.root}/lib/core_extensions/rack/handler" + +RSpec.describe Rack::Handler do + + describe 'default webserver' do + it 'should be puma' do + argument = 'argument' + expect(Rack::Handler.default argument).to eq(Rack::Handler::Puma) + end + end +end \ No newline at end of file