本文介绍了Ruby on Rails:如何在后台运行事物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建新资源后,在资源准备就绪之前需要进行一些冗长的处理,我该如何将该处理发送到后台,在其中不会阻止当前请求或我的网络应用的其他访问量?

When a new resource is created and it needs to do some lengthy processing before the resource is ready, how do I send that processing away into the background where it won't hold up the current request or other traffic to my web-app?

在我的模型中:

class User < ActiveRecord::Base
 after_save :background_check

 protected
 def background_check
  # check through a list of 10000000000001 mil different
  # databases that takes approx one hour :)
  if( check_for_record_in_www( self.username ) )
    # code that is run after the 1 hour process is finished.
    user.update_attribute( :has_record )
  end
 end
end

推荐答案

您绝对应该查看以下Railscast:

You should definitely check out the following Railscasts:

  • http://railscasts.com/episodes/127-rake-in-background
  • http://railscasts.com/episodes/128-starling-and-workling
  • http://railscasts.com/episodes/129-custom-daemon
  • http://railscasts.com/episodes/366-sidekiq

他们解释了如何以各种可能的方式(有或没有队列...)在Rails中运行后台进程

They explain how to run background processes in Rails in every possible way (with or without a queue ...)

这篇关于Ruby on Rails:如何在后台运行事物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 14:24