Commit 914bef67 authored by Shinya Maeda's avatar Shinya Maeda

Move Ci::CronParser to Gitlab::Ci::CronParser

parent 27f981b2
......@@ -14,7 +14,7 @@ module TriggersHelper
def real_next_run(trigger_schedule,
worker_cron: Settings.cron_jobs['trigger_schedule_worker']['cron'],
worker_time_zone: Time.zone.name)
Ci::CronParser.new(worker_cron, worker_time_zone)
.next_time_from(trigger_schedule.next_run_at)
Gitlab::Ci::CronParser.new(worker_cron, worker_time_zone)
.next_time_from(trigger_schedule.next_run_at)
end
end
......@@ -18,7 +18,7 @@ module Ci
after_create :schedule_next_run!
def schedule_next_run!
next_time = Ci::CronParser.new(cron, cron_time_zone).next_time_from(Time.now)
next_time = Gitlab::Ci::CronParser.new(cron, cron_time_zone).next_time_from(Time.now)
update!(next_run_at: next_time) if next_time.present?
end
end
......
......@@ -3,7 +3,7 @@
# Custom validator for Cron.
class CronValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
cron_parser = Ci::CronParser.new(record.cron, record.cron_time_zone)
cron_parser = Gitlab::Ci::CronParser.new(record.cron, record.cron_time_zone)
is_valid_cron, is_valid_cron_time_zone = cron_parser.validation
if !is_valid_cron
......
module Ci
class CronParser
VALID_SYNTAX_SAMPLE_TIME_ZONE = 'UTC'.freeze
VALID_SYNTAX_SAMPLE_CRON = '* * * * *'.freeze
def initialize(cron, cron_time_zone = 'UTC')
@cron = cron
@cron_time_zone = cron_time_zone
end
def next_time_from(time)
cron_line = try_parse_cron(@cron, @cron_time_zone)
if cron_line.present?
cron_line.next_time(time).in_time_zone(Time.zone)
else
nil
end
end
def validation
is_valid_cron = try_parse_cron(@cron, VALID_SYNTAX_SAMPLE_TIME_ZONE).present?
is_valid_cron_time_zone = try_parse_cron(VALID_SYNTAX_SAMPLE_CRON, @cron_time_zone).present?
return is_valid_cron, is_valid_cron_time_zone
end
private
def try_parse_cron(cron, cron_time_zone)
begin
Rufus::Scheduler.parse("#{cron} #{cron_time_zone}")
rescue
nil
end
end
end
end
module Gitlab
module Ci
class CronParser
VALID_SYNTAX_SAMPLE_TIME_ZONE = 'UTC'.freeze
VALID_SYNTAX_SAMPLE_CRON = '* * * * *'.freeze
def initialize(cron, cron_time_zone = 'UTC')
@cron = cron
@cron_time_zone = cron_time_zone
end
def next_time_from(time)
cron_line = try_parse_cron(@cron, @cron_time_zone)
if cron_line.present?
cron_line.next_time(time).in_time_zone(Time.zone)
else
nil
end
end
def validation
is_valid_cron = try_parse_cron(@cron, VALID_SYNTAX_SAMPLE_TIME_ZONE).present?
is_valid_cron_time_zone = try_parse_cron(VALID_SYNTAX_SAMPLE_CRON, @cron_time_zone).present?
return is_valid_cron, is_valid_cron_time_zone
end
private
def try_parse_cron(cron, cron_time_zone)
begin
Rufus::Scheduler.parse("#{cron} #{cron_time_zone}")
rescue
nil
end
end
end
end
end
\ No newline at end of file
......@@ -2,7 +2,7 @@ FactoryGirl.define do
factory :ci_trigger_schedule, class: Ci::TriggerSchedule do
trigger factory: :ci_trigger_for_trigger_schedule
cron '0 1 * * *'
cron_time_zone Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
cron_time_zone Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
after(:build) do |trigger_schedule, evaluator|
trigger_schedule.update!(project: trigger_schedule.trigger.project)
......@@ -16,17 +16,17 @@ FactoryGirl.define do
trait :cron_nightly_build do
cron '0 1 * * *'
cron_time_zone Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
cron_time_zone Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
end
trait :cron_weekly_build do
cron '0 1 * * 6'
cron_time_zone Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
cron_time_zone Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
end
trait :cron_monthly_build do
cron '0 1 22 * *'
cron_time_zone Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
cron_time_zone Gitlab::Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
end
end
end
......@@ -11,8 +11,8 @@ describe TriggersHelper do
let(:user_cron) { '1 0 1 1 *' } # every 00:01, January 1st
it 'returns nearest worker_next_time from next_run_at' do
is_expected.to eq(Ci::CronParser.new(arguments[:worker_cron], arguments[:worker_time_zone])
.next_time_from(trigger_schedule.next_run_at))
is_expected.to eq(Gitlab::Ci::CronParser.new(arguments[:worker_cron], arguments[:worker_time_zone])
.next_time_from(trigger_schedule.next_run_at))
end
end
......@@ -21,8 +21,8 @@ describe TriggersHelper do
let(:user_cron) { '0 0 1 1 *' } # every 00:00, January 1st
it 'returns nearest worker_next_time from next_run_at' do
is_expected.to eq(Ci::CronParser.new(arguments[:worker_cron], arguments[:worker_time_zone])
.next_time_from(trigger_schedule.next_run_at))
is_expected.to eq(Gitlab::Ci::CronParser.new(arguments[:worker_cron], arguments[:worker_time_zone])
.next_time_from(trigger_schedule.next_run_at))
end
end
......@@ -31,9 +31,9 @@ describe TriggersHelper do
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))
is_expected.to eq(Gitlab::Ci::CronParser.new(Settings.cron_jobs['trigger_schedule_worker']['cron'],
Time.zone.name)
.next_time_from(trigger_schedule.next_run_at))
end
end
end
......
......@@ -13,7 +13,7 @@ describe Ci::TriggerSchedule, models: true do
end
it 'updates next_run_at' do
next_time = Ci::CronParser.new(trigger_schedule.cron, trigger_schedule.cron_time_zone).next_time_from(Time.now)
next_time = Gitlab::Ci::CronParser.new(trigger_schedule.cron, trigger_schedule.cron_time_zone).next_time_from(Time.now)
expect(Ci::TriggerSchedule.last.next_run_at).to eq(next_time)
end
end
......
......@@ -23,7 +23,7 @@ describe TriggerScheduleWorker do
end
it 'updates next_run_at' do
next_time = Ci::CronParser.new(trigger_schedule.cron, trigger_schedule.cron_time_zone).next_time_from(Time.now)
next_time = Gitlab::Ci::CronParser.new(trigger_schedule.cron, trigger_schedule.cron_time_zone).next_time_from(Time.now)
expect(Ci::TriggerSchedule.last.next_run_at).to eq(next_time)
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