Ruby on Rails: How to run things in the background?

Stefan picture Stefan · May 22, 2009 · Viewed 35.6k times · Source

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?

in my model:

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

Answer

ujh picture ujh · May 22, 2009

You should definitely check out the following Railscasts:

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