应用程序一直崩溃 - Heroku

时间:2013-12-24 13:19:30

标签: ruby-on-rails heroku unicorn

我遇到了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运行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>'

如果有人能帮忙解决这个问题会很棒。谢谢!

UPD: 这是我的config / 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

1 个答案:

答案 0 :(得分:0)

我认为您的Procfile配置不正确。根据{{​​3}},我认为您还需要在Procfile中定义端口。

这是我的unicorn.rb文件在Unicorn上正常运行,以及相应的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

Procfile

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