Rails控制器动作挂了

Rails controller action hanging

我是 ruby/rails 的新手,在过去的 2 天里一直被一个问题弄得头昏脑胀,我希望这个论坛中的某个人可以帮助我解决这个问题。

我正在开发 rails 4 和 运行 生产中的应用程序,使用 Passenger + Nginx 和 Mysql 数据库(mysql2 适配器)。我的 ProductsController.rb 中有以下代码(log_info 是我的自定义日志记录包装器,它只调用 Rails 记录器):

def index
    log_info("ProductsController", "Getting Products...")
    @products = Product.get_products(params[:offset])
    log_info("ProductsController", "Got all Products...")

    # populate access_url before sending the data through
    update_products_with_access_url(@products)
end

这里是产品模型中get_products函数的定义:

def self.get_products(products_offset)
    products_offset ||= 0

    Product.includes(:categories, :suppliers).
            where(active: true).order(updated_at: :desc).
            limit(20).
            offset(products_offset * 20)
end

问题是当调用索引操作时,我的应用程序在生产环境中挂起(在开发环境中运行良好)几分钟。这种行为几乎有一半的时间都在发生——另一半的时间它运行良好。

我可以在日志中看到操作已被调用但未执行。这是日志文件的片段:

I, [2015-06-10T13:31:01.802673 #14916]  INFO -- : Processing by ProductsController#index as HTML
I, [2015-06-10T13:33:09.718339 #14916]  INFO -- : [INFO][2015-06-10 13:33:09 UTC][ProductsController][Getting Products...]
I, [2015-06-10T13:33:09.719365 #14916]  INFO -- : [INFO][2015-06-10 13:33:09 UTC][ProductsController][Got all Products...]
D, [2015-06-10T13:33:09.720980 #14916] DEBUG -- :   Product Load (0.8ms)  SELECT  `products`.* FROM `products` WHERE `products`.`active` = 1  ORDER BY `products`.`updated_at` DESC LIMIT 12 OFFSET 0
D, [2015-06-10T13:33:09.725110 #14916] DEBUG -- :   ProductCategory Load (0.5ms)  SELECT `product_categories`.* FROM `product_categories` WHERE `product_categories`.`product_id` IN (30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19)

如您所见,上面日志文件的前两行至少相差 2 分钟。并且日志显示索引操作已收到调用,但我不确定为什么要花这么长时间(有时超过 5 分钟)来执行该索引操作中的第一行。我怀疑发生了某种缓存。我早些时候认为调用可能挂在与 MySQL 数据库的连接中,但现在不确定。我在这里错过了什么吗?

有人可以帮我解决这个应用程序挂起问题吗?

请检查是否有前置过滤器,通常它们可能是此类挂起的原因。