Hello whenever, goodbye manual crontab

whenever cron ruby capistrano

01|30|2010

Do you hate managing cron configurations manually? How about maintaining different cron configs for every Rails application you might possibly have?

Wheneverize

Say hello to a ruby gem called whenever. It basically allows you to configure your cron jobs in ruby. An example to illustrate:

# assuming you have gemcutter in your list of sources
sudo gem install whenever

# in your rails application
wheneverize .

# now edit config/schedule.rb
vi config/schedule.rb

Now you're presented with a very easy to understand Ruby DSL. No more reading cron manuals, it's very straightforward and intuitive.

set :output, "/opt/myapp.com/current/log/cron_log.log"

every :reboot do
  rake "solr:start"
end

every :friday, :at => '8am' do
  rake "newsletters:deliver" 
end

every 1.day, :at => '12am' do
  rake "harvester:analyze"
end

Now this ruby file will not be enough to run itself. Fortunately, if you're using capistrano, updating your cron config after every deploy is dead simple:

# in your config/deploy/production.rb maybe

namespace :deploy do
  desc "Update the crontab file"
  task :update_crontab, :roles => :db do
    run "cd #{release_path} && whenever --update-crontab #{application}"
  end
end

after "deploy:restart", "deploy:update_crontab"

Say goodbye to minding crontabs saved on different servers and say hello to a well maintained and managed configuration in your rails app.

blog comments powered by Disqus