Monday, March 9, 2009

Schedule using Rufus scheduler

We learned how to create Rake tasks in rails app. Next thing in process will be scheduling these tasks.

I opted for Rufu-Scheduler, mainly bcs it is quite simple to implement/configure

You can download the gem directly from :

There some good resources in net which describes usage of Rufus scheduler.

Like :

But some how I got myself into trouble in getting this to working ;(

Following are questions that bubbled in my mind while implementing Rufus.

>> Where do I put the scheduler code?

>> How will rails trigger my tasks, do I need to include something in my config. file?

I created a file in my /lib dir named myscheduler.rb, this contains the scheduler code and inorder to allow rails to load your rufus scheduler, you have to do

require ' myscheduler'

in environment.rb

My myscheduler.rb has

require 'rubygems'
require 'rake'
require 'rufus/scheduler'
load File.join( RAILS_ROOT, 'lib',
'tasks', 'mytasks.rake')

# Note include file containing your rake tasks
# Else you will get - Don’t know how to build
#task XXXXX’ error for the tasks you
#have defined

scheduler = Rufus::Scheduler.start_new

scheduler.every("1m") do
puts "Scheduler invoked"

restart the server and check for 'Scheduler invoked' in your CMD.

Got it ......... your half done with your scheduling, take a break ;)


Anonymous said...

This post was extremely helpfull!
This is exactly what I was looking for.

Anonymous said...

Do you know if it is possible to call a method which is defined in my application_controller from 'myscheduler.rb'? If I try so I get a undefined method error.

Anonymous said...

That is exactly what I wanted to know. If i have a method in my players_controller, can I call it in rufus scheduler or does the method have to be somewhere else?