Search code examples
ruby-on-railsrubynginxrubygemspuma

Rails Capistrano production deploy runs a wrong command line which leads to an error 'invalid option --daemon'


I've been trying to fix this error, but I can't find a solution yet. I'm about to deploy a Rails application to production using nginx, puma, and capistrano, but I get an error when running this command line bundle exec cap production deploy:initial. Right after initializing 'PUMA:START' Top Task, my terminal throws an error message and exit the process.

shared/bundle/ruby/2.6.0/gems/puma-5.2.2/lib/puma/cli.rb:50:in `initialize': invalid option: --daemon (OptionParser::InvalidOption) puma stderr: Nothing written Tasks: TOP => puma:start (See full trace by running task with --trace)

I think this is caused by a wrong command line executed before which is:

00:00 puma:start
      01 RBENV_ROOT=$HOME/.rbenv RBENV_VERSION=2.6.5 $HOME/.rbenv/bin/rbenv exec bundle exec puma -C /home/usr/myapp/shared/puma.rb --daemon

I have tried to find the way I could fix this by modifying the deploy process command, but I haven't found anything relevant.

I will leave my Capfile, deploy.rb, nginx.conf and gems, so you might have a better understanding of what's going wrong.

deploy.rb

# Change these
server 'server', port: my_port, roles: [:web, :app, :db], primary: true

set :repo_url,        'link'
set :application,     'example-app'
set :user,            'myuser'
set :puma_threads,    [4, 16]
set :puma_workers,    0

set :rbenv_type, :user # or :system, or :fullstaq (for Fullstaq Ruby), depends on your rbenv setup
set :rbenv_ruby, '2.6.5'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_roles, :all # default value

# Don't change these unless you know what you're doing
set :pty,             true
set :use_sudo,        false
set :stage,           :production
set :deploy_via,      :remote_cache
set :deploy_to,       "/home/#{fetch(:user)}/#{fetch(:application)}"
set :puma_bind,       "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_error_log,  "#{release_path}/log/puma.access.log"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :ssh_options,     { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true  # Change to false when not using ActiveRecord

## Defaults:
# set :scm,           :git
# set :branch,        :master
# set :format,        :pretty
# set :log_level,     :debug
# set :keep_releases, 5

## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs,  %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :linked_files, %w{config/master.key}

namespace :puma do
  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end

  before :start, :make_dirs
end

namespace :deploy do
  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes."
        exit
      end
    end
  end

  desc 'Initial Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:restart', 'puma:start'
      invoke 'deploy'
    end
  end

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      invoke 'puma:restart'
    end
  end

  before :starting,     :check_revision
  after  :finishing,    :compile_assets
  after  :finishing,    :cleanup
  after  :finishing,    :restart
end

# ps aux | grep puma    # Get puma pid
# kill -s SIGUSR2 pid   # Restart puma
# kill -s SIGTERM pid   # Stop puma

nginx.conf

upstream puma {
  server unix:///home/deploy/my_app/shared/tmp/sockets/my_app-puma.sock;
}

server {
  listen 80 default_server deferred;
  # server_name example.com;

  root /home/deploy/CFE-Rails-App/current/public;
  access_log /home/deploy/my_app/current/log/nginx.access.log;
  error_log /home/deploy/my_app/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

Capfile

# Load DSL and set up stages
require "capistrano/setup"

# Include default deployment tasks
require "capistrano/deploy"

require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rbenv'
require 'capistrano/puma'

install_plugin Capistrano::Puma::Daemon
install_plugin Capistrano::Puma

# Load the SCM plugin appropriate to your project:
#
# require "capistrano/scm/hg"
# install_plugin Capistrano::SCM::Hg
# or
# require "capistrano/scm/svn"
# install_plugin Capistrano::SCM::Svn
# or
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git

# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails
#   https://github.com/capistrano/passenger
#

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

My installed gems

  gem "capistrano", require: false
  gem "capistrano-rails", require: false
  gem 'capistrano3-puma',   require: false
  gem 'capistrano-rbenv'

Solution

  • What Im suspecting is that the puma gem version ( probably > 5.0+) that you are using doesnt support --daemon option.

    This was highlighted in the capistrano-puma documentation. The puma cli command line parser will throw error as --daemon wasn't one of the supported option. You can check your Gemfile.lock for your puma version to verify this.

    In fact, the capistrano-puma doesn not stop you from using puma with version higher than 5.0. However, the gem user is responsible for the plugin compatibility requirement.