I, like many of you, have been searching for a good way to handle
running long processes in Rails. So I rolled my own solution. I
really want feedback! (I’ve tested this on very simple hello world
examples, so don’t use it on anything production yet) .
Grab the plugin:
http://opensvn.csie.org/rails_cron
Here’s the README:
OVERVIEW
RailsCron is a way to execute background tasks using your Ruby on
Rails environment. The RailsCron object is an ActiveRecord, so you
can manipulate it in familiar ways:
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"
Cron, when used with RoR, has the following shortcomings:
- Significant startup resources required
- Lots of RAM to run simultaneous processes
- Hard to start/stop/affect the background processes from within Rails.
RailsCron addresses these shortcomings by using one RoR instance with
threading (Ruby, native to be added later).
STARTING & STOPPING
RailsCron is started and stopped by Rake. List 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
These commands are UNIX-oriented. Windows users can start with “ruby
script\runner ‘RailsCron.start’” and stop with Ctrl-C. I have no idea
(or need) of how to make this a Windows service, but the patch would
be welcome.
The following environment variables are used by the Rake tasks:
- USER – The background job will run as this user. Default: current
user - RAILS_ENV – Default: development
- SLEEP – Time to sleep between checking for jobs Default: 1 second
INSTALLATION
RailsCron is a standard Ruby on Rails plugin. For information about
how to install plugins, type “ruby script/plugin --help” from within a
rails project.
RailsCron will add a table called ‘rails_crons’ to your database(s),
if it does not already exist.
BUGS & FEEDBACK
Bug reports (as well as patches) and feedback are very welcome. Please
send it to
[email protected]
–
Kyle M.
Chief Technologist
E Factor Media // FN Interactive
[email protected]
1-866-263-3261