Rails 4 custom 404 causes postgresql connection failure on Heroku

陌路散爱 提交于 2019-12-10 22:04:50

问题


I have a Rails 4 app deployed on Heroku in production with a custom domain. I also have a staging version. The app uses Comfortable Mexican Sofa.

The following problem occurs: The app will reach a state where all requests return a 500 error. Logs show:

[jesse@Athens expat]$ heroku logs
ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5.000 seconds (waited 5.000 seconds)):

[jesse@Athens expat]$ heroku pg:info
Connections: 5

[jesse@Athens expat]$ heroku pg:ps
 pid | state | source | running_for | waiting | query 
-----+-------+--------+-------------+---------+-------
(0 rows)

[jesse@Athens expat]$ heroku pg:killall
 pg_terminate_backend 
----------------------
 t
 t
 t
 t
 t
(5 rows)

Subsequent attempts to connect result in a 500 error and db connections remain at 0.

This issue cropped up after I created custom error pages using this guide: http://wearestac.com/blog/dynamic-error-pages-in-rails.

I can force the issue by creating a 404 page which uses the database and then issuing about 5 or 6 requests to the server for nonexistant pages.

Edit:

I can force the problem while using a static custom 404 page by issuing 5 or 6 requests for nonexistent files with a jpeg extension. The following appears in the logs:

Error during failsafe response: Missing template errors/not_found, application/not_found with {:locale=>[:en], :formats=>[:jpeg], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee, :haml]}. Searched in:
2014-02-17T17:26:14.595469+00:00 app[web.1]:   * "/app/app/vi
ews"
2014-02-17T17:26:14.594508+00:00 app[web.1]: Completed 500 Internal Server Error in 7ms

Any help isolating this problem would be greatly appreciated. Thanks in advance


回答1:


Here is the functional controller code:

class ErrorsController < ApplicationController
  def not_found
    respond_to do |format|
      format.any(:htm, :html, :xls, :xlsx) { render :status => 404, :layout => "error_frame", :formats => [:html] }
      format.all { render nothing: true, status: 404 }
    end
  end

  def unacceptable
    respond_to do |format|
      format.html { render :status => 422, :layout => "error_frame" }
      format.all { render nothing: true, status: 422 }
    end
  end

  def internal_error
    respond_to do |format|
      format.html { render :layout => false, :status => 500 }
      format.all { render nothing: true, status: 500}
    end
  end
end


来源:https://stackoverflow.com/questions/21834377/rails-4-custom-404-causes-postgresql-connection-failure-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!