Installing and Using RailsCron
Posted 07/04/2006 by dnaffis 3 CommentsAdd Your Comment
I recently started using Kyle Maxwell’s RailsCron for a couple Ruby on Rails apps I’ve been playing with. Cron turned out to be a headache in my case and I wanted ActiveRecord support without having to jump through any hoops.
Some of the shortcomings of Cron with RoR (from the RailsCron readme):- Significant startup resources required
- Lots of RAM to run simultaneous processes
- Hard to start/stop/affect the background processes from within Rails.
The documentation is sparse and there are very few examples to be found online.
This is straight from the readme:RailsCron.create(
:command => "Object.do_something()",
:start => 2.minutes.from_now,
:every => 12.hours, # default: 1.day
:finish => 2.years.from_now # optional
)
RailsCron.destroy_all
RailsCron.find_by_command "some_command"
There are a couple ways to install. Here’s what I did. From the root of your rails app run:
./script/plugin install http://svn.kylemaxwell.com/rails_cron/trunk
I set it up to run in the background. In my model I put the following:
class User < ActiveRecord::Base
background :update_accounts, :every => 1.minute, :concurrent => false
def self.update_accounts
# do some scheduled processing
end
end
The above will run the update_accounts method every minute. You can set it up so that it can run separate threads concurrently if it’s still working on the old one. It didn’t make sense in my case so I used :concurrent => false.
It has the following set of rake tasks:- cron_start—Starts RailsCron as a daemon
- cron_foreground—Starts RailsCron in the foreground
- cron_stop—Graceful stop
- cron_kill
- cron_graceful—Graceful restart
- cron_status
For example to stop then restart RailsCron you can run the following:
rake cron_graceful
That’s it. It should create a table in your database to keep track of the tasks.
April 17th, 2007 at 01:21 PM Had some trouble getting this to work with SQLServer. Later I changed the command column type from text to varchar and things were in place.
April 17th, 2007 at 01:21 PM The URL for railscron has changed. The new URL is http://svn.kylemaxwell.com/not_supported/rails_cron/trunk/README
April 17th, 2007 at 01:21 PM Kyle Maxwell posted today that RailsCron is deprecated for many reasons, and he recommends daemon_generator. I switched an app today, and I have to agree that it's a lot less work and a lot nicer.