向 ajax 进程指示延迟作业已完成
我有一个Rails 3应用程序,当用户点击页面时,它使用delayed_job来获取一些数据(使用类方法)。 如何向 ajax 进程指示类方法已运行(以便我可以停止轮询)?
编辑:
澄清一下,我想知道delayed_job何时运行,而不是ajax进程何时成功。然后我想将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)?
EDIT:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,执行此操作的最佳方法是在数据库中存储作业进度的指示。例如:
这样,当您将作业排入队列时:
您可以在控制器中查询作业的状态:
然后,轮询 ajax 进程可以简单地调用 check_status 来查看作业的进展情况,是否成功或是否已完成失败的。
Typically, the best way to do this is to store, in your database, an indication of the job's progress. For instance:
So that when you enqueue the job:
You can query, in your controller, the status of the job:
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.
有了这个 gem,您可以直接在
Delayed::Job
对象本身上进行进度跟踪:https: //github.com/GBH/delayed_job_progress已完成的作业不再自动删除,因此您可以对作业进行轮询,直到它以
completed
状态返回。With this gem you can have progress tracking directly on the
Delayed::Job
object itself: https://github.com/GBH/delayed_job_progressCompleted jobs are no longer automatically removed, so you can poll against a job until it comes back with
completed
state.如果您使用任何 JavaScript 框架(例如prototypejs),那么在可选选项哈希中,您通常会提供 onComplete 和/或 onSuccess 回调。 API 参考
If you are using any JavaScript framework like prototypejs, then in the optional options hash, you usually provide a onComplete and/or onSuccess callback. API Reference