Background jobs with Redis and Resque
December 12, 2010
Today we will talk about various technologies that can be used to schedule background jobs using a Redis datastore and Resque to manage them, we will assume that you have already installed redis in your machine.
Installing resque
Install the resque gem, this will also pull the redis client library if you didnt had it oun your system
Running redis
Start the resque web interfase
Integrating Redis and Resque with Rails
Lets create a redis.yml
configuration file
Now lets add the Resque initializer
and finally we need to create a resque rake task
Using Resque
Create a WorkerClass
WorkerClass
will be the class that will be called by Resque to dequeue and execute the job with the given parameters
it needs to be told which queue within Resque belongs to, and it must implement a perform method, which will be executed by Redis when a new job is retrieved
Launch Resque worker
Finally lets tell our worker to be ready to accept new jobs
Put a new job in the queue
Jobs scheduled at future dates
Resque can create both delayed jobs and recurring ‘cron style’ jobs
Creating recurring jobs
Create a job to be executed at exactly one set date
this will store the job for 5 days in the resque delayed queue at which time the scheduler process will pull it from the delayed queue and put it in the appropriate work queue for the given job and it will be processed as soon as a worker is available.
Canceling a scheduled delayed job
Resque-scheduler is another option to queue items to be executed in the future
Resque-scheduler is an extension to Resque that adds support for queueing items in the future.
####Installing resque-scheduler
Recurring (or scheduled) jobs are logically no different than a standard cron job
####Scheduling a job
Jobs are stored in the resque_schedule.yml
file
####Load the scheduler and scheduled jobs from your application
####Run the scheduler process
- Really gentle introduction to Resque
- A Resque & Redis integration guide with Rails
- Get Rescue-scheduler from its project page
- Ohm is a Object-Hash mapping library for redis