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
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 @@ module Whenever
end
end
output.join
combine(output).join
end
end

View File

@ -11,16 +11,22 @@ module Whenever
@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 File

@ -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