mirror of
https://github.com/plashchynski/crono.git
synced 2026-03-24 10:21:54 +01:00
Period#next should return today time if it is first run and not too late
This commit is contained in:
@@ -56,7 +56,7 @@ module Crono
|
||||
|
||||
logger.info "Jobs:"
|
||||
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
|
||||
|
||||
|
||||
@@ -6,8 +6,13 @@ module Crono
|
||||
end
|
||||
|
||||
def next(since: nil)
|
||||
since ||= Time.now
|
||||
@period.since(since).change({hour: @at_hour, min: @at_min}.compact)
|
||||
if since.nil?
|
||||
@next = Time.now.change(time_atts)
|
||||
return @next if @next.future?
|
||||
since = Time.now
|
||||
end
|
||||
|
||||
@period.since(since).change(time_atts)
|
||||
end
|
||||
|
||||
def description
|
||||
@@ -27,5 +32,10 @@ module Crono
|
||||
raise "Unknown 'at' format"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def time_atts
|
||||
{hour: @at_hour, min: @at_min}.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -22,12 +22,14 @@ describe Crono::Period do
|
||||
end
|
||||
|
||||
it "should set time to 'at' time as a string" do
|
||||
@period = Crono::Period.new(2.day, at: "15:20")
|
||||
expect(@period.next).to be_eql(2.day.from_now.change(hour: 15, min: 20))
|
||||
time = 10.minutes.ago
|
||||
@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
|
||||
|
||||
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)
|
||||
expect(@period.next).to be_eql(2.day.from_now.change(at))
|
||||
end
|
||||
@@ -42,6 +44,13 @@ describe Crono::Period do
|
||||
@period = Crono::Period.new(2.day)
|
||||
expect(@period.next(since: 1.day.ago)).to be_eql(1.day.from_now)
|
||||
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
|
||||
|
||||
@@ -25,11 +25,7 @@ describe Crono::Scheduler do
|
||||
|
||||
describe "#next" do
|
||||
it "should return next job in schedule" do
|
||||
expect(@scheduler.next).to be @jobs[1]
|
||||
end
|
||||
|
||||
it "should return next based on last" do
|
||||
expect(@scheduler.next)
|
||||
expect(@scheduler.next).to be @jobs[2]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user