如何使控制器等待延迟的作业,而应用程序的其余部分继续? [英] How to Make the Controller wait for a Delayed Job while the rest of the App continues on?

查看:161
本文介绍了如何使控制器等待延迟的作业,而应用程序的其余部分继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(此问题是

(This question is a follow-up to How do I handle long requests for a Rails App so other users are not delayed too much? )

用户提交对我的Rails应用程序的答案,它在后端检查长达10秒。这会对所有其他用户造成延迟,因此我尝试 delayed_job gem将检查移至一个工作进程。 Worker代码将结果返回给控制器。然而,控制器没有意识到它应该耐心等待结果,所以它会导致错误。

A user submits an answer to my Rails app and it gets checked in the back-end for up to 10 seconds. This would cause delays for all other users, so I'm trying out the delayed_job gem to move the checking to a Worker process. The Worker code returns the results back to the controller. However, the controller doesn't realize it's supposed to wait patiently for the results, so it causes an error.

如何让控制器等待结果,让其余应用程式同时处理简单要求?

在Javascript中,将使用回调函数来调用函数,而不是返回一个值。

How do I get the controller to wait for the results and let the rest of the app handle simple requests meanwhile?
In Javascript, one would use callbacks to call the function instead of returning a value. Should I do the same thing in Ruby and call back the controller from the Worker?

更新:

或者,如何从Worker调用控制器方法?然后我可以调用相关的动作时,它的完成。

Update:
Alternatively, how can I call a controller method from the Worker? Then I could just call the relevant actions when its done.

这是相关代码:

控制器:

def submit
  question = Question.find params[:question]
  user_answer = params[:user_answer]
  @result, @other_stuff = SubmitWorker.new.check(question, user_answer)
  render_ajax 
end

submit_worker。 rb:

submit_worker.rb :

class SubmitWorker
 def check
   #lots of code... 
 end
 handle_asynchronously :check 
end


推荐答案

您可以添加某种形式的回调到结束检查方法,以便当作业完成时,您的用户可以得到通知。

You can add some form of callback to the end of your check method so that when the job finishes your user can be notified.

您可以在此问题中找到有关执行通知的一些讨论: push-style通过Rails和jQuery向Facebook发送通知

You can find some discussion on performing notifications in this question: push-style notifications simliar to Facebook with Rails and jQuery

或者,您可以让浏览器定期调用控制器操作来检查作业的结果 - 理想情况下是一个ActiveRecord对象。同样,您可以在此问题中找到有关定期javascript的讨论: Rails 3等同于periodic_call_remote

Alternatively you can have your browser periodically call a controller action that checks for the results of the job - the results would ideally be an ActiveRecord object. Again you can find discussion on periodic javascript in this question: Rails 3 equivalent for periodically_call_remote

这篇关于如何使控制器等待延迟的作业,而应用程序的其余部分继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆