diff --git a/lib/crono/job.rb b/lib/crono/job.rb index ea25c47..2e2b754 100644 --- a/lib/crono/job.rb +++ b/lib/crono/job.rb @@ -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 diff --git a/lib/crono/period.rb b/lib/crono/period.rb index f1296df..5c9e172 100644 --- a/lib/crono/period.rb +++ b/lib/crono/period.rb @@ -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) diff --git a/spec/period_spec.rb b/spec/period_spec.rb index de53e46..6057d88 100644 --- a/spec/period_spec.rb +++ b/spec/period_spec.rb @@ -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