简体   繁体   English

Capistrano puma:restart 不起作用,但 puma:start 起作用

[英]Capistrano puma:restart not working, but puma:start does

I am using Capistrano to deploy my Rails 5 app to AWS EC2 instance.我正在使用 Capistrano 将我的 Rails 5 应用程序部署到 AWS EC2 实例。 When I type:当我输入:

cap production deploy

Things go smoothly and the deployment is successful.一切顺利,部署成功。 However, the process of restarting puma is not working.但是,重启puma的过程不起作用。 The last task I see as:我看到的最后一个任务是:

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

However, when I visit my website, I see:但是,当我访问我的网站时,我看到:

502 Bad Gateway
nginx/1.4.6 (Ubuntu)

Doing cap production puma:status warns me that Puma is not running.cap production puma:status警告我 Puma 没有运行。 Interestingly, cap production puma:start works perfectly with the following echo:有趣的是, 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

I have spent a few hours trying to find out what the issue is.我花了几个小时试图找出问题所在。 I can work around the issue by doing starting the Puma server everytime I deploy instead of the restart or phased_restart tasks.我可以通过在每次部署时启动 Puma 服务器而不是重启或 phased_restart 任务来解决这个问题。 But what could be causing this?但什么可能导致这种情况? I deployed a similar app 2 weeks back (it was an API only app) and it worked without any issues.两周前我部署了一个类似的应用程序(它是一个只有 API 的应用程序)并且它没有任何问题。

Here is my deploy.rb:这是我的 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

My config/deploy/production.rb:我的配置/部署/production.rb:

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

My puma.rb:我的 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

I am really stuck here.我真的被困在这里了。 Even after hours of research, I cannot find a solution that works!即使经过数小时的研究,我也找不到有效的解决方案!

As pointed out in the comments by Matt, this seems to be a reported bug.正如 Matt 在评论中指出的那样,这似乎是一个已报告的错误。 I have monkey patched the puma:restart task as a temporary solution until this gets fixed.我有猴子修补 puma:restart 任务作为临时解决方案,直到这个问题得到解决。

I have created /lib/capistrano/tasks/overwrite_restart.rake:我已经创建了/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

As an alternative, I guess we could overwrite the :register_hooks method in the capistrano-puma gem, and replace puma:restart with puma:stop and puma:start .作为替代方案,我想我们可以覆盖 capistrano-puma gem 中的 :register_hooks 方法,并将puma:restart替换为puma:stoppuma:start

I ran into the problem that Capistrano Puma basic tasks were not showing up (restart, stop, start).我遇到了 Capistrano Puma 基本任务没有显示的问题(重启、停止、启动)。 This happens because, in Capistrano Puma 5.0, the tasks were moved to the service manager modules,发生这种情况是因为在 Capistrano Puma 5.0 中,任务被移到了服务管理器模块中,

After之后

require "capistrano/puma"
install_plugin Capistrano::Puma

be sure to add:请务必添加:

install_plugin Capistrano::Puma::Daemon

Note that this is incompatible with Puma 5+ as daemonization support was removed.请注意,这与 Puma 5+ 不兼容,因为删除了守护进程支持。

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

or if you use Systemd或者如果您使用 Systemd

install_plugin Capistrano::Puma::Systemd

then all tasks should show up now and Puma should restart after deployment.那么现在所有任务都应该显示出来,Puma 应该在部署后重新启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM