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