diff options
author | Shinya Maeda <gitlab.shinyamaeda@gmail.com> | 2017-04-04 18:44:25 +0900 |
---|---|---|
committer | Shinya Maeda <gitlab.shinyamaeda@gmail.com> | 2017-04-06 23:46:58 +0900 |
commit | 914bef671f54c04a0d36d8e0f8c9830d6dea7b03 (patch) | |
tree | 46067eb41371f1b5a1cbc9a27444ecbc40c45043 | |
parent | 27f981b2901098f894e587bbd96b09e2a0f0c404 (diff) | |
download | gitlab-ce-914bef671f54c04a0d36d8e0f8c9830d6dea7b03.tar.gz |
Move Ci::CronParser to Gitlab::Ci::CronParser
-rw-r--r-- | app/helpers/triggers_helper.rb | 4 | ||||
-rw-r--r-- | app/models/ci/trigger_schedule.rb | 2 | ||||
-rw-r--r-- | app/validators/cron_validator.rb | 2 | ||||
-rw-r--r-- | lib/ci/cron_parser.rb | 36 | ||||
-rw-r--r-- | lib/gitlab/ci/cron_parser.rb | 38 | ||||
-rw-r--r-- | spec/factories/ci/trigger_schedules.rb | 8 | ||||
-rw-r--r-- | spec/helpers/triggers_helper_spec.rb | 14 | ||||
-rw-r--r-- | spec/models/ci/trigger_schedule_spec.rb | 2 | ||||
-rw-r--r-- | spec/workers/trigger_schedule_worker_spec.rb | 2 |
9 files changed, 55 insertions, 53 deletions
diff --git a/app/helpers/triggers_helper.rb b/app/helpers/triggers_helper.rb index 932ba595b73..a415ac11893 100644 --- a/app/helpers/triggers_helper.rb +++ b/app/helpers/triggers_helper.rb @@ -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 diff --git a/app/models/ci/trigger_schedule.rb b/app/models/ci/trigger_schedule.rb index 6e7c0b4f6c2..092338be9ce 100644 --- a/app/models/ci/trigger_schedule.rb +++ b/app/models/ci/trigger_schedule.rb @@ -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 diff --git a/app/validators/cron_validator.rb b/app/validators/cron_validator.rb index 4d9a0d62a4c..31eaa4147a5 100644 --- a/app/validators/cron_validator.rb +++ b/app/validators/cron_validator.rb @@ -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 diff --git a/lib/ci/cron_parser.rb b/lib/ci/cron_parser.rb deleted file mode 100644 index e0d589956a8..00000000000 --- a/lib/ci/cron_parser.rb +++ /dev/null @@ -1,36 +0,0 @@ -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 diff --git a/lib/gitlab/ci/cron_parser.rb b/lib/gitlab/ci/cron_parser.rb new file mode 100644 index 00000000000..01f37142510 --- /dev/null +++ b/lib/gitlab/ci/cron_parser.rb @@ -0,0 +1,38 @@ +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 diff --git a/spec/factories/ci/trigger_schedules.rb b/spec/factories/ci/trigger_schedules.rb index 2e6a35c6416..8aafbc1f81b 100644 --- a/spec/factories/ci/trigger_schedules.rb +++ b/spec/factories/ci/trigger_schedules.rb @@ -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 diff --git a/spec/helpers/triggers_helper_spec.rb b/spec/helpers/triggers_helper_spec.rb index 61d233421b2..ee3fd3fea0f 100644 --- a/spec/helpers/triggers_helper_spec.rb +++ b/spec/helpers/triggers_helper_spec.rb @@ -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 diff --git a/spec/models/ci/trigger_schedule_spec.rb b/spec/models/ci/trigger_schedule_spec.rb index 99668ff17b8..81104cb26b6 100644 --- a/spec/models/ci/trigger_schedule_spec.rb +++ b/spec/models/ci/trigger_schedule_spec.rb @@ -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 diff --git a/spec/workers/trigger_schedule_worker_spec.rb b/spec/workers/trigger_schedule_worker_spec.rb index 950f72a68d9..4df5731709b 100644 --- a/spec/workers/trigger_schedule_worker_spec.rb +++ b/spec/workers/trigger_schedule_worker_spec.rb @@ -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 |