Thursday, April 21, 2011

Ruby rufus-scheduler on Rails

I've been playing around with the rufus-scheduler, trying to make it work in a rails environment.
The problem I was facing was that the new threads that were created by the scheduler for running the jobs were somehow detached from the the Active Record environment resulting in a failure to load my application models within their scope.

Every time I tried loading a model object from the database I would get an error that the copy of my job instance "has been removed from the module tree but is still active"

Pretty confusing

My cowboy programming solution for this was to activate a block as the job instance and in it invoke a controller. from the controller, I managed to access everything I needed in terms of active support.

The code as following:

scheduler.every "10s" , :timeout => "1m", :tags => "etl_job" do |job|
  url = "#{server_url}?tag=#{job.params[:tags]}"
The run is defined as:
    logger.debug "--- Initiating Job with:  #{url} "
    uri = URI.parse(url)
    http =, uri.port)
    http.use_ssl = true if uri.scheme == 'https'
    request =
    response = http.request(request)
The url hits a controller and from there everything acts normally for rails.

Independent on Sundays

No comments:

Post a Comment

Please do not post spam on this blog, Spam sites will be reported to google.
thank you kindly.