Capistrano puma:restart 不工作,但 puma:start 可以

Capistrano puma:restart not working, but puma:start does

我正在使用 Capistrano 将我的 Rails 5 应用程序部署到 AWS EC2 实例。当我输入:

cap production deploy

一切顺利,部署成功。但是,重启 puma 的过程不起作用。我看到的最后一个任务是:

puma:restart
      01 ~/.rvm/bin/rvm ruby-2.4.0 do bundle exec pumactl -S /home/deploy/myapp/shared/tmp/pids/puma.state -F /home/deploy/myapp/s…
      01 Command restart sent success

但是,当我访问我的网站时,我看到:

502 Bad Gateway
nginx/1.4.6 (Ubuntu)

cap production puma:status 警告我 Puma 不是 运行。有趣的是,cap production puma:start 与以下 echo 完美配合:

puma:start
      using conf file /home/deploy/myapp/shared/puma.rb
      01 ~/.rvm/bin/rvm ruby-2.4.0 do bundle exec puma -C /home/deploy/myapp/shared/puma.rb --daemon
      01 Puma starting in single mode...
      01
      01 * Version 3.9.1 (ruby 2.4.0-p0), codename: Private Caller
      01
      01 * Min threads: 0, max threads: 8
      01
      01 * Environment: production
      01
      01 * Daemonizing...
      01

我花了几个小时试图找出问题所在。我可以通过在每次部署时启动 Puma 服务器而不是重新启动或 phased_restart 任务来解决这个问题。但可能是什么原因造成的?我在 2 周前部署了一个类似的应用程序(它是一个 API 唯一的应用程序)并且它没有任何问题。

这是我的 deploy.rb:

lock "3.8.2"

set :application, "myapp"
set :repo_url, "git@bitbucket.org:myorg/myapp.git"

# set :branch, :master
set :branch, :release
set :deploy_to, '/home/deploy/myapp'
set :pty, true
set :linked_files, %w{config/database.yml config/application.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}

set :keep_releases, 5
set :rvm_type, :user
set :rvm_ruby_version, 'ruby-2.4.0'

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, "unix://#{shared_path}/tmp/sockets/puma.sock"
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"
set :puma_role, :app
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
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

我的 config/deploy/production.rb:

server 'myapp.com', user: 'deploy', roles: %w{web app db}

我的puma.rb:

#!/usr/bin/env puma

directory '/home/deploy/myapp/current'
rackup "/home/deploy/myapp/current/config.ru"
environment 'production'
daemonize true

tag ''

pidfile "/home/deploy/myapp/shared/tmp/pids/puma.pid"
state_path "/home/deploy/myapp/shared/tmp/pids/puma.state"
stdout_redirect '/home/deploy/myapp/shared/log/puma_error.log', '/home/deploy/myapp/shared/log/puma_access.log', true

threads 0,8

bind 'unix:///home/deploy/myapp/shared/tmp/sockets/puma.sock'

workers 0
prune_bundler

on_restart do
  puts 'Refreshing Gemfile'
  ENV["BUNDLE_GEMFILE"] = "/home/deploy/myapp/current/Gemfile"
end

我真的卡在这里了。即使经过数小时的研究,我也找不到有效的解决方案!

正如 Matt 在评论中指出的那样,这似乎是一个已报告的错误。我让猴子修补了 puma:restart 任务作为临时解决方案,直到这个问题得到解决。

我创建了 /lib/capistrano/tasks/overwrite_restart.rake:

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

作为替代方案,我想我们可以覆盖 capistrano-puma gem 中的 :register_hooks 方法,并将 puma:restart 替换为 puma:stoppuma:start.

我 运行 遇到了 Capist运行o Puma 基本任务没有出现的问题(重启、停止、开始)。发生这种情况是因为,在 Capist运行o Puma 5.0 中,任务被移动到服务管理器模块,

之后

require "capistrano/puma"
install_plugin Capistrano::Puma

一定要加上:

install_plugin Capistrano::Puma::Daemon

请注意,这与 Puma 5+ 不兼容,因为守护进程支持已被删除。

来源:https://github.com/seuros/capistrano-puma/issues/310

或者如果您使用 Systemd

install_plugin Capistrano::Puma::Systemd

然后所有任务现在应该显示出来,Puma 应该在部署后重新启动。