Allow multiple times, arrays, and combine results whenever possible

Signed-off-by: Javan Makhmali <javan@javan.us>
This commit is contained in:
Sam Ruby 2009-06-09 02:22:02 +08:00 committed by Javan Makhmali
parent abab2f8835
commit 5ffc2b48bd
3 changed files with 81 additions and 9 deletions

View File

@ -88,6 +88,26 @@ module Whenever
output.join output.join
end 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 def cron_jobs
return if @jobs.empty? return if @jobs.empty?
@ -102,7 +122,7 @@ module Whenever
end end
end end
output.join combine(output).join
end end
end end

View File

@ -11,18 +11,24 @@ module Whenever
@at = at.is_a?(String) ? (Chronic.parse(at) || 0) : (at || 0) @at = at.is_a?(String) ? (Chronic.parse(at) || 0) : (at || 0)
end end
def self.output(time, job) def self.enumerate(item)
if job.at and job.at.is_a?(String) and !job.at.empty? if item and item.is_a?(String)
ats = job.at.split(',') items = item.split(',')
else else
ats = [job.at] items = item
items = [items] unless items and items.respond_to?(:each)
end
items
end end
ats.each do |at| def self.output(times, job)
enumerate(times).each do |time|
enumerate(job.at).each do |at|
out = new(time, job.output, at) out = new(time, job.output, at)
yield "#{out.time_in_cron_syntax} #{out.task}" yield "#{out.time_in_cron_syntax} #{out.task}"
end end
end end
end
def time_in_cron_syntax def time_in_cron_syntax
case @time case @time

View File

@ -17,7 +17,23 @@ class OutputAtTest < Test::Unit::TestCase
end end
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 setup do
@output = Whenever.cron \ @output = Whenever.cron \
<<-file <<-file
@ -33,4 +49,34 @@ class OutputAtTest < Test::Unit::TestCase
end end
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 end