From 94a7e13dce3ff56b03093f42972d88403f059f81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 9 Oct 2019 14:37:35 +0200 Subject: [PATCH 1/4] Update capistrano's restart unicorn task Our current unicorn task wasn't working in some cases. We also had a version in the `capistrano` branch, which was the one we recommended. However, that version assumed RVM, a certain ruby version and a certain deploy folder were used. This version uses `bundle exec` and variables like `release_path`, so it does not depend on any specific configuration. Even if we're replacing unicorn with puma, I wanted to make this change in case we need it as a reference in the future. --- lib/capistrano/tasks/restart.cap | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/capistrano/tasks/restart.cap b/lib/capistrano/tasks/restart.cap index 54c90819a..e8975077e 100644 --- a/lib/capistrano/tasks/restart.cap +++ b/lib/capistrano/tasks/restart.cap @@ -1,10 +1,11 @@ namespace :deploy do - desc 'Commands for unicorn application' - %w(start stop force-stop restart upgrade reopen-logs).each do |command| - task command.to_sym do - on roles(:app), in: :sequence, wait: 5 do - execute "/etc/init.d/unicorn_#{fetch(:full_app_name)} #{command}" - end + desc "Restart Unicorn" + task :restart do + on roles(:app) do + execute "kill -QUIT `cat #{deploy_to}/pids/unicorn.pid`; true" + execute "kill -QUIT `cat #{shared_path}/pids/unicorn.pid`; true" + execute "kill -QUIT `cat #{shared_path}/tmp/pids/unicorn.pid`; true" + execute "cd #{release_path} && bundle exec unicorn -c config/unicorn.rb -E production -D" end end end From b36e659f4eedbc9109d04fffccf5b4f746bd53e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 10 Oct 2019 22:08:55 +0200 Subject: [PATCH 2/4] Use puma instead of unicorn Puma is the server we use in the development environment, so this way we don't need to maintain two servers. Furthermore, puma seems to offer a few advantages over unicorn (like multithreading) and no disadvantages. --- Capfile | 3 +++ Gemfile | 4 ++-- Gemfile.lock | 15 +++++++-------- config/deploy.rb | 3 +-- lib/capistrano/tasks/restart.cap | 11 ----------- 5 files changed, 13 insertions(+), 23 deletions(-) delete mode 100644 lib/capistrano/tasks/restart.cap diff --git a/Capfile b/Capfile index cef760291..335828c06 100644 --- a/Capfile +++ b/Capfile @@ -12,6 +12,9 @@ require "capistrano/delayed_job" require "whenever/capistrano" require "rvm1/capistrano3" +require "capistrano/puma" +install_plugin Capistrano::Puma + #SCM: Git require "capistrano/scm/git" install_plugin Capistrano::SCM::Git diff --git a/Gemfile b/Gemfile index 5ad8bccfe..b13cc75a2 100644 --- a/Gemfile +++ b/Gemfile @@ -41,6 +41,7 @@ gem "paperclip", "~> 5.2.1" gem "paranoia", "~> 2.4.2" gem "pg", "~> 0.21.0" gem "pg_search", "~> 2.0.1" +gem "puma", "~> 4.2.1" gem "recipient_interceptor", "~> 0.2.0" gem "redcarpet", "~> 3.4.0" gem "responders", "~> 2.4.0" @@ -55,7 +56,6 @@ gem "translator-text", "~> 0.1.0" gem "turbolinks", "~> 2.5.3" gem "turnout", "~> 2.4.0" gem "uglifier", "~> 4.1.2" -gem "unicorn", "~> 5.4.1" gem "whenever", "~> 0.10.0", require: false gem "wicked_pdf", "~> 1.1.0" gem "wkhtmltopdf-binary", "~> 0.12.4" @@ -74,7 +74,6 @@ group :development, :test do gem "knapsack_pro", "~> 1.1.0" gem "launchy", "~> 2.4.3" gem "letter_opener_web", "~> 1.3.4" - gem "puma", "~> 4.1.1" gem "spring", "~> 2.0.1" gem "spring-commands-rspec", "~> 1.0.4" end @@ -94,6 +93,7 @@ group :development do gem "capistrano-bundler", "~> 1.2", require: false gem "capistrano-rails", "~> 1.4.0", require: false gem "capistrano3-delayed-job", "~> 1.7.3" + gem "capistrano3-puma", "~> 4.0.0" gem "erb_lint", require: false gem "mdl", "~> 0.5.0", require: false gem "rubocop", "~> 0.75.0", require: false diff --git a/Gemfile.lock b/Gemfile.lock index aa490cd70..dcb108500 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -116,6 +116,10 @@ GEM capistrano3-delayed-job (1.7.5) capistrano (~> 3.0, >= 3.0.0) daemons (~> 1.2.4) + capistrano3-puma (4.0.0) + capistrano (~> 3.7) + capistrano-bundler + puma (~> 4.0) capybara (2.17.0) addressable mini_mime (>= 0.1.3) @@ -301,7 +305,6 @@ GEM activerecord kaminari-core (= 1.1.1) kaminari-core (1.1.1) - kgio (2.11.2) knapsack_pro (1.1.0) rake kramdown (1.17.0) @@ -388,7 +391,7 @@ GEM activesupport (>= 4.2) arel (>= 6) public_suffix (4.0.1) - puma (4.1.1) + puma (4.2.1) nio4r (~> 2.0) rack (2.0.7) rack-accept (0.4.5) @@ -428,7 +431,6 @@ GEM rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) rainbow (3.0.0) - raindrops (0.19.0) rake (12.3.3) recipient_interceptor (0.2.0) mail @@ -554,9 +556,6 @@ GEM uglifier (4.1.19) execjs (>= 0.3.0, < 3) unicode-display_width (1.6.0) - unicorn (5.4.1) - kgio (~> 2.6) - raindrops (~> 0.7) uniform_notifier (1.11.0) user_agent_parser (2.4.1) uuidtools (2.1.5) @@ -598,6 +597,7 @@ DEPENDENCIES capistrano-bundler (~> 1.2) capistrano-rails (~> 1.4.0) capistrano3-delayed-job (~> 1.7.3) + capistrano3-puma (~> 4.0.0) capybara (~> 2.17.0) capybara-webmock (~> 0.5.3) ckeditor (~> 4.2.3) @@ -641,7 +641,7 @@ DEPENDENCIES paranoia (~> 2.4.2) pg (~> 0.21.0) pg_search (~> 2.0.1) - puma (~> 4.1.1) + puma (~> 4.2.1) rails (= 5.0.7.2) rails-assets-leaflet! rails-assets-markdown-it (~> 8.2.1)! @@ -669,7 +669,6 @@ DEPENDENCIES turbolinks (~> 2.5.3) turnout (~> 2.4.0) uglifier (~> 4.1.2) - unicorn (~> 5.4.1) web-console (~> 3.3.0) whenever (~> 0.10.0) wicked_pdf (~> 1.1.0) diff --git a/config/deploy.rb b/config/deploy.rb index c56d88f3e..417336372 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -21,7 +21,7 @@ set :log_level, :info set :pty, true set :use_sudo, false -set :linked_files, %w[config/database.yml config/secrets.yml config/unicorn.rb config/environments/production.rb] +set :linked_files, %w[config/database.yml config/secrets.yml config/environments/production.rb] set :linked_dirs, %w[log tmp public/system public/assets public/ckeditor_assets] set :keep_releases, 5 @@ -35,7 +35,6 @@ set(:config_files, %w[ log_rotation database.yml secrets.yml - unicorn.rb ]) set :whenever_roles, -> { :app } diff --git a/lib/capistrano/tasks/restart.cap b/lib/capistrano/tasks/restart.cap deleted file mode 100644 index e8975077e..000000000 --- a/lib/capistrano/tasks/restart.cap +++ /dev/null @@ -1,11 +0,0 @@ -namespace :deploy do - desc "Restart Unicorn" - task :restart do - on roles(:app) do - execute "kill -QUIT `cat #{deploy_to}/pids/unicorn.pid`; true" - execute "kill -QUIT `cat #{shared_path}/pids/unicorn.pid`; true" - execute "kill -QUIT `cat #{shared_path}/tmp/pids/unicorn.pid`; true" - execute "cd #{release_path} && bundle exec unicorn -c config/unicorn.rb -E production -D" - end - end -end From 3b79a1a3dbb356b23fa836666dac3beacf0aaa8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 12 Oct 2019 15:05:55 +0200 Subject: [PATCH 3/4] Add compatibility between puma and RMV1 Puma was adding commands to `rvm_map_bins`, which meant RMV1 wasn't using the default value of `rvm1_map_bins`. Changing the order we use to require `rmv1/capistrano3` and `capistrano/puma` did not fix the issue. --- config/deploy.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/deploy.rb b/config/deploy.rb index 417336372..d120e3140 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -8,6 +8,7 @@ end set :rails_env, fetch(:stage) set :rvm1_ruby_version, "2.3.2" +set :rvm1_map_bins, -> { fetch(:rvm_map_bins).to_a.concat(%w[rake gem bundle ruby]).uniq } set :application, "consul" set :full_app_name, deploysecret(:full_app_name) From f26f8b3c3eabb8cde61b98d83418c3546e73aea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 12 Oct 2019 17:00:46 +0200 Subject: [PATCH 4/4] Add support for legacy unicorn installations Old CONSUL nginx configurations will probably have a reference to a unicorn socket. Making that file a symbolic link to a puma socket makes it possible for the application to keep working without updating the nginx configuration file. --- config/deploy.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/config/deploy.rb b/config/deploy.rb index d120e3140..de562dc58 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -50,6 +50,8 @@ namespace :deploy do after :published, "delayed_job:restart" after :published, "refresh_sitemap" + before "deploy:restart", "setup_puma" + after :finishing, "deploy:cleanup" desc "Deploys and runs the tasks needed to upgrade to a new release" @@ -94,3 +96,22 @@ task :execute_release_tasks do end end end + +desc "Create pid and socket folders needed by puma and convert unicorn sockets into symbolic links \ + to the puma socket, so legacy nginx configurations pointing to the unicorn socket keep working" +task :setup_puma do + on roles(:app) do + with rails_env: fetch(:rails_env) do + execute "mkdir -p #{shared_path}/tmp/sockets; true" + execute "mkdir -p #{shared_path}/tmp/pids; true" + + if test("[ -e #{shared_path}/tmp/sockets/unicorn.sock ]") + execute "ln -sf #{shared_path}/tmp/sockets/puma.sock #{shared_path}/tmp/sockets/unicorn.sock; true" + end + + if test("[ -e #{shared_path}/sockets/unicorn.sock ]") + execute "ln -sf #{shared_path}/tmp/sockets/puma.sock #{shared_path}/sockets/unicorn.sock; true" + end + end + end +end