Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinya Maeda <gitlab.shinyamaeda@gmail.com>2017-03-30 21:16:24 +0300
committerShinya Maeda <gitlab.shinyamaeda@gmail.com>2017-04-06 17:46:58 +0300
commit9573bb44bc94261814dbdbb384b9ad7acf2907ff (patch)
tree7f21ed207330cf4412be6a35f77ee0bf4c61f627
parentd48658e340c6d8d8b5e028afa6d5962ec7616e24 (diff)
real_next_run (WIP)
-rw-r--r--app/models/ci/trigger_schedule.rb19
-rw-r--r--lib/ci/cron_parser.rb9
-rw-r--r--spec/factories/ci/trigger_schedules.rb42
-rw-r--r--spec/factories/ci/triggers.rb4
-rw-r--r--spec/models/ci/trigger_schedule_spec.rb94
5 files changed, 102 insertions, 66 deletions
diff --git a/app/models/ci/trigger_schedule.rb b/app/models/ci/trigger_schedule.rb
index b9a99d86500..0df0a03d63e 100644
--- a/app/models/ci/trigger_schedule.rb
+++ b/app/models/ci/trigger_schedule.rb
@@ -18,12 +18,27 @@ module Ci
after_create :schedule_next_run!
def schedule_next_run!
+ puts "cron: #{cron.inspect} | cron_time_zone: #{cron_time_zone.inspect}"
next_time = Ci::CronParser.new(cron, cron_time_zone).next_time_from_now
if next_time.present?
update!(next_run_at: next_time)
end
end
+ def real_next_run(worker_cron: nil, worker_time_zone: nil)
+ puts "worker_cron: #{worker_cron.inspect} | worker_time_zone: #{worker_time_zone.inspect}"
+ 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_now
+
+ puts "next_run_at: #{next_run_at.inspect} | worker_next_time: #{worker_next_time.inspect}"
+ if next_run_at > worker_next_time
+ next_run_at
+ else
+ worker_next_time
+ end
+ end
+
private
def check_cron
@@ -40,9 +55,9 @@ module Ci
end
def check_ref
- if !trigger.ref.present?
+ if !ref.present?
self.errors.add(:ref, " is empty")
- elsif trigger.project.repository.ref_exists?(trigger.ref)
+ elsif project.repository.ref_exists?(ref)
self.errors.add(:ref, " does not exist")
end
end
diff --git a/lib/ci/cron_parser.rb b/lib/ci/cron_parser.rb
index be41474198d..a569de293b3 100644
--- a/lib/ci/cron_parser.rb
+++ b/lib/ci/cron_parser.rb
@@ -1,5 +1,8 @@
module Ci
class CronParser
+ VALID_SYNTAX_SAMPLE_TIME_ZONE = 'UTC'
+ VALID_SYNTAX_SAMPLE_CRON = '* * * * *'
+
def initialize(cron, cron_time_zone = 'UTC')
@cron = cron
@cron_time_zone = cron_time_zone
@@ -12,10 +15,8 @@ module Ci
end
def validation
- VALID_SYNTAX_TIME_ZONE = 'Europe/Istanbul'
- VALID_SYNTAX_CRON = '* * * * *'
- is_valid_cron = try_parse_cron(@cron, VALID_SYNTAX_TIME_ZONE).present?
- is_valid_cron_time_zone = try_parse_cron(VALID_SYNTAX_CRON, @cron_time_zone).present?
+ 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
diff --git a/spec/factories/ci/trigger_schedules.rb b/spec/factories/ci/trigger_schedules.rb
index 0dd397435c1..f572540d9e3 100644
--- a/spec/factories/ci/trigger_schedules.rb
+++ b/spec/factories/ci/trigger_schedules.rb
@@ -1,7 +1,7 @@
FactoryGirl.define do
factory :ci_trigger_schedule, class: Ci::TriggerSchedule do
project factory: :project
- trigger factory: :ci_trigger
+ trigger factory: :ci_trigger_with_ref
trait :force_triggable do
after(:create) do |trigger_schedule, evaluator|
@@ -11,49 +11,17 @@ FactoryGirl.define do
trait :cron_nightly_build do
cron '0 1 * * *'
- cron_time_zone 'Europe/Istanbul'
- # next_run_at do # TODO: Use CronParser
- # time = Time.now.in_time_zone(cron_time_zone)
- # time = time + 1.day if time.hour > 1
- # time = time.change(sec: 0, min: 0, hour: 1)
- # time
- # end
+ cron_time_zone Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
end
trait :cron_weekly_build do
- cron '0 1 * * 5'
- cron_time_zone 'Europe/Istanbul'
- # TODO: next_run_at
+ cron '0 1 * * 6'
+ cron_time_zone Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
end
trait :cron_monthly_build do
cron '0 1 22 * *'
- cron_time_zone 'Europe/Istanbul'
- # TODO: next_run_at
- end
-
- trait :cron_every_5_minutes do
- cron '*/5 * * * *'
- cron_time_zone 'Europe/Istanbul'
- # TODO: next_run_at
- end
-
- trait :cron_every_5_hours do
- cron '* */5 * * *'
- cron_time_zone 'Europe/Istanbul'
- # TODO: next_run_at
- end
-
- trait :cron_every_5_days do
- cron '* * */5 * *'
- cron_time_zone 'Europe/Istanbul'
- # TODO: next_run_at
- end
-
- trait :cron_every_5_months do
- cron '* * * */5 *'
- cron_time_zone 'Europe/Istanbul'
- # TODO: next_run_at
+ cron_time_zone Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
end
end
end
diff --git a/spec/factories/ci/triggers.rb b/spec/factories/ci/triggers.rb
index d38800b58f7..c9d2687b28e 100644
--- a/spec/factories/ci/triggers.rb
+++ b/spec/factories/ci/triggers.rb
@@ -2,6 +2,10 @@ FactoryGirl.define do
factory :ci_trigger_without_token, class: Ci::Trigger do
factory :ci_trigger do
token { SecureRandom.hex(15) }
+
+ factory :ci_trigger_with_ref do
+ ref 'master'
+ end
end
end
end
diff --git a/spec/models/ci/trigger_schedule_spec.rb b/spec/models/ci/trigger_schedule_spec.rb
index d8b6bd93954..11e8083fc86 100644
--- a/spec/models/ci/trigger_schedule_spec.rb
+++ b/spec/models/ci/trigger_schedule_spec.rb
@@ -5,37 +5,85 @@ describe Ci::TriggerSchedule, models: true do
let(:project) { create(:project) }
let(:trigger) { create(:ci_trigger, owner: user, project: project, ref: 'master') }
- describe 'associations' do
- it { is_expected.to belong_to(:project) }
- it { is_expected.to belong_to(:trigger) }
- end
+ it { is_expected.to belong_to(:project) }
+ it { is_expected.to belong_to(:trigger) }
+ # it { is_expected.to validate_presence_of :cron }
+ # it { is_expected.to validate_presence_of :cron_time_zone }
+ it { is_expected.to respond_to :ref }
- describe 'validation' do
- let(:trigger_schedule) { create(:ci_trigger_schedule, :cron_nightly_build, trigger: trigger) }
+ # describe '#schedule_next_run!' do
+ # let(:trigger_schedule) { create(:ci_trigger_schedule, :cron_nightly_build, next_run_at: nil, trigger: trigger) }
- it { expect(trigger_schedule).to validate_presence_of(:trigger) }
- it { is_expected.to validate_presence_of(:cron) }
- it { is_expected.to validate_presence_of(:cron_time_zone) }
+ # before do
+ # trigger_schedule.schedule_next_run!
+ # end
- it '#check_cron' do
- subject.cron = 'Hack'
- subject.valid?
- subject.errors[:screen_name].to include(' is invalid syntax')
- end
+ # it 'updates next_run_at' do
+ # expect(Ci::TriggerSchedule.last.next_run_at).not_to be_nil
+ # end
+ # end
- it '#check_ref' do
- end
- end
+ describe '#real_next_run' do
+ subject { trigger_schedule.real_next_run(worker_cron: worker_cron, worker_time_zone: worker_time_zone) }
+
+ context 'when next_run_at > worker_next_time' do
+ let(:worker_cron) { '0 */12 * * *' } # each 00:00, 12:00
+ let(:worker_time_zone) { 'UTC' }
+ let(:trigger_schedule) { create(:ci_trigger_schedule, :cron_weekly_build, cron_time_zone: user_time_zone, trigger: trigger) }
+
+ context 'when user is in Europe/London(+00:00)' do
+ let(:user_time_zone) { 'Europe/London' }
+
+ it 'returns next_run_at' do
+ is_expected.to eq(trigger_schedule.next_run_at)
+ end
+ end
+
+ context 'when user is in Asia/Hong_Kong(+08:00)' do
+ let(:user_time_zone) { 'Asia/Hong_Kong' }
- describe '#schedule_next_run!' do
- let(:trigger_schedule) { create(:ci_trigger_schedule, :cron_nightly_build, next_run_at: nil, trigger: trigger) }
+ it 'returns next_run_at' do
+ is_expected.to eq(trigger_schedule.next_run_at)
+ end
+ end
- before do
- trigger_schedule.schedule_next_run!
+ context 'when user is in Canada/Pacific(-08:00)' do
+ let(:user_time_zone) { 'Canada/Pacific' }
+
+ it 'returns next_run_at' do
+ is_expected.to eq(trigger_schedule.next_run_at)
+ end
+ end
end
- it 'updates next_run_at' do
- expect(Ci::TriggerSchedule.last.next_run_at).not_to be_nil
+ context 'when worker_next_time > next_run_at' do
+ let(:worker_cron) { '0 0 */2 * *' } # every 2 days
+ let(:worker_time_zone) { 'UTC' }
+ let(:trigger_schedule) { create(:ci_trigger_schedule, :cron_nightly_build, cron_time_zone: user_time_zone, trigger: trigger) }
+
+ context 'when user is in Europe/London(+00:00)' do
+ let(:user_time_zone) { 'Europe/London' }
+
+ it 'returns worker_next_time' do
+ is_expected.to eq(Ci::CronParser.new(worker_cron, worker_time_zone).next_time_from_now)
+ end
+ end
+
+ context 'when user is in Asia/Hong_Kong(+08:00)' do
+ let(:user_time_zone) { 'Asia/Hong_Kong' }
+
+ it 'returns worker_next_time' do
+ is_expected.to eq(Ci::CronParser.new(worker_cron, worker_time_zone).next_time_from_now)
+ end
+ end
+
+ context 'when user is in Canada/Pacific(-08:00)' do
+ let(:user_time_zone) { 'Canada/Pacific' }
+
+ it 'returns worker_next_time' do
+ is_expected.to eq(Ci::CronParser.new(worker_cron, worker_time_zone).next_time_from_now)
+ end
+ end
end
end
end