views:

406

answers:

2

Hello,

I'm using Delayed Job to manage background work.
However I have some tasks that need to be executed at regular interval. Every hour, every day or every week for example.

For now, when I execute the task, I create a new one to be executed in one day/week/month.

However I don't really like it. If for any reason, the task isn't completely executed, we don't create the next one and we might lose the execution of the task.

How do you manage that kind of things (with delayed job) in your rails apps to be sure your regular tasks list remains correct ?

+1  A: 

Is there any particular reason why you wouldn't use cron for this type of things? Or maybe something more rubyish like rufus-scheduler, which is quite easy to use and very reliable.

If you don't need queuing, these tools are a way to go, I think.

Milan Novota
Well I already have the delayed job deamon. And I'd like to avoid launching a second one.
Damien MATHIEU
You don't need to launch any new daemon with rufus-scheduler. It uses simple loop or EventMachine to check for jobs to be run. cron is a daemon, but you usually need it up and running anyway.
Milan Novota
+1  A: 

If you have access to Cron, I highly recommend Whenever

http://github.com/javan/whenever

You specify what you want to run and at what frequency in dead simple ruby, and whenever supplies rake tasks to convert this into a crontab and to update your system's crontab.

If you don't have access to frequent cron (like I don't, since we're on Heroku), then DJ is the way to go.

You have a couple options.

  1. Do what you're doing. DJ will retry each task a certain number of times, so you have some leniency there

  2. Put the code that creates the next DJ job in an ensure block, to make sure it gets created even after an exception or other bad event

  3. Create another DJ that runs periodically, checks to make sure the appropriate DJs exist, and creates them if they don't. Of course, this is just as error prone as the other options, since the monitor and the actual DJ are both running in the same env, but it's something.

Mike H
Nice thing, whenever. With some tasks to allow me to reload the crontab when I change something (I need to change tasks dynamically). Thanks :)
Damien MATHIEU