Heroku在部署后仅运行一次查询

时间:2012-01-05 17:17:29

标签: ruby-on-rails ruby-on-rails-3.1 heroku

我正在写一个Rails应用程序&目前我正在制作站点地图生成器。一切都很好,在我的电脑上仍然很好。但是当我将它部署到Heroku时,我遇到了问题。

生成器工作非常简单:它只写一个资源的所有URL,代码如下:

控制器:

class SitemapController < ApplicationController
    layout nil

    def index
        @solutions = Solution.find(:all, :order => "updated_at DESC") 
        @base_url = "http://#{request.host_with_port}"
        headers['Content-Type'] = 'application/xml'
        def index
            respond_to do |format|
                format.html
                format.xml
            end
        end
    end
end

查看:

<% if @solutions %>
    <url>
        <loc><%= "#{@base_url}#{solutions_path}" %></loc>
        <lastmod><%= @solutions.first.updated_at.to_s(:sitemap) %></lastmod>
        <priority>0.6</priority>
    </url>
    <% @solutions.each do |solution| %>
    <url>
        <loc><%= "#{@base_url}#{url_for(solution)}" %></loc>
        <lastmod><%= solution.updated_at.to_s(:sitemap) %></lastmod>
        <priority>0.5</priority>
    </url>
<% end %>

正如我已经说过的那样,在本地它可以正常工作,但是在Heroku上,预期结果在部署后只显示一次,所有其他时间都没有显示链接,因为没有选择解决方案。

以下是日志:

2012-01-05T16:32:34+00:00 app[web.1]: Started GET "/sitemap.xml" for 194.44.214.138 at 2012-01-05 16:32:34 +0000
2012-01-05T16:32:34+00:00 app[web.1]: 
2012-01-05T16:32:34+00:00 app[web.1]:   Processing by SitemapController#index as XML
2012-01-05T16:32:34+00:00 app[web.1]:   Solution Load (5.1ms)  SELECT "solutions".* FROM "solutions" ORDER BY updated_at DESC
2012-01-05T16:32:34+00:00 app[web.1]:    (1.4ms)  SHOW search_path
2012-01-05T16:32:34+00:00 app[web.1]: Rendered sitemap/index.xml.erb (19.4ms)
2012-01-05T16:32:34+00:00 app[web.1]: Completed 200 OK in 92ms (Views: 70.4ms | ActiveRecord: 21.3ms)
2012-01-05T16:32:34+00:00 app[web.1]: cache: [GET /sitemap.xml] miss
2012-01-05T16:32:40+00:00 app[web.1]: 
2012-01-05T16:32:40+00:00 app[web.1]: 
2012-01-05T16:32:40+00:00 app[web.1]: Started GET "/sitemap.xml" for 194.44.214.138 at 2012-01-05 16:32:40 +0000
2012-01-05T16:32:40+00:00 app[web.1]:   Processing by SitemapController#index as XML
2012-01-05T16:32:40+00:00 app[web.1]: Rendered sitemap/index.xml.erb (0.0ms)
2012-01-05T16:32:40+00:00 app[web.1]: Completed 200 OK in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms)
2012-01-05T16:32:40+00:00 app[web.1]: cache: [GET /sitemap.xml] miss
2012-01-05T16:32:40+00:00 heroku[router]: GET www.my.url/sitemap.xml dyno=web.1 queue=0 wait=0ms service=9ms status=200 bytes=299
2012-01-05T16:32:43+00:00 app[web.1]: 
2012-01-05T16:32:43+00:00 app[web.1]: 
2012-01-05T16:32:43+00:00 app[web.1]: Started GET "/sitemap.xml" for 194.44.214.138 at 2012-01-05 16:32:43 +0000
2012-01-05T16:32:43+00:00 app[web.1]:   Processing by SitemapController#index as XML
2012-01-05T16:32:43+00:00 app[web.1]: Rendered sitemap/index.xml.erb (0.0ms)
2012-01-05T16:32:43+00:00 app[web.1]: Completed 200 OK in 0ms (Views: 0.3ms | ActiveRecord: 0.0ms)
2012-01-05T16:32:43+00:00 app[web.1]: cache: [GET /sitemap.xml] miss
2012-01-05T16:32:43+00:00 heroku[router]: GET www.my.url/sitemap.xml dyno=web.1 queue=0 wait=0ms service=7ms status=200 bytes=299
2012-01-05T16:32:44+00:00 app[web.1]: 
2012-01-05T16:32:44+00:00 app[web.1]: 
2012-01-05T16:32:44+00:00 app[web.1]: Started GET "/sitemap.xml" for 194.44.214.138 at 2012-01-05 16:32:44 +0000
2012-01-05T16:32:44+00:00 app[web.1]:   Processing by SitemapController#index as XML
2012-01-05T16:32:44+00:00 app[web.1]: Rendered sitemap/index.xml.erb (0.0ms)
2012-01-05T16:32:44+00:00 app[web.1]: Completed 200 OK in 1ms (Views: 0.4ms | ActiveRecord: 0.0ms)
2012-01-05T16:32:44+00:00 app[web.1]: cache: [GET /sitemap.xml] miss
2012-01-05T16:32:44+00:00 heroku[router]: GET www.my.url/sitemap.xml dyno=web.1 queue=0 wait=0ms service=30ms status=200 bytes=299

因此,您可以看到Solution Load仅在第一次请求时发出,在其他请求期间跳过。 你知道这是什么问题吗?

顺便说一句,我还没有尝试过以前的版本,但是从谷歌网站管理员工具看来,一切都很好。这可能是由我所做的格式更改引起的吗?我在

中添加了下一行

配置/初始化/ time_formats.rb

Time::DATE_FORMATS[:sitemap] = "%Y-%m-%d"

并在控制器中

.to_s(:sitemap)

方法调用updated_at

我会非常感谢任何回复或暗示。

1 个答案:

答案 0 :(得分:2)

您的控制器代码是各种果味。出于某种原因,你有def index两次(在其内部)。你应该使用:

def index
    @solutions = Solution.find(:all, :order => "updated_at DESC") 
    @base_url = "http://#{request.host_with_port}"
    headers['Content-Type'] = 'application/xml'
    respond_to do |format|
      format.html
      format.xml
    end
end