Thursday, February 26, 2009

Whenever: an easy way to do cron jobs from Ruby

Since Inkling got started over 3 years ago, we have always used something called a cron job. Cron jobs have been around for ages and are a tried and true way of scheduling things to happen on our servers. Timing out sessions, sending out mail asynchronously, etc.

A cron job is run from something called a crontab file, and though it's not "rocket surgery", it's not the most intuitive thing to use in the world. Especially when you compare how much more intuitive (not perfect, mind you) many facets of ruby, and rails are.

Over time, cron jobs have kind of been tough to keep sorted out. Especially as we keep growing, adding/upgrading servers, etc. The problem of managing our cron jobs came to a head, as Javan was tasked with creating a version of Inkling that could reside behind some corporations' firewalls. When your talking about installable software, having a way to deploy changes to these cron jobs didn't really exist.

Javan knew there must be a better way, so he created Whenever.

Whenever allows us to write all our cron jobs out in ruby and even includes a capistrano task that deploys those changes to cron to our production servers.

Whenever has been taking off in popularity. Today it made the front page of Hacker News (news.ycombinator.com). And on github it has over 185 people "watching" it. Sounds like some other folks really needed something like this too.

Nice job Javan. Very cool contribution.

4 comments:

Khelal said...

Hi guys! This is really a great gem and very useful. One thing I'm wondering tho before making it part of our application is how does it manage users? i.e. we do all the work on our production system as root, but the application is actually run as www-data (default apache user). Will the crontab be created for www-data or for root?

Thx!

Javan said...

Khelal,

Whenever, by default, will create crontab entries for the user that issues the `whenever` command, but you can use the --user option to specify a different user.

brandon said...

Hey, this is beautiful--not only for the elegant syntax and extremely improved readability over crontabs, but also so that the cron jobs can be managed from within the Rails project even though they are run by the OS. Thanks!

I'd like to mention, though, that there's a better syntax that you can use in the crontab itself for running something (for example) every 5 minutes:
*/5 * * * * /path/to/somescript.sh
(The same syntax obviously works for every x hours, every x days, etc.)

Javan said...

Thanks brandon!

I'm aware of the */5 syntax, but I read that it was less compatible across unix systems so I went the more verbose route.