A Ruby DSL for outputting Cron intervals.
Using a natural syntax to write cron task intervals assists in generating the correct cron interval you are after instead of trying to remember the cron syntax and conventions.
cron = Cronos::Interval puts cron.new.hourly # => '0 * * * *' puts cron.new.daily # => '0 0 * * *' puts cron.new.weekly # => '0 0 * * 0' puts cron.new.weekdays.at(12.30) # => '30 12 * * 1-5' puts cron.new.weekends.at(12.30) # => '30 12 * * 0,6' puts cron.new.at('1.30pm').daily # => '30 13 * * *' puts cron.new.every(6).hours.on('15th').of(:january) # => '0 0,6,12,18 15 1 *' puts cron.new.every(20).minutes.on('15th'..'18th').of(:jan, :feb, :mar) # => '0,20,40 * 15-18 1,2,3 *' puts cron.new.at(14.45).every(:monday, :tuesday) # => '45 14 * * 1,2'
You can also output the whole cron task string using the schedule method:
puts Cronos.schedule('some_task').every(:Sunday).at('3am') # => '0 3 * * 0 some_task'
This does not actually schedule the task in cron. Cronos is merely a DSL to output the cron intervals.
Certain combinations produce unintuitive results. They should be obvious but just in case I will list some. Such as
weekly.on_days(:monday, :tuesday)
Will actually run the task twice weekly
TODO: more weird stuff?
Also cron itself allows days of month and days of week to be set. These are independent of each other.
For example:
on_the('15th').of(:jan).on_days(:mon, :tue)
This will be run on the 15th of January AND every Monday and Tuesday of January, so be aware. Though it is a strange combination anyway, the result is the union of both and not the intersection.
sudo gem install cronos
Adam Meehan (duckpunching.com, [email protected])
Copyright © 2008 Adam Meehan, released under the MIT license