diff --git a/app/helpers/triggers_helper.rb b/app/helpers/triggers_helper.rb index a48d4475e97..be5cce9aea0 100644 --- a/app/helpers/triggers_helper.rb +++ b/app/helpers/triggers_helper.rb @@ -10,4 +10,17 @@ module TriggersHelper def service_trigger_url(service) "#{Settings.gitlab.url}/api/v3/projects/#{service.project_id}/services/#{service.to_param}/trigger" end + + def real_next_run(trigger_schedule, worker_cron: nil, worker_time_zone: nil) + worker_cron = Settings.cron_jobs['trigger_schedule_worker']['cron'] unless worker_cron.present? + worker_time_zone = Time.zone.name unless worker_time_zone.present? + + worker_next_time = Ci::CronParser.new(worker_cron, worker_time_zone).next_time_from(Time.now) + + if trigger_schedule.next_run_at > worker_next_time + trigger_schedule.next_run_at + else + worker_next_time + end + end end diff --git a/app/models/ci/trigger_schedule.rb b/app/models/ci/trigger_schedule.rb index b861f41fed1..aedba8bdf54 100644 --- a/app/models/ci/trigger_schedule.rb +++ b/app/models/ci/trigger_schedule.rb @@ -25,19 +25,6 @@ module Ci end end - def real_next_run(worker_cron: nil, worker_time_zone: nil) - worker_cron = Settings.cron_jobs['trigger_schedule_worker']['cron'] unless worker_cron.present? - worker_time_zone = Time.zone.name unless worker_time_zone.present? - - worker_next_time = Ci::CronParser.new(worker_cron, worker_time_zone).next_time_from(Time.now) - - if next_run_at > worker_next_time - next_run_at - else - worker_next_time - end - end - private def less_than_1_hour_from_now?(time) diff --git a/spec/helpers/triggers_helper_spec.rb b/spec/helpers/triggers_helper_spec.rb new file mode 100644 index 00000000000..ce17f4442ab --- /dev/null +++ b/spec/helpers/triggers_helper_spec.rb @@ -0,0 +1,27 @@ +require 'rails_helper' + +describe TriggersHelper do + describe '#real_next_run' do + let(:trigger_schedule) { create(:ci_trigger_schedule, cron: user_cron, cron_time_zone: 'UTC') } + + subject { helper.real_next_run(trigger_schedule, worker_cron: worker_cron, worker_time_zone: 'UTC') } + + context 'when next_run_at > worker_next_time' do + let(:worker_cron) { '* * * * *' } # every minutes + let(:user_cron) { '0 0 1 1 *' } # every 00:00, January 1st + + it 'returns next_run_at' do + is_expected.to eq(trigger_schedule.next_run_at) + end + end + + context 'when worker_next_time > next_run_at' do + let(:worker_cron) { '0 0 1 1 *' } # every 00:00, January 1st + let(:user_cron) { '0 */6 * * *' } # each six hours + + it 'returns worker_next_time' do + is_expected.to eq(Ci::CronParser.new(worker_cron, 'UTC').next_time_from(Time.now)) + end + end + end +end diff --git a/spec/models/ci/trigger_schedule_spec.rb b/spec/models/ci/trigger_schedule_spec.rb index da23611f1a0..6a586a4e9b1 100644 --- a/spec/models/ci/trigger_schedule_spec.rb +++ b/spec/models/ci/trigger_schedule_spec.rb @@ -54,28 +54,4 @@ describe Ci::TriggerSchedule, models: true do end end end - - describe '#real_next_run' do - let(:trigger_schedule) { create(:ci_trigger_schedule, cron: user_cron, cron_time_zone: 'UTC') } - - subject { trigger_schedule.real_next_run(worker_cron: worker_cron, worker_time_zone: 'UTC') } - - context 'when next_run_at > worker_next_time' do - let(:worker_cron) { '* * * * *' } # every minutes - let(:user_cron) { '0 0 1 1 *' } # every 00:00, January 1st - - it 'returns next_run_at' do - is_expected.to eq(trigger_schedule.next_run_at) - end - end - - context 'when worker_next_time > next_run_at' do - let(:worker_cron) { '0 0 1 1 *' } # every 00:00, January 1st - let(:user_cron) { '0 */6 * * *' } # each six hours - - it 'returns worker_next_time' do - is_expected.to eq(Ci::CronParser.new(worker_cron, 'UTC').next_time_from(Time.now)) - end - end - end end