Rails Capistrano 生产部署运行错误的命令行导致错误 'invalid option --daemon'

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

我一直在尝试修复这个错误,但我找不到解决方案。我正准备使用 nginx、puma 和 capistrano 将 Rails 应用程序部署到生产环境,但是当 运行 此命令行 bundle exec cap production deploy:initial 时我收到错误消息。在初始化 'PUMA:START' Top Task 后,我的终端立即抛出一条错误消息并退出进程。

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)

我认为这是由之前执行的错误命令行引起的:

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

我试图通过修改部署过程命令来找到解决此问题的方法,但我没有找到任何相关的东西。

我将留下我的 Capfile、deploy.rb、nginx.conf 和 gems,这样您可能会更好地了解问题所在。

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 }

我安装的 gem

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

我怀疑您使用的 puma gem 版本(可能 > 5.0+)不支持 --daemon 选项。

这已在 capistrano-puma documentation 中突出显示。 puma cli 命令行解析器将抛出错误,因为 --daemon 不是受支持的选项之一。您可以检查 Gemfile.lockpuma 版本以验证这一点。

事实上,capistrano-puma 并不会阻止您使用高于 5.0 的版本 puma。但是,gem 用户负责插件兼容性要求。