Period#next should return today time if it is first run and not too late

This commit is contained in:
Dzmitry Plashchynski
2015-03-07 04:13:59 +02:00
parent 368cdde296
commit 6d90cb3233
4 changed files with 26 additions and 11 deletions

View File

@@ -56,7 +56,7 @@ module Crono
logger.info "Jobs:" logger.info "Jobs:"
Crono.scheduler.jobs.each do |job| Crono.scheduler.jobs.each do |job|
logger.info job.description logger.info %{"#{job.performer}" with rule "#{job.period.description}" next time will perform at #{job.next}}
end end
end end

View File

@@ -6,8 +6,13 @@ module Crono
end end
def next(since: nil) def next(since: nil)
since ||= Time.now if since.nil?
@period.since(since).change({hour: @at_hour, min: @at_min}.compact) @next = Time.now.change(time_atts)
return @next if @next.future?
since = Time.now
end
@period.since(since).change(time_atts)
end end
def description def description
@@ -27,5 +32,10 @@ module Crono
raise "Unknown 'at' format" raise "Unknown 'at' format"
end end
end end
private
def time_atts
{hour: @at_hour, min: @at_min}.compact
end
end end
end end

View File

@@ -22,12 +22,14 @@ describe Crono::Period do
end end
it "should set time to 'at' time as a string" do it "should set time to 'at' time as a string" do
@period = Crono::Period.new(2.day, at: "15:20") time = 10.minutes.ago
expect(@period.next).to be_eql(2.day.from_now.change(hour: 15, min: 20)) @period = Crono::Period.new(2.day, at: [time.hour, time.min].join(':'))
expect(@period.next).to be_eql(2.day.from_now.change(hour: time.hour, min: time.min))
end end
it "should set time to 'at' time as a hash" do it "should set time to 'at' time as a hash" do
at = {hour: 18, min: 45} time = 10.minutes.ago
at = {hour: time.hour, min: time.min}
@period = Crono::Period.new(2.day, at: at) @period = Crono::Period.new(2.day, at: at)
expect(@period.next).to be_eql(2.day.from_now.change(at)) expect(@period.next).to be_eql(2.day.from_now.change(at))
end end
@@ -42,6 +44,13 @@ describe Crono::Period do
@period = Crono::Period.new(2.day) @period = Crono::Period.new(2.day)
expect(@period.next(since: 1.day.ago)).to be_eql(1.day.from_now) expect(@period.next(since: 1.day.ago)).to be_eql(1.day.from_now)
end end
it "should return today time if it is first run and not too late" do
time = 10.minutes.from_now
at = {hour: time.hour, min: time.min}
@period = Crono::Period.new(2.day, at: at)
expect(@period.next).to be_eql(Time.now.change(at))
end
end end
end end
end end

View File

@@ -25,11 +25,7 @@ describe Crono::Scheduler do
describe "#next" do describe "#next" do
it "should return next job in schedule" do it "should return next job in schedule" do
expect(@scheduler.next).to be @jobs[1] expect(@scheduler.next).to be @jobs[2]
end
it "should return next based on last" do
expect(@scheduler.next)
end end
end end
end end