【问题标题】:Puma not working with Rails 5Puma 不支持 Rails 5
【发布时间】:2018-02-28 07:56:07
【问题描述】:

我正在将我的 Rails 应用程序从 4.1.1 升级到 5.1.4。以前它在应用服务器中使用 webrick,但由于 Rails 5 使用 Puma 作为默认服务器,所以我安装了它但出现错误。

config/puma.rb

# Change to match your CPU core count
workers 2

# Min and Max threads per worker
threads 1, 6

app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"

# Default to production
rails_env = ENV['RAILS_ENV'] || "development"
environment rails_env

# Set up socket location
bind "unix:/home/system/project/shared/sockets/puma.sock"
# bind "unix:#{shared_dir}/sockets/puma.sock"

# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true

# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
activate_control_app

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

我创建了一个具有适当权限的文件夹根路径“shared/sockets”:

drwxrwxrwx 1 system system 4096 Sep 19 10:35 tmp
drwxr-xr-x 1 system system 4096 Sep 19 10:37 sockets
drwxr-xr-x 1 system system 4096 Sep 19 10:51 shared

当我运行rails s 或尝试在前台模式下运行puma 时,出现以下错误:

system@system /home/system/project (rails_upgrade_5 *) $ rails s
=> Booting Puma
=> Rails 5.1.4 application starting in development 
=> Run `rails server -h` for more startup options
[28725] Puma starting in cluster mode...
[28725] * Version 3.10.0 (ruby 2.4.1-p111), codename: Russell's Teapot
[28725] * Min threads: 1, max threads: 6
[28725] * Environment: development
[28725] * Process workers: 2
[28725] * Phased restart available
[28725] * Listening on unix:/home/system/project/shared/sockets/puma.sock
Exiting
/home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/puma-3.10.0/lib/puma/binder.rb:367:in `initialize': Operation not permitted - connect(2) for /home/system/project/shared/sockets/puma.sock (Errno::EPERM)
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/puma-3.10.0/lib/puma/binder.rb:367:in `new'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/puma-3.10.0/lib/puma/binder.rb:367:in `add_unix_listener'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/puma-3.10.0/lib/puma/binder.rb:141:in `block in parse'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/puma-3.10.0/lib/puma/binder.rb:88:in `each'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/puma-3.10.0/lib/puma/binder.rb:88:in `parse'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/puma-3.10.0/lib/puma/cluster.rb:420:in `run'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/puma-3.10.0/lib/puma/launcher.rb:183:in `run'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/puma-3.10.0/lib/rack/handler/puma.rb:69:in `run'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/rack-2.0.3/lib/rack/server.rb:297:in `start'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:44:in `start'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:135:in `block in perform'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `tap'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `perform'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
    from /home/system/.rvm/gems/ruby-2.4.1@rails_5/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>

我尝试了stackoverflow 上给出的一些解决方案,但无法正常工作。 如果我做错了什么,请告诉我。

【问题讨论】:

    标签: ruby-on-rails puma application-server ruby-on-rails-5.1 ruby-on-rails-5


    【解决方案1】:

    更新你的 puma.rb 到这个文件

    # 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.
    #
    threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
    threads threads_count, threads_count
    
    # 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.
    #
    environment ENV.fetch("RAILS_ENV") { "development" }
    
    # Specifies the number of `workers` to boot in clustered mode.
    # Workers are forked webserver 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. If you use this option
    # you need to make sure to reconnect any threads in the `on_worker_boot`
    # block.
    #
    # preload_app!
    
    # The code in the `on_worker_boot` will be called if you are using
    # clustered mode by specifying a number of `workers`. After each worker
    # process is booted this block will be run, if you are using `preload_app!`
    # option you will want to use this block to reconnect to any threads
    # or connections that may have been created at application boot, Ruby
    # cannot share connections between processes.
    #
    # on_worker_boot do
    #   ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
    # end
    
    # Allow puma to be restarted by `rails restart` command.
    plugin :tmp_restart
    

    【讨论】:

    • 我需要 sock 文件。
    • sock 文件是什么意思?
    • 检查我的puma.rb 文件。我基本上使用的是 nginx,在 nginx 块中,我已经向上游提供了 sock 文件。
    【解决方案2】:

    问题在于以下行

    config/puma.rb

    # Set up socket location
    bind "unix:/home/system/project/shared/sockets/puma.sock"
    

    修改如下:

    # Set up socket location
    bind "unix:///home/system/project/shared/sockets/puma.sock"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 2014-02-08
      • 2018-08-24
      • 1970-01-01
      相关资源
      最近更新 更多