Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 490
Stefan Avatar asked May 22 '09 12:05

Stefan


1 Answers

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

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

like image 153
ujh Avatar answered Sep 23 '22 04:09

ujh