From 76132772ca38d72882f3d2678c356ac31e295ebc Mon Sep 17 00:00:00 2001 From: John Mettraux Date: Mon, 9 Nov 2020 07:33:46 +0900 Subject: [PATCH] Introduce :discard_past = false for cron, gh-305 --- lib/rufus/scheduler/jobs_repeat.rb | 5 ++- spec/job_cron_spec.rb | 57 ++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/lib/rufus/scheduler/jobs_repeat.rb b/lib/rufus/scheduler/jobs_repeat.rb index 94ce5087..bcd5c6aa 100644 --- a/lib/rufus/scheduler/jobs_repeat.rb +++ b/lib/rufus/scheduler/jobs_repeat.rb @@ -319,11 +319,14 @@ def set_next_time(trigger_time, is_post=false, now=nil) t = trigger_time || now || EoTime.now + previous = @previous_time || @scheduled_at + t = previous if ! discard_past? && t > previous + @next_time = if @first_at && @first_at > t @first_at else - next_time_from(t) + @cron_line.next_time(t) end end end diff --git a/spec/job_cron_spec.rb b/spec/job_cron_spec.rb index 1195d5a0..d501dfbb 100644 --- a/spec/job_cron_spec.rb +++ b/spec/job_cron_spec.rb @@ -212,5 +212,62 @@ expect(times.first).to be_between(Time.now + 2.5, Time.now + 3.5) end end + + describe '#resume' do + + context 'discard_past: true' do + + # Discard_past can be set at scheduler level, at job level, and + # at resume level. + # Test only the last case here. + # Every tests them all. + + it 'discards' do + + @scheduler.discard_past = false + + job = @scheduler + .schedule_cron('* * * * * *', discard_past: false) do; end + job.pause + + expect(job.count).to eq(0) + + wait_next_seconds(2) + + job.resume(discard_past: true) + + wait_until { job.count > 0 } + + expect(job.count).to eq(1) + end + end + + context 'discard_past: false' do + + # Discard_past can be set at scheduler level, at job level, and + # at resume level. + # Test only the last case here. + # Every tests them all. + + it 'triggers the past' do + + job = + @scheduler.schedule_cron('* * * * * *', discard_past: false) do; end + job.pause + + expect(job.count).to eq(0) + + wait_next_seconds(3) + + expect(job.count).to eq(0) + + job.resume(discard_past: false) + + wait_next_second + + expect(job.count).to be > 2 + end + end + end end