简体   繁体   English

如何从控制器动作中向Rails动态添加after过滤器?

[英]How do I dynamically add an after filter to Rails from within a controller action?

Each time a user accesses a page in my Rails app, the database needs to send them a Thingy. 每次用户访问我的Rails应用程序中的页面时,数据库都需要向他们发送一个Thingy。 When the database runs out of Thingies, it has to do some expensive processing to generate more. 当数据库用完Thingies时,它必须执行一些昂贵的处理才能生成更多数据。 I want to add a controller filter dynamically to generate the thingies after the response has been sent to the user so that it doesn't affect page load times. 我想动态添加一个控制器过滤器,以将响应发送给用户之后生成thingies 以免影响页面加载时间。 Here's what my controller looks like: 这是我的控制器的外观:

class ThingyController < ApplicationController

  def get_a_thingy
    if Thingy.count <= 5 
      # Only five thingies left! we need to generate some more. 
      # I want to dynamically send a block to an after_filter: here to  
      # generate thingies after the controller sends the response  
      # because generating thingies is really slow
    end

    # Pop the first thingy from the database and return
    thingy = Thingy.first
    thingy.delete 
    return thingy.content
  end

What can I add in the get_a_thingy function to make this happen? 我可以在get_a_thingy函数中添加些什么来实现此目的?

You may try some background processing tools( https://www.ruby-toolbox.com/categories/Background_Jobs check this), cause I'm not sure you can do it inside a request handler. 您可以尝试使用某些后台处理工具( https://www.ruby-toolbox.com/categories/Background_Jobs进行检查),因为我不确定您是否可以在请求处理程序中执行此操作。

You may also try to return all the content to user (via smthing like http streaming) and only then make your heavy things. 您还可以尝试将所有内容返回给用户(通过诸如HTTP流媒体之类的东西),然后再进行繁重的工作。

Probably the best idea will by a gem like delayed_job there is also a railscast for it. 也许是最好的想法会被像宝石的delayed_job也有railscast它。

Delayed job is an asynchronously priority queue system. 延迟的作业是异步优先级队列系统。

  • Install and setup the delayed_job. 安装并设置delay_job。 It is pretty well documented on the github page . 它在github 页面上有很好的记录。

  • Start the worker with rake jobs:work 开始rake jobs:work的工人rake jobs:work

  • Now simply change your code to use the delay job by adding the .delay method 现在,只需添加.delay方法,即可更改代码以使用延迟作业

class ThingyController < ApplicationController
  after_filter :generate_thingies
  .
  .
  .
  def generate_thingies
    if Thingy.count <= 5
      #change
      Thingy.generate_thingies
      #to
      Thingy.delay.generate_thingies
    end
  end
end

NOTE: This is a small tutorial and I left out a some of things that you will need to get it working. 注意:这是一个小教程,我省略了一些使它正常运行所需的内容。 I recommend you to check the Github page for a full documentation. 我建议您查看Github页面以获取完整的文档。

Just put the if statement that checks for the number of thingies in your after_filter code: 只需在after_filter代码中放入if语句,以检查事物的数量:

class ThingyController < ApplicationController
  after_filter :generate_new_thingies

  def get_a_thingy
    thingy = Thingy.first
    thingy.delete
    return thingy.content
  end

  def generate_new_thingies
    if Thingy.count <= 5
      # Generate thingies
    end
  end
end

Does using after_filter really prevent long page loads? 使用after_filter真的可以防止长页面加载吗? It may be better to have a look at something like Delayed Job or Whenever . 最好看一下“ 延迟工作”或“ 无论何时”之类的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Rails的控制器中访问路由信息(HTTP动词,动作名称等)? - How do I access the route information (HTTP verb, action name, etc . . .) from within a controller in Rails? 如何从控制器中获取当前操作? - How do I get the current action from within a controller? 如何从控制器动作中调用另一个控制器动作? - How do I call another controller action from within a controller action? Rails - 如何避免必须从另一个控制器中的create action触发一个控制器中的create操作 - Rails - how do I avoid having to trigger the create action in one controller from the create action in another controller 如何在Rails 4.2中的动作中呈现不同的控制器/动作? - How to render a different controller / action from within an action in Rails 4.2? 如何创建Rails控制器动作? - How do I create a rails controller action? 在具有Railscasts的自定义异常处理程序(#53)的Rails 4应用程序中,如何在控制器操作中将某人强制为404 - In a Rails 4 app with Railscasts' custom exception handler (#53), how do I force someone to a 404 within a controller action 如何从Rails 3.07中的过滤器中排除单个控制器操作? - How to exclude a single controller action from a filter in Rails 3.07? 如何在Rails 4中向控制器添加线程? - How do I add a Thread to a controller in Rails 4? 如何从其控制器内部调用动作,以便其过滤器也被隐式调用? - How do I call an action from within its controller such that its filters are also called implicitly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM