adzap / cronos

Tool for generating cron task intervals using a natural syntax

This URL has Read+Write access

adzap (author)
Mon Dec 14 15:10:51 -0800 2009
commit  047931d322c435831331be6daad60454c277a3d1
tree    3e902cd42921ae49d8cbffaee51d875b568a04af
parent  ad5545f7e6305c1c2f78fc15c6b008f02f43bb2b
cronos /
name age message
file .gitignore Tue Dec 23 03:00:59 -0800 2008 added ignore file [adzap]
file CHANGELOG Thu Mar 05 18:01:12 -0800 2009 release 0.4.0 [adzap]
file LICENSE Mon Dec 22 03:08:27 -0800 2008 initial commit [adzap]
file README.rdoc Mon Dec 14 15:10:51 -0800 2009 update gem install doc [adzap]
file Rakefile Tue Feb 03 03:09:10 -0800 2009 release 0.3.1 [adzap]
file TODO Sun Mar 15 18:33:11 -0700 2009 added todo ideas [adzap]
file cronos.gemspec Thu Mar 05 18:01:12 -0800 2009 release 0.4.0 [adzap]
directory lib/ Thu Mar 05 18:01:12 -0800 2009 release 0.4.0 [adzap]
directory spec/ Thu Mar 05 17:57:42 -0800 2009 added TaskInterval class to output task in cron... [adzap]
README.rdoc

Cronos

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.

Examples

  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.

Caveats

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.

Install

  sudo gem install cronos

Credits

Adam Meehan (duckpunching.com, adam.meehan@gmail.com)

License

Copyright © 2008 Adam Meehan, released under the MIT license