ed1t
ed1t

Reputation: 8699

scheduling tasks in rails 3 app

I'm writing an rails 3 application which requires performing small tasks on a custom schedule for each user. The scheduled tasks will be defined dynamically. Right now my plan is to use resque scheduler with redis.

Once I set the schedule for a specify task (for eg. run task A every 48 hours) I would like to run that task indefinitely. So I would like to store those schedules in a db or something so in case an app crashes when it restarts it would load queue those task again.

Is this something Resque supports by default by storing it in redis or do I need to write my own custom thing? I was also looking at ruby-taskr (http://code.google.com/p/ruby-taskr/). I am not sure if taskr supports storing it in a database and registering it on start?

Also it would be helpful if there are applications/demo that I can look at it.

Thanks

Upvotes: 1

Views: 860

Answers (1)

Matthew Rathbone
Matthew Rathbone

Reputation: 8269

I have a similar setup for batch jobs. The user adds them on a web dashboard and they get run however often is specified.

I use active-record to store the scheduling definitions, use resque for execution and a single cron entry for enqueueing using a rake task.

so then in the rake task:

to_run = Report.daily
to_run += Report.weekly if Time.now.monday?
to_run += Report.monthly if Time.now.day == 1

to_run.each{|r| r.enqueue!}

where daily, weekly, monthly are named scopes on the model:

class Report < ActiveRecord::Base
  scope :daily, where(:when_to_run => 'daily')
  scope :weekly, where(:when_to_run => 'weekly')
  scope :monthly, where(:when_to_run => 'monthly')
end

This is a little hacky, but it works well and I stay within the stack nicely. Hope that is useful

Upvotes: 1

Related Questions