在Heroku中在puma上部署rails app时出错

时间:2014-06-23 10:46:25

标签: ruby heroku ruby-on-rails-4 puma

我正在尝试部署在puma和jruby上运行的rails应用程序。 Procfile如下

web: bundle exec puma -C config/puma.rb -p $PORT -e $RACK_ENV

将Puma的配置放在

  

配置/ puma.rb

if ENV['RACK_ENV'] != 'production' || ENV['RAILS_ENV'] != 'production'
  workers Integer(ENV['PUMA_WORKERS'] || 4)
end

threads Integer(ENV['MIN_THREADS']  || 1), Integer(ENV['MAX_THREADS'] || 4)

rackup      DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
preload_app!

on_worker_boot do
  # worker specific setup
  ActiveSupport.on_load(:active_record) do
    config = ActiveRecord::Base.configurations[Rails.env] ||
                Rails.application.config.database_configuration[Rails.env]
    config['pool'] = ENV['MAX_THREADS'] || 16
    ActiveRecord::Base.establish_connection(config)
  end
end

我对工人进行条件初始化的原因是因为Heroku抱怨(或可能是Puma)工作模式不适用于JRuby和Windows;无论如何,

我在heroku上注意到的奇怪行为是(与我的本地环境不同)群集Puma实例启动没有任何问题,并选择两个不同的端口进行绑定,即3000和5000.

然而,在生产中,即Heroku,这会导致崩溃

2014-06-23T08:50:54.545724+00:00 heroku[web.1]: State changed from crashed to starting
2014-06-23T08:51:02.510184+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Djava.rmi.server.useCodebaseOnly=true
2014-06-23T08:51:01.333763+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb -p 49576 -e production`
2014-06-23T08:51:21.658663+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Djava.rmi.server.useCodebaseOnly=true
2014-06-23T08:51:44.964380+00:00 app[web.1]: * Version 2.8.2 (jruby 1.9.3), codename: Sir Edmund Percival Hillary
2014-06-23T08:51:44.962724+00:00 app[web.1]: The signal USR1 is in use by the JVM and will not work correctly on this platform
2014-06-23T08:51:44.964563+00:00 app[web.1]: * Min threads: 1, max threads: 4
2014-06-23T08:51:44.964738+00:00 app[web.1]: * Environment: production
2014-06-23T08:51:44.963925+00:00 app[web.1]: Puma starting in single mode...
2014-06-23T08:51:57.852471+00:00 app[web.1]: Errno::EADDRINUSE: Address already in use - bind - Address already in use
2014-06-23T08:51:57.830580+00:00 app[web.1]: * Listening on tcp://0.0.0.0:49576
2014-06-23T08:51:57.852474+00:00 app[web.1]:         initialize at org/jruby/ext/socket/RubyTCPServer.java:118
2014-06-23T08:51:57.852478+00:00 app[web.1]:   add_tcp_listener at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/lib/puma/binder.rb:195
2014-06-23T08:51:57.852476+00:00 app[web.1]:                new at org/jruby/RubyIO.java:852
2014-06-23T08:51:57.850256+00:00 app[web.1]: * Listening on tcp://0.0.0.0:49576
2014-06-23T08:51:57.852479+00:00 app[web.1]:              parse at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/lib/puma/binder.rb:96
2014-06-23T08:51:57.852481+00:00 app[web.1]:               each at org/jruby/RubyArray.java:1613
2014-06-23T08:51:57.852482+00:00 app[web.1]:              parse at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/lib/puma/binder.rb:82
2014-06-23T08:51:57.852484+00:00 app[web.1]:      load_and_bind at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/lib/puma/runner.rb:119
2014-06-23T08:51:57.852486+00:00 app[web.1]:                run at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/lib/puma/single.rb:73
2014-06-23T08:51:57.852487+00:00 app[web.1]:                run at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/lib/puma/cli.rb:499
2014-06-23T08:51:57.852489+00:00 app[web.1]:             (root) at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/bin/puma:10
2014-06-23T08:51:57.852491+00:00 app[web.1]:               load at org/jruby/RubyKernel.java:1099
2014-06-23T08:51:57.852492+00:00 app[web.1]:             (root) at /app/vendor/bundle/jruby/1.9/bin/puma:23
2014-06-23T08:51:59.347012+00:00 heroku[web.1]: State changed from starting to crashed
2014-06-23T08:51:59.333790+00:00 heroku[web.1]: Process exited with status 1

当我在笔记本电脑上运行时,看不到这种奇怪的启动行为。

我在Heroku工作jruby的唯一方法是通过这个设置

if ENV['RACK_ENV'] != 'production' || ENV['RAILS_ENV'] != 'production'
  workers Integer(ENV['PUMA_WORKERS'] || 4)
  port ENV['PORT'] || 3000
  environment ENV['RACK_ENV'] || 'development'
end

threads Integer(ENV['MIN_THREADS']  || 1), Integer(ENV['MAX_THREADS'] || 4)

rackup      DefaultRackup
preload_app!

on_worker_boot do
  # worker specific setup
  ActiveSupport.on_load(:active_record) do
    config = ActiveRecord::Base.configurations[Rails.env] ||
                Rails.application.config.database_configuration[Rails.env]
    config['pool'] = ENV['MAX_THREADS'] || 16
    ActiveRecord::Base.establish_connection(config)
  end
end

即。如果端口和环境指令在if块外面Heroku不知何故试图在同一个端口上运行两个实例,这显然会导致崩溃。

我是否遗漏了这里的任何配置或基本上以群集模式运行(我甚至尝试在Ruby 2.0.0而不是JRuby上运行)在Heroku中是不可能的

*更新*

Procfile

web: bundle exec puma -C config/puma.rb

配置/ puma.rb

if ENV['RACK_ENV'] != 'production' || ENV['RAILS_ENV'] != 'production'
  workers Integer(ENV['PUMA_WORKERS'] || 4)
end

threads Integer(ENV['MIN_THREADS']  || 1), Integer(ENV['MAX_THREADS'] || 16)

rackup      DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
preload_app!

on_worker_boot do
  # worker specific setup
  ActiveSupport.on_load(:active_record) do
    config = ActiveRecord::Base.configurations[Rails.env] ||
                Rails.application.config.database_configuration[Rails.env]
    config['pool'] = ENV['MAX_THREADS'] || 16
    ActiveRecord::Base.establish_connection(config)
  end
end

条件块的原因(引自初始问题描述

  

我对工人进行有条件初始化的原因是因为   Heroku抱怨(或可能是Puma)工作模式不可用   JRuby和Windows;

错误日志(即使我删除JRUBY,条件工作区)并尝试在集群模式下运行

606 2014-06-23T07:13:42.058507+00:00 heroku[api]: Release v35 created by foo@bar.com
 607 2014-06-23T07:13:47.674708+00:00 app[web.1]: [2] Puma starting in cluster mode...
 608 2014-06-23T07:13:47.674777+00:00 app[web.1]: [2] * Version 2.8.2 (ruby 2.1.1-p76), codename: Sir Edmund Percival Hillary
 609 2014-06-23T07:13:47.674816+00:00 app[web.1]: [2] * Min threads: 1, max threads: 16
 610 2014-06-23T07:13:47.674939+00:00 app[web.1]: [2] * Preloading application
 611 2014-06-23T07:13:47.674858+00:00 app[web.1]: [2] * Environment: production
 612 2014-06-23T07:13:47.674900+00:00 app[web.1]: [2] * Process workers: 4
 613 2014-06-23T07:13:49.078330+00:00 app[web.1]: [2] * Listening on tcp://0.0.0.0:45245
 614 2014-06-23T07:13:49.078766+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `initialize': Address already in use - bind(2) for "0.0.0.0     " port 45245 (Errno::EADDRINUSE)
 615 2014-06-23T07:13:49.078775+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `add_tcp_listener'
 616 2014-06-23T07:13:49.078771+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `new'
 617 2014-06-23T07:13:49.078780+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:82:in `each'
 618 2014-06-23T07:13:49.078785+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/runner.rb:119:in `load_and_bind'
 619 2014-06-23T07:13:49.078778+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:96:in `block in parse'
 620 2014-06-23T07:13:49.078784+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:82:in `parse'
 621 2014-06-23T07:13:49.078793+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/cli.rb:499:in `run'
 622 2014-06-23T07:13:49.078789+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/cluster.rb:271:in `run'
 623 2014-06-23T07:13:49.078794+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/bin/puma:10:in `<top (required)>'
 624 2014-06-23T07:13:49.078798+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/bin/puma:23:in `load'
 625 2014-06-23T07:13:49.078801+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/bin/puma:23:in `<main>'
 626 2014-06-23T07:13:49.078565+00:00 app[web.1]: [2] * Listening on tcp://0.0.0.0:45245
 627 2014-06-23T07:13:50.359244+00:00 heroku[web.1]: Process exited with status 1
 628 2014-06-23T07:13:45.838792+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb -p 45245 -e production`
 629 2014-06-23T07:13:50.385427+00:00 heroku[web.1]: State changed from starting to crashed
 630 2014-06-23T07:13:50.385913+00:00 heroku[web.1]: State changed from crashed to starting
 631 2014-06-23T07:13:57.445391+00:00 app[web.1]: [2] Puma starting in cluster mode...
 632 2014-06-23T07:13:57.445417+00:00 app[web.1]: [2] * Environment: production
 633 2014-06-23T07:13:57.445413+00:00 app[web.1]: [2] * Version 2.8.2 (ruby 2.1.1-p76), codename: Sir Edmund Percival Hillary
 634 2014-06-23T07:13:57.445415+00:00 app[web.1]: [2] * Min threads: 1, max threads: 16
 635 2014-06-23T07:13:57.445419+00:00 app[web.1]: [2] * Process workers: 4
 636 2014-06-23T07:13:57.445423+00:00 app[web.1]: [2] * Preloading application
 637 2014-06-23T07:13:54.371058+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb -p 33243 -e production`
 638 2014-06-23T07:13:59.883490+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `initialize': Address already in use - bind(2) for "0.0.0.0     " port 33243 (Errno::EADDRINUSE)
 639 2014-06-23T07:13:59.882892+00:00 app[web.1]: [2] * Listening on tcp://0.0.0.0:33243
 640 2014-06-23T07:13:59.883501+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:96:in `block in parse'
 641 2014-06-23T07:13:59.883494+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `new'
 642 2014-06-23T07:13:59.883499+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `add_tcp_listener'
 643 2014-06-23T07:13:59.883506+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:82:in `parse'
 644 2014-06-23T07:13:59.883510+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/runner.rb:119:in `load_and_bind'
 645 2014-06-23T07:13:59.883503+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:82:in `each'
 646 2014-06-23T07:13:59.883512+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/cluster.rb:271:in `run'
 647 2014-06-23T07:13:59.883519+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/bin/puma:23:in `load'
 648 2014-06-23T07:13:59.883517+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/bin/puma:10:in `<top (required)>'
 649 2014-06-23T07:13:59.883514+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/cli.rb:499:in `run'
 650 2014-06-23T07:13:59.883525+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/bin/puma:23:in `<main>'
 651 2014-06-23T07:13:59.883273+00:00 app[web.1]: [2] * Listening on tcp://0.0.0.0:33243
 652 2014-06-23T07:14:01.451472+00:00 heroku[api]: Set DEV-2-PASS config vars by foo@bar.com
 653 2014-06-23T07:14:01.451540+00:00 heroku[api]: Release v36 created by foo@bar.com
 654 2014-06-23T07:14:01.624035+00:00 heroku[web.1]: State changed from starting to crashed
 655 2014-06-23T07:14:01.963688+00:00 heroku[web.1]: State changed from crashed to starting
 656 2014-06-23T07:14:01.615855+00:00 heroku[web.1]: Process exited with status 1

1 个答案:

答案 0 :(得分:7)

我和你有同样的问题。

它似乎与端口的重复定义相关

首先在Procfile以及config/puma.rb文件

我从Procfile(以及环境)移除了端口,因为这已在config/puma.rb中定义。

web: bundle exec puma -C config/puma.rb