表明该延迟作业已完成一个ajax过程 [英] Indicate to an ajax process that the delayed job has completed
问题描述
我有一个Rails 3应用程序,使用delayed_job的获取一些数据(一类方法),当用户点击该页面。 我怎么能指示该类方法运行(这样我就可以停止轮询)的AJAX程序?
I have a Rails 3 app that uses delayed_job to fetch some data (with a class method) when the user hits the page. How can I indicate to the ajax process that the class method has run (so that I can stop polling)?
编辑:
要澄清一下,我想知道什么时候的delayed_job已经运行,而不是当AJAX过程已成功完成。然后,我想通过完成状态到正在运行的AJAX过程中的delayed_job的。
To clarify, I want to know when the delayed_job has run, not when the ajax process has succeeded. Then I want to pass the delayed_job completed status to the running ajax process.
推荐答案
通常情况下,要做到这一点的最好办法是存储在数据库中,作业进度的指示。例如:
Typically, the best way to do this is to store, in your database, an indication of the job's progress. For instance:
class User
def perform_calculation
begin
self.update_attributes :calculation_status => 'started'
do_something_complex
self.update_attributes :calculation_status => 'success'
rescue Exception => e
self.update_attributes :calculation_status => 'error'
end
end
end
所以,当你排队的任务:
So that when you enqueue the job:
User.update_attributes :calculation_status => 'enqueued'
User.send_later :perform_calculation
您可以查询,在你的控制器,该作业的状态:
You can query, in your controller, the status of the job:
def check_status
@user = User.find(params[:id])
render :json => @user.calculation_status
end
然后,您查询阿贾克斯过程可以简单地调用check_status看到作业进展如何,如果它成功了,或者如果它已经失败。
You polling ajax process can then simply call check_status to see how the job is progressing, if it has succeeded or if it has failed.
这篇关于表明该延迟作业已完成一个ajax过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!