Question

Rails Puma silently crashes without error

Posted December 2, 2020 2k views
Ruby on RailsUbuntuRuby

I have a Ruby on Rails application deployed in DigitalOcean ubuntu image.

My versions are:

ruby 2.6.0
rails 5.1.5.rc1
puma 4.3.6

I deploy my application using Capistrano

deploy.rb

# config valid for current version and patch releases of Capistrano
lock "~> 3.14.1"

set :application, "ricsonwin"
set :repo_url, "git@github.com:XXX/XXX.git"
set :deploy_user, "deploy"

set :pty, true
set :linked_files, %w{config/database.yml config/secrets.yml}
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}

set :keep_releases, 5
set :rvm_ruby_version, '2.6.0' # Edit this depends on the rvm ruby version installed in the server
set :bundle_binstubs, nil

set :puma_role, :app
set :puma_threads, [0, 8]
set :puma_workers, 0
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :puma_preload_app, false

# Creates database
before 'deploy:migrate', 'deploy:db:create'

after 'deploy:symlink:release', 'puma:config'

namespace :puma do
  Rake::Task[:restart].clear_actions

  desc "Overwritten puma:restart task"
  task :restart do
    puts "Overwriting puma:restart to ensure that puma is running. Effectively, we are just starting Puma."
    puts "A solution to this should be found."
    invoke 'puma:stop'
    invoke 'puma:start'
  end
end

deploy/production.log

server 'XXX.XXX.XXX.XXX', user: 'deploy', roles: %w{web app db}, primary: true

set :location, 'XXX.XXX.XXX.XXX'
role :db, fetch(:location), primary: true
set :branch, :master

set :full_app_name, "#{fetch(:application)}_#{fetch(:stage)}"
set :deploy_to, "/srv/www/apps/#{fetch(:full_app_name)}"

set :stage, :production
set :rails_env, :production
set :console_env, :production
set :environment, :production

set :rvm_type, :user

set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
set :puma_rackup, -> { File.join(current_path, 'config.ru') }
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, ["tcp://0.0.0.0:9292"]
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_access_log, "#{shared_path}/log/puma_error.log"
set :puma_error_log, "#{shared_path}/log/puma_access.log"

config/puma.rb

if Rails.env.production?

  directory = "/srv/www/apps/myapp_production/current"
  rackup "/srv/www/apps/myapp_production/current/config.ru"
  environment Rails.env

  pidfile "/srv/www/apps/myapp_production/shared/tmp/pids/puma.pid"
  state_path "/srv/www/apps/myapp_production/shared/tmp/pids/puma.state"
  stdout_redirect "/srv/www/apps/myapp_production/shared/log/puma_access.log", "/srv/www/apps/myapp_production/shared/log/puma_error.log", true

  threads 4, 16
  workers 1

  bind "unix:///srv/www/apps/myapp_production/shared/tmp/sockets/puma.sock"

  preload_app!

  on_worker_boot do
    ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
    end
  end

else
  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
end

When I check the logs
production.log

Item Load (0.3ms)  SELECT  "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2  [["id", 208], ["LIMIT", 1]]
  CACHE Unit Load (0.0ms)  SELECT  "units".* FROM "units" WHERE "units"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
  Item Load (0.3ms)  SELECT  "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2  [["id", 215], ["LIMIT", 1]]
  CACHE Unit Load (0.0ms)  SELECT  "units".* FROM "units" WHERE "units"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
  Item Load (0.2ms)  SELECT  "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2  [["id", 198], ["LIMIT", 1]]
  CACHE Unit Load (0.0ms)  SELECT  "units".* FROM "units" WHERE "units"."id" = $1 LIMIT $2  [["id", 10], ["LIMIT", 1]]
  Rendering text template
  Rendered text template (0.0ms)
Sent data Delivery Receipt 12-2020-001.pdf (0.5ms)
Completed 200 OK in 4326ms (Views: 0.3ms | ActiveRecord: 37.4ms)

puma_access.log

=== puma startup: 2020-12-01 01:07:13 +0000 ===

puma_error.log

Puma starting in single mode...
* Version 4.3.1 (ruby 2.6.0-p0), codename: Mysterious Traveller
* Min threads: 0, max threads: 8
* Environment: production
* Daemonizing...
=== puma startup: 2020-09-21 05:16:28 +0000 ===
=== puma startup: 2020-09-21 05:17:39 +0000 ===
* Listening on tcp://0.0.0.0:9292
=== puma startup: 2020-09-21 06:11:35 +0000 ===
- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2020-09-21 06:11:35 +0000 ===

My application is running but after a while it will stop, I’m not sure what’s causing the error.

Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!