From 087119ce2d3abc38fbda558c02ef73d6da5df626 Mon Sep 17 00:00:00 2001 From: Gerrit Riessen Date: Fri, 25 Jun 2010 16:45:43 +0800 Subject: [PATCH] Allow the specification of an offset (using :at => [1-59]) when using every( X.minutes ) { ... } for job definition. For example: every 5.minutes, :at => 1 do command "blahblah" end produces 1,6,11,16,21,26,31,36,41,46,51,56 * * * * blahblah --- lib/whenever/outputs/cron.rb | 2 +- test/output_at_test.rb | 59 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/lib/whenever/outputs/cron.rb b/lib/whenever/outputs/cron.rb index 03110d0..044cd9a 100644 --- a/lib/whenever/outputs/cron.rb +++ b/lib/whenever/outputs/cron.rb @@ -77,7 +77,7 @@ module Whenever raise ArgumentError, "Time must be in minutes or higher" when 1.minute...1.hour minute_frequency = @time / 60 - timing[0] = comma_separated_timing(minute_frequency, 59) + timing[0] = comma_separated_timing(minute_frequency, 59, @at || 0) when 1.hour...1.day hour_frequency = (@time / 60 / 60).round timing[0] = @at.is_a?(Time) ? @at.min : @at diff --git a/test/output_at_test.rb b/test/output_at_test.rb index 4d84267..8131b28 100644 --- a/test/output_at_test.rb +++ b/test/output_at_test.rb @@ -175,4 +175,63 @@ class OutputAtTest < Test::Unit::TestCase end end + context "every 5 minutes but but starting at 1" do + setup do + @output = Whenever.cron \ + <<-file + every 5.minutes, :at => 1 do + command "blahblah" + end + file + end + + should "output the command using that time" do + assert_match '1,6,11,16,21,26,31,36,41,46,51,56 * * * * blahblah', @output + end + end + + context "every 4 minutes but starting at 2" do + setup do + @output = Whenever.cron \ + <<-file + every 4.minutes, :at => 2 do + command "blahblah" + end + file + end + + should "output the command using that time" do + assert_match '2,6,10,14,18,22,26,30,34,38,42,46,50,54,58 * * * * blahblah', @output + end + end + + context "every 3 minutes but starting at 7" do + setup do + @output = Whenever.cron \ + <<-file + every 3.minutes, :at => 7 do + command "blahblah" + end + file + end + + should "output the command using that time" do + assert_match '7,10,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58 * * * * blahblah', @output + end + end + + context "every 2 minutes but starting at 27" do + setup do + @output = Whenever.cron \ + <<-file + every 2.minutes, :at => 27 do + command "blahblah" + end + file + end + + should "output the command using that time" do + assert_match '27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59 * * * * blahblah', @output + end + end end