views:

45

answers:

4

Hi,

I have to implement a fairly database intensive task periodically. I am using rails for the application to which this task is related and thus for the sake of easiness and uniqueness in approach I want to know how can I best implement cron job with rails. I am aware that rake is one solution, but am totally unaware of how to use it.

Thanks.

+3  A: 

javan-whenever is another good thing to look at.

http://github.com/javan/whenever

Gordon Isnor
A: 

rubber, a Capistrano plugin for cloud deployment, provides a set of helper scripts for managing cron jobs. These scripts will work completely independently of rubber, however.

If you grab the "cron-rake", "cron-runner", and "cron-sh" scripts from http://github.com/wr0ngway/rubber/tree/master/lib/generators/vulcanize/templates/base/script/ and place them in the script/ directory of your Rails project, you should be good to go. Don't forget to make them executable.

Most of the real work is done in "cron-sh." "cron-rake" is a convenience wrapper for running rake tasks. "cron-runner" is a convenience wrapper for running arbitrary Ruby (much like script/runner provides).

nirvdrum
will this prevent users from accessing the the url?
Amit
+1  A: 

Just implement your script to do whatever it is you need, and then invoke it with script/runner from a cron job.

0 * * * * cd /path/to_your/app && RAILS_ENV=production script/runner scripts/my_maintenance_script.rb

That'll kick off my_maintenance_script.rb in the context of your production environment once per hour. Pretty bare-metal, but it'll get the job done.

Chris Heald
the thing is, with this even users can run the script... that will overload the server.
Amit
A: 

delayed_job is good, it stores the details of the job in the database.

dagda1
delayed_job is good for running a particular task at a particular time. Not for repetitive tasks. Of course, you can schedule the job's next run at the end of each job. But that'll confuse matters when a job fails and is re-run.
Vijay Dev