【问题标题】:Application crashes all the time - Heroku应用程序一直崩溃 - Heroku
【发布时间】:2013-12-24 13:19:30
【问题描述】:

我遇到了 Heroku 的问题。有一段时间一切都还好,但后来应用程序开始一直崩溃。没有可能导致此类行为的更改。这是heroku日志所说的:

2013-12-24T13:02:33.056291+00:00 heroku[web.1]: State changed from crashed to starting
2013-12-24T13:02:39.813510+00:00 heroku[web.1]: Starting process with command `bundle exec unicorn -c ./config/unicorn.rb`
2013-12-24T13:02:41.529872+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.6.3/lib/unicorn.rb:5:in `require': cannot load such     file -- rack (LoadError)
2013-12-24T13:02:41.529872+00:00 app[web.1]:  from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.6.3/lib/unicorn.rb:5:in `<top (required)>'
2013-12-24T13:02:41.529872+00:00 app[web.1]:  from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.6.3/lib/unicorn/launcher.rb:9:in `require'
2013-12-24T13:02:41.529872+00:00 app[web.1]:  from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.6.3/bin/unicorn:3:in `require'
2013-12-24T13:02:41.529872+00:00 app[web.1]:  from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.6.3/lib/unicorn/launcher.rb:9:in `<top (    required)>'
2013-12-24T13:02:41.529872+00:00 app[web.1]:  from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.6.3/bin/unicorn:3:in `<top (required)>'
2013-12-24T13:02:41.529872+00:00 app[web.1]:  from /app/vendor/bundle/ruby/1.9.1/bin/unicorn:23:in `load'
2013-12-24T13:02:41.529872+00:00 app[web.1]:  from /app/vendor/bundle/ruby/1.9.1/bin/unicorn:23:in `<main>'
2013-12-24T13:02:42.643135+00:00 heroku[web.1]: Process exited with status 1
2013-12-24T13:02:42.658659+00:00 heroku[web.1]: State changed from starting to crashed

如果我尝试让 heroku 运行 rake db:migrate,我会得到这个:

/app/vendor/bundle/ruby/1.9.1/bin/rake:23:in `load': cannot load such file --      /app/vendor/bundle/ruby/1.9.1/specifications/bin/rake (LoadError)
from /app/vendor/bundle/ruby/1.9.1/bin/rake:23:in `<main>'

在 heroku run rails c 之后,我得到了这个:

/app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails.rb:14:in `require': cannot load such file -- action_dispatch/railtie (LoadError)
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails.rb:14:in `<top (required)>'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/all.rb:1:in `require'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/all.rb:1:in `<top (required)>'
from /app/config/application.rb:3:in `require'
from /app/config/application.rb:3:in `<top (required)>'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:39:in `require'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:39:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

如果有人可以提供帮助,那就太好了。谢谢!

更新: 这是我的配置/unicorn.rb:

timeout 30
preload_app true
worker_processes Integer(ENV['UNICORN_WORKERS'] || 3)
listen ENV['PORT'], :backlog => Integer(ENV['UNICORN_BACKLOG'] || 16)

before_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
    Rails.logger.info('Disconnected from ActiveRecord')
  end

  sleep 1
end

after_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
    Rails.logger.info('Connected to ActiveRecord')
  end
end

这是我的 Procfile:

web: bundle exec unicorn -c ./config/unicorn.rb

【问题讨论】:

  • 嘿,你有一个 config/unicorn.rb 文件,对吧?都设置好了?
  • 如果你运行 webrick 一切都好吗?
  • 是的,用 webrick 一切正常

标签: ruby-on-rails heroku unicorn


【解决方案1】:

我认为您的 Procfile 配置不正确。根据the Heroku Unicorn docs,我认为您还需要在Procfile 中定义端口。

这是我在 Unicorn 上运行良好的 unicorn.rb 文件,以及对应的 Procfile

# config/unicorn.rb

worker_processes Integer(ENV['WEB_CONCURRENCY'] || 3)
timeout Integer(ENV['WEB_TIMEOUT'] || 15)
preload_app true

before_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end  

after_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

过程文件

web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb

【讨论】:

  • 感谢您的建议。我已经尝试过了,但效果不佳。我试图将应用程序部署到另一个heroku(具有相同的配置)并且一切正常。奇怪...
猜你喜欢
  • 2019-02-06
  • 2011-10-03
  • 2015-04-22
  • 2016-11-20
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多