Commit 9573bb44 authored by Shinya Maeda's avatar Shinya Maeda

real_next_run (WIP)

parent d48658e3
...@@ -18,12 +18,27 @@ module Ci ...@@ -18,12 +18,27 @@ module Ci
after_create :schedule_next_run! after_create :schedule_next_run!
def 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 next_time = Ci::CronParser.new(cron, cron_time_zone).next_time_from_now
if next_time.present? if next_time.present?
update!(next_run_at: next_time) update!(next_run_at: next_time)
end end
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 private
def check_cron def check_cron
...@@ -40,9 +55,9 @@ module Ci ...@@ -40,9 +55,9 @@ module Ci
end end
def check_ref def check_ref
if !trigger.ref.present? if !ref.present?
self.errors.add(:ref, " is empty") 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") self.errors.add(:ref, " does not exist")
end end
end end
......
module Ci module Ci
class CronParser class CronParser
VALID_SYNTAX_SAMPLE_TIME_ZONE = 'UTC'
VALID_SYNTAX_SAMPLE_CRON = '* * * * *'
def initialize(cron, cron_time_zone = 'UTC') def initialize(cron, cron_time_zone = 'UTC')
@cron = cron @cron = cron
@cron_time_zone = cron_time_zone @cron_time_zone = cron_time_zone
...@@ -12,10 +15,8 @@ module Ci ...@@ -12,10 +15,8 @@ module Ci
end end
def validation def validation
VALID_SYNTAX_TIME_ZONE = 'Europe/Istanbul' is_valid_cron = try_parse_cron(@cron, VALID_SYNTAX_SAMPLE_TIME_ZONE).present?
VALID_SYNTAX_CRON = '* * * * *' is_valid_cron_time_zone = try_parse_cron(VALID_SYNTAX_SAMPLE_CRON, @cron_time_zone).present?
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?
return is_valid_cron, is_valid_cron_time_zone return is_valid_cron, is_valid_cron_time_zone
end end
......
FactoryGirl.define do FactoryGirl.define do
factory :ci_trigger_schedule, class: Ci::TriggerSchedule do factory :ci_trigger_schedule, class: Ci::TriggerSchedule do
project factory: :project project factory: :project
trigger factory: :ci_trigger trigger factory: :ci_trigger_with_ref
trait :force_triggable do trait :force_triggable do
after(:create) do |trigger_schedule, evaluator| after(:create) do |trigger_schedule, evaluator|
...@@ -11,49 +11,17 @@ FactoryGirl.define do ...@@ -11,49 +11,17 @@ FactoryGirl.define do
trait :cron_nightly_build do trait :cron_nightly_build do
cron '0 1 * * *' cron '0 1 * * *'
cron_time_zone 'Europe/Istanbul' cron_time_zone Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
# 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
end end
trait :cron_weekly_build do trait :cron_weekly_build do
cron '0 1 * * 5' cron '0 1 * * 6'
cron_time_zone 'Europe/Istanbul' cron_time_zone Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
# TODO: next_run_at
end end
trait :cron_monthly_build do trait :cron_monthly_build do
cron '0 1 22 * *' cron '0 1 22 * *'
cron_time_zone 'Europe/Istanbul' cron_time_zone Ci::CronParser::VALID_SYNTAX_SAMPLE_TIME_ZONE
# 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
end end
end end
end end
...@@ -2,6 +2,10 @@ FactoryGirl.define do ...@@ -2,6 +2,10 @@ FactoryGirl.define do
factory :ci_trigger_without_token, class: Ci::Trigger do factory :ci_trigger_without_token, class: Ci::Trigger do
factory :ci_trigger do factory :ci_trigger do
token { SecureRandom.hex(15) } token { SecureRandom.hex(15) }
factory :ci_trigger_with_ref do
ref 'master'
end
end end
end end
end end
...@@ -5,37 +5,85 @@ describe Ci::TriggerSchedule, models: true do ...@@ -5,37 +5,85 @@ describe Ci::TriggerSchedule, models: true do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:trigger) { create(:ci_trigger, owner: user, project: project, ref: 'master') } 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(:project) } it { is_expected.to belong_to(:trigger) }
it { is_expected.to belong_to(:trigger) } # it { is_expected.to validate_presence_of :cron }
end # it { is_expected.to validate_presence_of :cron_time_zone }
it { is_expected.to respond_to :ref }
describe 'validation' do # describe '#schedule_next_run!' do
let(:trigger_schedule) { create(:ci_trigger_schedule, :cron_nightly_build, trigger: trigger) } # 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) } # before do
it { is_expected.to validate_presence_of(:cron) } # trigger_schedule.schedule_next_run!
it { is_expected.to validate_presence_of(:cron_time_zone) } # end
it '#check_cron' do # it 'updates next_run_at' do
subject.cron = 'Hack' # expect(Ci::TriggerSchedule.last.next_run_at).not_to be_nil
subject.valid? # end
subject.errors[:screen_name].to include(' is invalid syntax') # end
end
it '#check_ref' do describe '#real_next_run' do
end subject { trigger_schedule.real_next_run(worker_cron: worker_cron, worker_time_zone: worker_time_zone) }
end
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 it 'returns next_run_at' do
let(:trigger_schedule) { create(:ci_trigger_schedule, :cron_nightly_build, next_run_at: nil, trigger: trigger) } is_expected.to eq(trigger_schedule.next_run_at)
end
end
before do context 'when user is in Canada/Pacific(-08:00)' do
trigger_schedule.schedule_next_run! let(:user_time_zone) { 'Canada/Pacific' }
it 'returns next_run_at' do
is_expected.to eq(trigger_schedule.next_run_at)
end
end
end end
it 'updates next_run_at' do context 'when worker_next_time > next_run_at' do
expect(Ci::TriggerSchedule.last.next_run_at).not_to be_nil 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 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