Permalink
Browse files

Allow multiple times, arrays, and combine results whenever possible

Signed-off-by: Javan Makhmali <javan@javan.us>
  • Loading branch information...
1 parent abab2f8 commit 5ffc2b48bd5585432fd79c1e609af04802b4662e @rubys rubys committed with javan Jun 8, 2009
Showing with 81 additions and 9 deletions.
  1. +21 −1 lib/job_list.rb
  2. +13 −7 lib/outputs/cron.rb
  3. +47 −1 test/output_at_test.rb
View
@@ -88,6 +88,26 @@ def environment_variables
output.join
end
+ def combine entries
+ entries.map! {|entry| entry.split(/ +/,6)}
+ 0.upto(5) do |f|
+ (entries.length-1).downto(1) do |i|
+ next if entries[i][f] == '*'
+ comparison = entries[i][0...f] + entries[i][f+1..-1]
+ (i-1).downto(0) do |j|
+ next if entries[j][f] == '*'
+ if comparison == entries[j][0...f] + entries[j][f+1..-1]
+ entries[j][f] += ',' + entries[i][f]
+ entries.delete_at(i)
+ break
+ end
+ end
+ end
+ end
+
+ entries.map {|entry| entry.join(' ')}
+ end
+
def cron_jobs
return if @jobs.empty?
@@ -102,7 +122,7 @@ def cron_jobs
end
end
- output.join
+ combine(output).join
end
end
View
@@ -11,16 +11,22 @@ def initialize(time = nil, task = nil, at = nil)
@at = at.is_a?(String) ? (Chronic.parse(at) || 0) : (at || 0)
end
- def self.output(time, job)
- if job.at and job.at.is_a?(String) and !job.at.empty?
- ats = job.at.split(',')
+ def self.enumerate(item)
+ if item and item.is_a?(String)
+ items = item.split(',')
else
- ats = [job.at]
+ items = item
+ items = [items] unless items and items.respond_to?(:each)
end
+ items
+ end
- ats.each do |at|
- out = new(time, job.output, at)
- yield "#{out.time_in_cron_syntax} #{out.task}"
+ def self.output(times, job)
+ enumerate(times).each do |time|
+ enumerate(job.at).each do |at|
+ out = new(time, job.output, at)
+ yield "#{out.time_in_cron_syntax} #{out.task}"
+ end
end
end
View
@@ -17,7 +17,23 @@ class OutputAtTest < Test::Unit::TestCase
end
end
- context "weekday at a multiple diverse times" do
+ context "weekday at a multiple diverse times, via an array" do
+ setup do
+ @output = Whenever.cron \
+ <<-file
+ every "weekday", :at=>%w(5:02am 3:52pm) do
+ command "blahblah"
+ end
+ file
+ end
+
+ should "output the runner using that path" do
+ assert_match '2 5 * * mon-fri blahblah', @output
+ assert_match '52 15 * * mon-fri blahblah', @output
+ end
+ end
+
+ context "weekday at a multiple diverse times, comma separated" do
setup do
@output = Whenever.cron \
<<-file
@@ -33,4 +49,34 @@ class OutputAtTest < Test::Unit::TestCase
end
end
+ context "weekday at a multiple aligned times" do
+ setup do
+ @output = Whenever.cron \
+ <<-file
+ every "weekday", :at=>'5:02am, 3:02pm' do
+ command "blahblah"
+ end
+ file
+ end
+
+ should "output the runner using that path" do
+ assert_match '2 5,15 * * mon-fri blahblah', @output
+ end
+ end
+
+ context "various days at a various aligned times" do
+ setup do
+ @output = Whenever.cron \
+ <<-file
+ every "mon,wed,fri", :at=>'5:02am, 3:02pm' do
+ command "blahblah"
+ end
+ file
+ end
+
+ should "output the runner using that path" do
+ assert_match '2 5,15 * * mon,wed,fri blahblah', @output
+ end
+ end
+
end

0 comments on commit 5ffc2b4

Please sign in to comment.