From 1654f677744d70da8bcd320dfd05401cc5959691 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Mon, 23 May 2011 21:08:43 -0400 Subject: [PATCH] convert most shortcuts to seconds --- CHANGELOG.md | 5 +++++ lib/whenever/cron.rb | 25 +++++++++++++++++-------- test/functional/output_at_test.rb | 2 +- test/unit/cron_test.rb | 18 ++++++++++++------ 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c878d4d..0fa7d69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### 0.6.8 / May 23rd, 2011 + +* Convert most shortcuts to seconds. every :day -> every 1.day. #129 [Javan Makhmali] + + ### 0.6.7 / March 23rd, 2011 * Fix issue with comment block being corrupted during subsequent insertion of duplicate entries to the crontab. #123 [Jeremy (@lingmann)] diff --git a/lib/whenever/cron.rb b/lib/whenever/cron.rb index ea14b0c..a68b40b 100644 --- a/lib/whenever/cron.rb +++ b/lib/whenever/cron.rb @@ -45,16 +45,25 @@ module Whenever def parse_symbol shortcut = case @time - when :reboot then '@reboot' - when :year, :yearly then '@annually' - when :day, :daily then '@daily' - when :midnight then '@midnight' - when :month, :monthly then '@monthly' - when :week, :weekly then '@weekly' - when :hour, :hourly then '@hourly' + when :reboot then '@reboot' + when :year then 12.months + when :yearly, + :annually then '@annually' + when :day then 1.day + when :daily then '@daily' + when :midnight then '@midnight' + when :month then 1.month + when :monthly then '@monthly' + when :week then 1.week + when :weekly then '@weekly' + when :hour then 1.hour + when :hourly then '@hourly' end - if shortcut + if shortcut.is_a?(Numeric) + @time = shortcut + parse_time + elsif shortcut if @at.is_a?(Time) || (@at.is_a?(Numeric) && @at > 0) raise ArgumentError, "You cannot specify an ':at' when using the shortcuts for times." else diff --git a/test/functional/output_at_test.rb b/test/functional/output_at_test.rb index 96fe6d4..2ee620d 100644 --- a/test/functional/output_at_test.rb +++ b/test/functional/output_at_test.rb @@ -166,7 +166,7 @@ class OutputAtTest < Test::Unit::TestCase <<-file set :job_template, nil set :path, '/your/path' - every :day do + every :daily do rake "blah:blah" runner "runner_1" command "command_1" diff --git a/test/unit/cron_test.rb b/test/unit/cron_test.rb index 5427a31..ca3378f 100644 --- a/test/unit/cron_test.rb +++ b/test/unit/cron_test.rb @@ -174,20 +174,26 @@ class CronTest < Test::Unit::TestCase context "When parsing time using the cron shortcuts" do should "parse a :symbol into the correct shortcut" do assert_equal '@reboot', parse_time(:reboot) - assert_equal '@annually', parse_time(:year) + assert_equal '@annually', parse_time(:annually) assert_equal '@annually', parse_time(:yearly) - assert_equal '@daily', parse_time(:day) assert_equal '@daily', parse_time(:daily) assert_equal '@midnight', parse_time(:midnight) - assert_equal '@monthly', parse_time(:month) assert_equal '@monthly', parse_time(:monthly) - assert_equal '@hourly', parse_time(:hour) + assert_equal '@weekly', parse_time(:weekly) assert_equal '@hourly', parse_time(:hourly) end + should "convert time-based shortcuts to times" do + assert_equal '0 0 1 * *', parse_time(:month) + assert_equal '0 0 * * *', parse_time(:day) + assert_equal '0 * * * *', parse_time(:hour) + assert_equal '0 0 1 12 *', parse_time(:year) + assert_equal '0 0 1,8,15,22 * *', parse_time(:week) + end + should "raise an exception if a valid shortcut is given but also an :at" do assert_raises ArgumentError do - parse_time(:hour, nil, "1:00 am") + parse_time(:hourly, nil, "1:00 am") end assert_raises ArgumentError do @@ -195,7 +201,7 @@ class CronTest < Test::Unit::TestCase end assert_raises ArgumentError do - parse_time(:day, nil, '4:20pm') + parse_time(:daily, nil, '4:20pm') end end end