Commit 27f981b2 authored by Shinya Maeda's avatar Shinya Maeda

Improve real_next_run. Improve triggers_helper_spec.

parent f6be8c04
...@@ -11,16 +11,10 @@ module TriggersHelper ...@@ -11,16 +11,10 @@ module TriggersHelper
"#{Settings.gitlab.url}/api/v3/projects/#{service.project_id}/services/#{service.to_param}/trigger" "#{Settings.gitlab.url}/api/v3/projects/#{service.project_id}/services/#{service.to_param}/trigger"
end end
def real_next_run(trigger_schedule, worker_cron: nil, worker_time_zone: nil) def real_next_run(trigger_schedule,
worker_cron = Settings.cron_jobs['trigger_schedule_worker']['cron'] unless worker_cron.present? worker_cron: Settings.cron_jobs['trigger_schedule_worker']['cron'],
worker_time_zone = Time.zone.name unless worker_time_zone.present? worker_time_zone: Time.zone.name)
Ci::CronParser.new(worker_cron, worker_time_zone)
worker_next_time = Ci::CronParser.new(worker_cron, worker_time_zone).next_time_from(Time.now) .next_time_from(trigger_schedule.next_run_at)
if trigger_schedule.next_run_at > worker_next_time
trigger_schedule.next_run_at
else
worker_next_time
end
end end
end end
...@@ -4,23 +4,36 @@ describe TriggersHelper do ...@@ -4,23 +4,36 @@ describe TriggersHelper do
describe '#real_next_run' do describe '#real_next_run' do
let(:trigger_schedule) { create(:ci_trigger_schedule, cron: user_cron, cron_time_zone: 'UTC') } 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') } subject { helper.real_next_run(trigger_schedule, arguments) }
context 'when next_run_at > worker_next_time' do context 'when next_run_at > worker_next_time' do
let(:worker_cron) { '0 0 1 1 *' } # every 00:00, January 1st let(:arguments) { { worker_cron: '0 0 1 1 *', worker_time_zone: 'UTC' } } # every 00:00, January 1st
let(:user_cron) { '1 0 1 1 *' } # every 00:01, January 1st let(:user_cron) { '1 0 1 1 *' } # every 00:01, January 1st
it 'returns next_run_at' do it 'returns nearest worker_next_time from next_run_at' do
is_expected.to eq(trigger_schedule.next_run_at) is_expected.to eq(Ci::CronParser.new(arguments[:worker_cron], arguments[:worker_time_zone])
.next_time_from(trigger_schedule.next_run_at))
end end
end end
context 'when worker_next_time > next_run_at' do context 'when worker_next_time > next_run_at' do
let(:worker_cron) { '1 0 1 1 *' } # every 00:01, January 1st let(:arguments) { { worker_cron: '1 0 1 1 *', worker_time_zone: 'UTC' } } # every 00:01, January 1st
let(:user_cron) { '0 0 1 1 *' } # every 00:00, January 1st let(:user_cron) { '0 0 1 1 *' } # every 00:00, January 1st
it 'returns worker_next_time' do it 'returns nearest worker_next_time from next_run_at' do
is_expected.to eq(Ci::CronParser.new(worker_cron, 'UTC').next_time_from(Time.now)) is_expected.to eq(Ci::CronParser.new(arguments[:worker_cron], arguments[:worker_time_zone])
.next_time_from(trigger_schedule.next_run_at))
end
end
context 'when worker_cron and worker_time_zone are ommited' do
let(:arguments) { {} }
let(:user_cron) { '* * * * *' } # every minutes
it 'returns nearest worker_next_time from next_run_at by server configuration' do
is_expected.to eq(Ci::CronParser.new(Settings.cron_jobs['trigger_schedule_worker']['cron'],
Time.zone.name)
.next_time_from(trigger_schedule.next_run_at))
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment