mirror of
https://github.com/plashchynski/crono.git
synced 2026-03-19 16:21:36 +01:00
Fix: Next performing time should be related to last performing time
This commit is contained in:
@@ -2,17 +2,19 @@ module Crono
|
||||
class Job
|
||||
attr_accessor :performer
|
||||
attr_accessor :period
|
||||
attr_accessor :last_performed_at
|
||||
|
||||
def initialize(performer, period)
|
||||
self.performer, self.period = performer, period
|
||||
end
|
||||
|
||||
def next
|
||||
period.next
|
||||
period.next(since: last_performed_at)
|
||||
end
|
||||
|
||||
def perform
|
||||
Crono.logger.info "Perform #{performer}"
|
||||
self.last_performed_at = Time.now
|
||||
Thread.new { performer.new.perform }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,8 +5,9 @@ module Crono
|
||||
@at_hour, @at_min = parse_at(at) if at
|
||||
end
|
||||
|
||||
def next
|
||||
@period.from_now.change({hour: @at_hour, min: @at_min}.compact)
|
||||
def next(since: nil)
|
||||
since ||= Time.now
|
||||
@period.since(since).change({hour: @at_hour, min: @at_min}.compact)
|
||||
end
|
||||
|
||||
def parse_at(at)
|
||||
|
||||
@@ -30,6 +30,11 @@ describe Crono::Period do
|
||||
@period = Crono::Period.new(2.day, at: 1)
|
||||
}.to raise_error("Unknown 'at' format")
|
||||
end
|
||||
|
||||
it "should return time in relation to last time" do
|
||||
@period = Crono::Period.new(2.day)
|
||||
expect(@period.next(since: 1.day.ago)).to be_eql(1.day.from_now)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user