Skip to content

Commit

Permalink
Merge pull request #342 from austinarbor/allow-first-at-in-past
Browse files Browse the repository at this point in the history
Optionally allow first_at to be set in the past
  • Loading branch information
jmettraux committed Jun 9, 2023
2 parents 1683dd7 + e210933 commit 213bcf2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/rufus/scheduler/jobs_repeat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def initialize(scheduler, duration, opts, block)

@times = opts[:times]

@first_at_no_error = opts[:first_at_no_error] || false

fail ArgumentError.new(
"cannot accept :times => #{@times.inspect}, not nil or an int"
) unless @times == nil || @times.is_a?(Integer)
Expand Down Expand Up @@ -43,6 +45,7 @@ def first_at=(first)

@first_at = (fdur && (EoTime.now + fdur)) || EoTime.make(first)
@first_at = n1 if @first_at >= n0 && @first_at < n1
@first_at = n0 if @first_at < n0 && @first_at_no_error

fail ArgumentError.new(
"cannot set first[_at|_in] in the past: " +
Expand Down Expand Up @@ -319,4 +322,3 @@ def set_next_time(trigger_time, is_post=false, now=nil)
end
end
end

10 changes: 9 additions & 1 deletion spec/job_repeat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@
}.to raise_error(ArgumentError)
end

it 'does not raise on points in the past when first_at_no_error is set' do
n = Time.now
job =
@scheduler.schedule_every '7s',
{ :first => Time.now - 60, :first_at_no_error => true } do; end

expect(job.first_at).to be < n + 0.7
end

context ':first_time => :now/:immediately/0' do

it 'schedules the first execution immediately (:first => :now)' do
Expand Down Expand Up @@ -439,4 +448,3 @@
end
end
end

0 comments on commit 213bcf2

Please sign in to comment.