How to do Daily Database Maintenance in a Rails Way?

I am learning Rails and came across the following problem.

I need to do some daily maintenance to the MySQL database. By
maintenance, I mean reading data from one table, do some calculation,
and put the result in another table. This should happen everyday at a
specified time, e.g., at mid-night, no matter if there is any visitor
visiting the site or not. Ideally this should happen quietly in the
background without any human intervention.

How can I do it in Rails?

I understand that in some other environments like PHP I could do it in
at least two ways:

1. Write a stored procedure for the daily maintenance;
2. Write a standalone script for the daily maintenance and use cron to
schedule it to run once per day;

But these two are pretty un-Rails. So what should I do for tasks like
this in a Rails way?


I guess the Rails way is creating a rake task that depends on rails
environment (i.e. boots the Rails runtime), doing there everything you
need using ActiveRecord and then calling this rake task from cron

task :my_task => :environment
   # Do whatever you need

calling it:

rake my_task


Sorry, but isn't it ugly, using wget to trigger some logic when you
have script/runner and rake tasks? It's like driving from NY to
Washington via LA.


I think that's a fine solution. Cron is great as scheduling, and you can still use your Rails environment definitions (and ActiveRecord, if necessary).
You may be interested in BackgrounDrb ( It is designed for offloading long-running tasks from your Rails server, and it includes scheduling capabilities (similar to cron).


David L Altenburg