1

I'm using Puma as application server for my Rails 4 project on MRI 2.1.0. I'm using Capistrano 3 to handle deployments. Everything is working like a charm. But, I recently noticed an issue with my deployment process. If I change my Gemfile then, puma fails to complete phased-restart and eventually all workers get killed. I'm running Puma in cluster mode and preload_app! is set true.

Here is my Capistrano recipe to handle phased-restart.

desc "Restart the application (phased restart)"
task :phased_restart do
  on roles(:app) do |h|
    execute "cd #{fetch(:current_path)} && bundle exec pumactl -S #{fetch(:puma_state)}     phased-restart", :pty => true
  end
end

This is truncated output of Capistrano log.

DEBUG [4790766f] Command: cd /home/app/current && bundle exec pumactl -S /home/app/shared/tmp/pids/puma.state phased-restart
DEBUG [de00176a]    Command phased-restart sent success
 INFO [de00176a] Finished in 0.909 seconds with exit status 0 (successful).

This is my config/puma.rb file.

#!/usr/bin/env puma
require 'active_support'

environment 'production'
daemonize
pidfile '/home/app/shared/tmp/pids/puma.pid'
state_path '/home/app/shared/tmp/pids/puma.state'
stdout_redirect 'log/puma_stdout.log', 'log/puma_stderr.log'
threads 100, 100

bind 'tcp://0.0.0.0:9292'
bind 'unix:////home/app/shared/tmp/pids/puma.sock'

on_worker_boot do
  ActiveSupport.on_load(:active_record) do
  ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
  ActiveRecord::Base.establish_connection(YAML.load_file("#{Rails.root}/config/database.yml")[Rails.env])
  end
end
workers 4
preload_app!

Does anybody see anything wrong in my puma config file?

So, currently I do bundle exec cap production deploy:start to start Puma when this happens. But, I want zero-downtime-deployment in every cases.

Can Puma keep using old worker processes in case new spawned processes couldn't be started?

2 Answers 2

6

Do you know that preload_app! conflicts with phased restarts?

Proof: https://github.com/puma/puma/blob/0ea7af5e2cc8fa192ec82934a4a47880bdb592f8/lib/puma/configuration.rb#L333-L335

I think first you need to decide which to use.

0
4

For doing a phased restart you need to enable the prune_bundler option and disable preload_app! See https://github.com/puma/puma/blob/master/DEPLOYMENT.md#restarting

To do zero-downtime deploys with Capistrano, you can use the capistrano3-puma gem with the following options:

set :puma_preload_app, false
set :puma_prune_bundler, true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.