Forum: Ruby on Rails How to do Daily Database Maintenance in a Rails Way?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
F12e71096774b459e8e8f61b0c289945?d=identicon&s=25 newbie (Guest)
on 2007-06-02 13:07
(Received via mailing list)
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?

Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2007-06-02 16:47
When I've had to do this sort of thing in the past, I've always created
a controller that had actions that did the maintenance. Then, in order
to make it happen in the background, I would create a cron job that used
wget to request that controller/action. This gives you a lot of

The other option is to use script/runner and stick that in a cron job.
Whatever floats your boat, really.
8d1e089fd6a6703f5f13dbee271aa77c?d=identicon&s=25 Yuri Leikind (Guest)
on 2007-06-03 01:25
(Received via mailing list)
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


On 6/2/07, newbie <> wrote:
> How can I do it in Rails?
> Thanks!
> >

Best regards,
Yuri Leikind
8d1e089fd6a6703f5f13dbee271aa77c?d=identicon&s=25 Yuri Leikind (Guest)
on 2007-06-03 01:27
(Received via mailing list)
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.


On 6/2/07, Bryan Duxbury <> wrote:
> --
> Posted via
> >

Best regards,
Yuri Leikind
4ca0194155a23e622c2686f9215ac414?d=identicon&s=25 David Altenburg (Guest)
on 2007-06-03 09:12
(Received via mailing list)
On Jun 2, 2007, at 6:25 PM, Yuri Leikind wrote:

> rake my_task

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 (http:// It is designed for offloading long-
running tasks from your Rails server, and it includes scheduling
capabilities (similar to cron).


David L Altenburg
This topic is locked and can not be replied to.