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 ...@@ -14,7 +14,7 @@ module TriggersHelper
def real_next_run(trigger_schedule, def real_next_run(trigger_schedule,
worker_cron: Settings.cron_jobs['trigger_schedule_worker']['cron'], worker_cron: Settings.cron_jobs['trigger_schedule_worker']['cron'],
worker_time_zone: Time.zone.name) worker_time_zone: Time.zone.name)
Ci::CronParser.new(worker_cron, worker_time_zone) Gitlab::Ci::CronParser.new(worker_cron, worker_time_zone)
.next_time_from(trigger_schedule.next_run_at) .next_time_from(trigger_schedule.next_run_at)
end end
end end
...@@ -18,7 +18,7 @@ module Ci ...@@ -18,7 +18,7 @@ module Ci
after_create :schedule_next_run! after_create :schedule_next_run!
def 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? update!(next_run_at: next_time) if next_time.present?
end end
end end
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Custom validator for Cron. # Custom validator for Cron.
class CronValidator < ActiveModel::EachValidator class CronValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value) 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 is_valid_cron, is_valid_cron_time_zone = cron_parser.validation
if !is_valid_cron 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 ...@@ -2,7 +2,7 @@ FactoryGirl.define do
factory :ci_trigger_schedule, class: Ci::TriggerSchedule do factory :ci_trigger_schedule, class: Ci::TriggerSchedule do
trigger factory: :ci_trigger_for_trigger_schedule trigger factory: :ci_trigger_for_trigger_schedule
cron '0 1 * * *' 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| after(:build) do |trigger_schedule, evaluator|
trigger_schedule.update!(project: trigger_schedule.trigger.project) trigger_schedule.update!(project: trigger_schedule.trigger.project)
...@@ -16,17 +16,17 @@ FactoryGirl.define do ...@@ -16,17 +16,17 @@ FactoryGirl.define do
trait :cron_nightly_build do trait :cron_nightly_build do
cron '0 1 * * *' 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 end
trait :cron_weekly_build do trait :cron_weekly_build do
cron '0 1 * * 6' 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 end
trait :cron_monthly_build do trait :cron_monthly_build do
cron '0 1 22 * *' 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 end
end end
...@@ -11,8 +11,8 @@ describe TriggersHelper do ...@@ -11,8 +11,8 @@ describe TriggersHelper do
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 nearest worker_next_time from next_run_at' do 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]) is_expected.to eq(Gitlab::Ci::CronParser.new(arguments[:worker_cron], arguments[:worker_time_zone])
.next_time_from(trigger_schedule.next_run_at)) .next_time_from(trigger_schedule.next_run_at))
end end
end end
...@@ -21,8 +21,8 @@ describe TriggersHelper do ...@@ -21,8 +21,8 @@ describe TriggersHelper do
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 nearest worker_next_time from next_run_at' do 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]) is_expected.to eq(Gitlab::Ci::CronParser.new(arguments[:worker_cron], arguments[:worker_time_zone])
.next_time_from(trigger_schedule.next_run_at)) .next_time_from(trigger_schedule.next_run_at))
end end
end end
...@@ -31,9 +31,9 @@ describe TriggersHelper do ...@@ -31,9 +31,9 @@ describe TriggersHelper do
let(:user_cron) { '* * * * *' } # every minutes let(:user_cron) { '* * * * *' } # every minutes
it 'returns nearest worker_next_time from next_run_at by server configuration' do 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'], is_expected.to eq(Gitlab::Ci::CronParser.new(Settings.cron_jobs['trigger_schedule_worker']['cron'],
Time.zone.name) Time.zone.name)
.next_time_from(trigger_schedule.next_run_at)) .next_time_from(trigger_schedule.next_run_at))
end end
end end
end end
......
...@@ -13,7 +13,7 @@ describe Ci::TriggerSchedule, models: true do ...@@ -13,7 +13,7 @@ describe Ci::TriggerSchedule, models: true do
end end
it 'updates next_run_at' do 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) expect(Ci::TriggerSchedule.last.next_run_at).to eq(next_time)
end end
end end
......
...@@ -23,7 +23,7 @@ describe TriggerScheduleWorker do ...@@ -23,7 +23,7 @@ describe TriggerScheduleWorker do
end end
it 'updates next_run_at' do 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) expect(Ci::TriggerSchedule.last.next_run_at).to eq(next_time)
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