Puma 工作线程无法在 Ubuntu 20.04 VM 上的 Rails 5.2 中启动

问题描述 投票:0回答:1

我正在学习使用

Capistrano
部署演示 Rails 应用程序,并使用 Puma 作为应用程序服务器和相应的 Nginx Web 服务器。我已经设置了
puma_bind
puma_state
puma_workers
和其他必要的 puma 设置配置,最后将
puma
设置为
systemd
服务。但由于未知的原因,Puma 工人并没有按照流程启动。我的
puma.rb
就像,

#!/usr/bin/env puma

directory '/home/deployer/myarticles_staging/current'
environment 'staging'

pidfile '/home/deployer/myarticles_staging/shared/tmp/pids/puma.pid'
state_path '/home/deployer/myarticles_staging/shared/tmp/states/puma.state'
stdout_redirect '/home/deployer/myarticles_staging/shared/log/puma_access.log', '/home/deployer/myarticles_staging/shared/log/puma_error.log', true

daemonize

threads 4, 8

bind 'unix:///home/deployer/myarticles_staging/shared/tmp/sockets/puma.myarticles_staging.sock'

activate_control_app 'unix:///home/deployer/myarticles_staging/shared/tmp/sockets/pumactl.myarticles_staging.sock'

workers '4'
  
preload_app!
  
on_restart do
  puts 'Refreshing Gemfile'
  ENV['BUNDLE_GEMFILE'] = ''
end

before_fork do
  ActiveRecord::Base.connection_pool.disconnect!
end

on_worker_boot do
  ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
  end
end

我已经注册了一个

puma
服务,该服务指向名为
puma_myarticles_staging
的应用程序,当我以
/etc/init.d/puma_myarticles_staging start
身份启动 puma 服务时,它会输出,

Starting Puma rack web server puma_myarticles_staging ...
--> Woke up puma /home/deployer/myarticles_staging/current
[1484] Puma starting in cluster mode...
[1484] * Version 3.12.6 (ruby 2.7.0-p0), codename: Llamas in Pajamas
[1484] * Min threads: 4, max threads: 8
[1484] * Environment: staging
[1484] * Process workers: 4
[1484] * Preloading application
/home/deployer/myarticles_staging/shared/bundle/ruby/2.7.0/gems/activerecord-5.2.8.1/lib/active_record/type.rb:27: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/home/deployer/myarticles_staging/shared/bundle/ruby/2.7.0/gems/activerecord-5.2.8.1/lib/active_record/type/adapter_specific_registry.rb:9: warning: The called method `add_modifier' is defined here
[1484] * Listening on unix:///home/deployer/myarticles_staging/shared/tmp/sockets/puma.myarticles_staging.sock
[1484] ! WARNING: Detected 1 Thread(s) started in app boot:
[1484] ! #<Thread:0x0000561adf6c0228 /home/deployer/myarticles_staging/shared/bundle/ruby/2.7.0/gems/activerecord-5.2.8.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:299 sleep> - /home/deployer/myarticles_staging/shared/bundle/ruby/2.7.0/gems/activerecord-5.2.8.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:301:in `sleep'
[1484] * Daemonizing...

最终,当我检查

puma.pid
puma.state
时,puma 服务人员没有启动,没有文件被写入。当我运行
rbenv exec bundle exec rails s
手动测试时它可以工作。

但是,当我使用

ps ax | grep puma
检查被妖魔化为 systemd 服务的 puma 进程时,它输出,

1516 pts/0    S+     0:00 grep --color=auto puma

对我可能做错的事情有什么建议吗?预先感谢。

ruby-on-rails ruby ubuntu puma ruby-on-rails-5.2
1个回答
0
投票

也许问题在于 puma 的启动方式。我很确定这里没有 Puma bug。 您还没有发布有关 systemctl 设置的任何详细信息,但最佳实践是使用用户服务文件,这样 puma 将始终在启动时启动,并且不需要密码

我使用的标准 puma 配置是这样的

# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count

# Change to match your CPU core count
workers 0

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 3000 }

# Specifies the `environment` that Puma will run in.
#

rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env

app_dir = File.expand_path("../..", __FILE__)
#shared_dir = "#{app_dir}/shared"
shared_dir = "/home/project/apps/comtech/shared" # Use your projects path

# Specifies the `pidfile` that Puma will use.
#pidfile ENV.fetch("PIDFILE") { "pids/server.pid" }

pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
activate_control_app

# Set up socket location
bind "unix://#{shared_dir}/sockets/comtech_puma.sock"

on_worker_boot do
  require "active_record"
  ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
  ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
end

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked web server processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
#
# preload_app!

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

显然,您需要将

ENVIRONMENT
变量设置为在服务器上暂存

相应调整 pids 文件夹以使用 tmp。

如果没有更多信息,真的无法提供更多帮助

© www.soinside.com 2019 - 2024. All rights reserved.