Rails 4自定义404页面导致在Heroku上与PostgreSQL的连接失败

3

我有一个Rails 4应用程序,已经部署在Heroku上作为生产环境,并使用自定义域名。我还有一个staging版本。该应用程序使用Comfortable Mexican Sofa。

出现以下问题:应用程序将达到一种状态,其中所有请求都会返回500错误。日志显示:

[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)

尝试连接的后续操作导致500错误,数据库连接保持为0。

在使用此指南创建自定义错误页面后,出现了此问题:http://wearestac.com/blog/dynamic-error-pages-in-rails

我可以通过创建一个使用数据库的404页面,然后向服务器发出大约5或6个请求来强制出现此问题,请求的页面不存在。

编辑:

我可以通过使用静态自定义404页面并发出5或6个对具有jpeg扩展名的不存在文件的请求来强制出现问题。日志中出现以下内容:

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

任何帮助解决这个问题都将不胜感激。提前致谢。

1
它试图对JPEG图像做出404响应。因此,它正在寻找not_found.jpg.erb模板。显然你没有那个。我不知道Heroku是如何做到的,但当提供静态内容(如图像、CSS、JS等)时,你确实希望防止它触及Rails堆栈。 - Grocery
啊,你可能想要安装这个:https://github.com/heroku/rails_serve_static_assets - Grocery
谢谢 Grocery。在 respond_to 块中处理不同的格式解决了问题。 - laertiades
1
你能把解决方案移到答案区吗?这样它就会显示为已回答。 - Anson
1个回答

1
这里是功能性控制器的代码:
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接