向 ajax 进程指示延迟作业已完成

发布于 2024-10-18 04:59:15 字数 206 浏览 5 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三岁铭 2024-10-25 04:59:15

通常,执行此操作的最佳方法是在数据库中存储作业进度的指示。例如:

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

这样,当您将作业排入队列时:

User.update_attributes :calculation_status => 'enqueued'
User.send_later :perform_calculation

您可以在控制器中查询作业的状态:

def check_status
  @user = User.find(params[:id])
  render :json => @user.calculation_status
end

然后,轮询 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:

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

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.

失退 2024-10-25 04:59:15

有了这个 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_progress

Completed jobs are no longer automatically removed, so you can poll against a job until it comes back with completed state.

得不到的就毁灭 2024-10-25 04:59:15

如果您使用任何 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文