[英]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.