Capistrano 不创建 puma.pid 文件

Capistrano doesn't create puma.pid file

我使用 Capistrano 来部署我的 Rails 应用程序。 但是,当我部署我的应用程序时,puma.pid 文件没有被创建,这导致了一个问题——我无法重新启动服务器或使用 capistrano 部署新版本——capistrano 无法停止 puma,因为 puma.pid 不存在,它假定没有 puma 进程正在 运行ning。当 capistrano 尝试 运行 另一个版本的 puma 时,我得到 ADDRESS ALREADY IN USE 错误。

这是我的deploy.rb

lock '3.4.0'

set :application, 'appname'
set :repo_url, 'giturl'
set :deploy_to, '/home/user/appname'
set :pty, false
set :linked_files, %w(config/application.yml config/database.yml)
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, '2.3.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" # accept array for multi-bind
set :puma_conf, "#{shared_path}/config/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, 1
set :puma_workers, 1
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :puma_preload_app, true

set :sidekiq_config, 'config/sidekiq.yml'
set :sidekiq_log, '/dev/null'
set :sidekiq_processes, 1

set :clockwork_file, "clock.rb"

namespace :deploy do

  desc 'Recompile all enterprise themes'
  task :recompile_themes, [:command] => 'deploy:set_rails_env' do |task, args|
    on primary(:app) do
      within current_path do
        with :rails_env => fetch(:rails_env) do
          rake 'themes:recompile'
        end
      end
    end
  end

  after :finishing, "deploy:recompile_themes"
end

require 'appsignal/capistrano'

例如,当我尝试停止 运行ning Puma 时,出现以下错误:

...
DEBUG [37113fcf] Running [ -f /home/user/appname/shared/tmp/pids/puma.pid ] as user@server
DEBUG [37113fcf] Command: [ -f /home/user/appname/shared/tmp/pids/puma.pid ]
DEBUG [37113fcf] Finished in 0.273 seconds with exit status 1 (failed).
WARN Puma not running

事实上,/home/user/appname/shared/tmp/pids/puma.pid 文件不存在。我需要手动创建它还是什么? sidekiq.pid在同一个目录中可以找到同样以相同方式创建的,所以我认为这不是权限问题。

关于如何处理这个问题有什么建议吗?

我设法通过编辑 config/puma.rb 文件并添加以下行来解决此问题:

# Store the pid of the server in the file at "path".
pidfile '/home/user/appname/shared/tmp/pids/puma.pid'

# Use "path" as the file to store the server info state. This is
# used by "pumactl" to query and control the server.
state_path '/home/user/appname/shared/tmp/pids/puma.state'

但这当然远非理想的解决方案,因为它会造成重复,所以我正在等待来自 SO 社区的其他更优雅的解决方案

我假设您使用的是 capistrano3-puma gem。 gem 的工作原理如下:各种 puma_* 设置对 Puma 没有直接影响。 这些设置不会从 Capistrano 传递到 Puma。 Puma 从 config/puma.rb 文件中获取所有设置。为了使您的 puma_* 设置生效,您必须明确 运行:

cap production puma:config

这采用您在 Capistrano 中指定的 puma_* 设置,使用这些值构建适当的 config.rb 文件,并将其上传到服务器。具体来说,它将它放在 #{shared_path}/config/puma.rb.

您应该在 :linked_files 中添加 config/puma.rb,以便每个部署都链接到此共享配置。

换句话说,解决方案是从您的源代码存储库中删除 config/puma.rb,然后依靠 cap production puma:config 为您生成一个合适的代码。