Do you write rails apps? Have you ever wanted to automate rake tasks with cron? Easy peasy, right? Well, its not that hard for the most part, but there are some issues to think about:
- What happens if you try to re-deploy your rails app while your cronned tasks are running?
- What happens if you migrate your database while a long-running cron is processing?
- What happens if you get the cron syntax wrong? Ever have a script start every second when it should run once a day?
- Ever have a cron task start before the previous one had completed?
- Ever try to find your cronned task's log intermixed with your controller log entries?
- Ever want to change your cron entries, and think its ugly that everything else about your deploy is automated?
Wouldn't it be great to solve all these issues with one gem? Crondonkulous!
install the gem:
gem sources -a http://gems.github.com
gem install bokmann-crondonkulous
in your rails app, generate the files:
The generate will give you some further instructions - in a nutshell:
require 'crondonkulous/recipes'to the top of your deploy.rb cap file
config.gem "lockfile"to your environment.rb
- uncomment whatever cron entries you want to run in the generated config/crontab.erb file
- write your periodic tasks where it says 'your code here' in the
Pretty easy, right?
What you get:
- a lib/tasks/crondonkulous.rake file that you add your code to. Stubbed out tasks include every_minute, every_5_minutes, every_15_minutes, every_hour, every_day, every_week, and every_month.
- a config/ctontab.erb file that gets rendered and put into the cron table. This file already includes the proper syntax for tasks that run every minute, every 5 minutes, every 15 minutes, hourly, daily, weekly, and monthly.
- conventions for naming/logging. Each periodic task gets its own log file, like
- protection via the LockFile gem. crondonkulous rake tasks won't run at the same time (unless you let them), and will wait/retry to run the number of times you specify.
- Capistrano integration to remove the old cron entries while deploying and insert new cron entries before application restart
A while back I wrote the 'Automate Repeatable Tasks' recipe (#72) for the 'Advanced Rails Recipes' book. Those ideas, and more, are embodied in this gem. In particular, the management of entries into the cron tab is a lot easier. I have done this for various apps time and time again, and wanted to promote some common conventions.
Crondonkulous inserts the crontab entries into the user doing the deployment by default. When I have total control over the hosting environment, I prefer to create a user specifically for the application and install the cron entries there. you can add:
set :cron_user, "my_app_user"
to your deploy.rb if you want to do the same.
You can follow the project on github.