Commit c426763c authored by Shinya Maeda's avatar Shinya Maeda

Rename ScheduledTrigger to TriggerSchedule. Because table structure changed.

parent e32c1a5c
......@@ -8,6 +8,7 @@ module Ci
belongs_to :owner, class_name: "User"
has_many :trigger_requests, dependent: :destroy
has_one :trigger_schedule, dependent: :destroy
validates :token, presence: true, uniqueness: true
......
module Ci
class ScheduledTrigger < ActiveRecord::Base
class TriggerSchedule < ActiveRecord::Base
extend Ci::Model
acts_as_paranoid
belongs_to :project
belongs_to :owner, class_name: "User"
belongs_to :trigger
def schedule_next_run!
next_time = Ci::CronParser.new(cron, cron_time_zone).next_time_from_now
......@@ -14,8 +14,8 @@ module Ci
end
end
def update_last_run!
update_attributes(last_run_at: Time.now)
end
# def update_last_run!
# update_attributes(last_run_at: Time.now)
# end
end
end
class ScheduledTriggerWorker
class TriggerScheduleWorker
include Sidekiq::Worker
include CronjobQueue
def perform
Ci::ScheduledTrigger.where("next_run_at < ?", Time.now).find_each do |trigger|
Ci::TriggerSchedule.where("next_run_at < ?", Time.now).find_each do |trigger|
begin
Ci::CreatePipelineService.new(trigger.project, trigger.owner, ref: trigger.ref).
execute(ignore_skip_ci: true, scheduled_trigger: true)
......@@ -11,7 +11,6 @@ class ScheduledTriggerWorker
Rails.logger.error "#{trigger.id}: Failed to trigger job: #{e.message}"
ensure
trigger.schedule_next_run!
trigger.update_last_run!
end
end
end
......
......@@ -181,7 +181,7 @@ production: &base
stuck_ci_jobs_worker:
cron: "0 * * * *"
# Execute scheduled triggers
scheduled_trigger_worker:
trigger_schedule_worker:
cron: "0 * * * *"
# Remove expired build artifacts
expire_build_artifacts_worker:
......
......@@ -315,9 +315,9 @@ Settings['cron_jobs'] ||= Settingslogic.new({})
Settings.cron_jobs['stuck_ci_jobs_worker'] ||= Settingslogic.new({})
Settings.cron_jobs['stuck_ci_jobs_worker']['cron'] ||= '0 * * * *'
Settings.cron_jobs['stuck_ci_jobs_worker']['job_class'] = 'StuckCiJobsWorker'
Settings.cron_jobs['scheduled_trigger_worker'] ||= Settingslogic.new({})
Settings.cron_jobs['scheduled_trigger_worker']['cron'] ||= '0 * * * *'
Settings.cron_jobs['scheduled_trigger_worker']['job_class'] = 'ScheduledTriggerWorker'
Settings.cron_jobs['trigger_schedule_worker'] ||= Settingslogic.new({})
Settings.cron_jobs['trigger_schedule_worker']['cron'] ||= '0 * * * *'
Settings.cron_jobs['trigger_schedule_worker']['job_class'] = 'TriggerScheduleWorker'
Settings.cron_jobs['expire_build_artifacts_worker'] ||= Settingslogic.new({})
Settings.cron_jobs['expire_build_artifacts_worker']['cron'] ||= '50 * * * *'
Settings.cron_jobs['expire_build_artifacts_worker']['job_class'] = 'ExpireBuildArtifactsWorker'
......
FactoryGirl.define do
factory :ci_scheduled_trigger, class: Ci::ScheduledTrigger do
factory :ci_trigger_schedule, class: Ci::TriggerSchedule do
project factory: :project
owner factory: :user
ref 'master'
trigger factory: :ci_trigger
trait :force_triggable do
next_run_at Time.now - 1.month
......
require 'spec_helper'
describe Ci::ScheduledTrigger, models: true do
describe Ci::TriggerSchedule, models: true do
describe 'associations' do
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:owner) }
it { is_expected.to belong_to(:trigger) }
end
describe '#schedule_next_run!' do
subject { scheduled_trigger.schedule_next_run! }
subject { trigger_schedule.schedule_next_run! }
let(:scheduled_trigger) { create(:ci_scheduled_trigger, :cron_nightly_build, next_run_at: nil) }
let(:trigger_schedule) { create(:ci_trigger_schedule, :cron_nightly_build, next_run_at: nil) }
it 'updates next_run_at' do
is_expected.not_to be_nil
end
end
describe '#update_last_run!' do
subject { scheduled_trigger.update_last_run! }
# describe '#update_last_run!' do
# subject { scheduled_trigger.update_last_run! }
let(:scheduled_trigger) { create(:ci_scheduled_trigger, :cron_nightly_build, last_run_at: nil) }
# let(:scheduled_trigger) { create(:ci_scheduled_trigger, :cron_nightly_build, last_run_at: nil) }
it 'updates last_run_at' do
is_expected.not_to be_nil
end
end
# it 'updates last_run_at' do
# is_expected.not_to be_nil
# end
# end
end
......@@ -7,6 +7,7 @@ describe Ci::Trigger, models: true do
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:owner) }
it { is_expected.to have_many(:trigger_requests) }
it { is_expected.to have_one(:trigger_schedule) }
end
describe 'before_validation' do
......
require 'spec_helper'
describe ScheduledTriggerWorker do
describe TriggerScheduleWorker do
let(:worker) { described_class.new }
before do
......@@ -9,7 +9,7 @@ describe ScheduledTriggerWorker do
context 'when there is a scheduled trigger within next_run_at' do
before do
create(:ci_scheduled_trigger, :cron_nightly_build, :force_triggable)
create(:ci_trigger_schedule, :cron_nightly_build, :force_triggable)
worker.perform
end
......@@ -18,13 +18,13 @@ describe ScheduledTriggerWorker do
end
it 'schedules next_run_at' do
scheduled_trigger2 = create(:ci_scheduled_trigger, :cron_nightly_build)
expect(Ci::ScheduledTrigger.last.next_run_at).to eq(scheduled_trigger2.next_run_at)
trigger_schedule2 = create(:ci_trigger_schedule, :cron_nightly_build)
expect(Ci::TriggerSchedule.last.next_run_at).to eq(trigger_schedule2.next_run_at)
end
end
context 'when there are no scheduled triggers within next_run_at' do
let!(:scheduled_trigger) { create(:ci_scheduled_trigger, :cron_nightly_build) }
let!(:trigger_schedule) { create(:ci_trigger_schedule, :cron_nightly_build) }
before do
worker.perform
......@@ -35,12 +35,12 @@ describe ScheduledTriggerWorker do
end
it 'do not reschedule next_run_at' do
expect(Ci::ScheduledTrigger.last.next_run_at).to eq(scheduled_trigger.next_run_at)
expect(Ci::TriggerSchedule.last.next_run_at).to eq(trigger_schedule.next_run_at)
end
end
context 'when next_run_at is nil' do
let!(:scheduled_trigger) { create(:ci_scheduled_trigger, :cron_nightly_build, next_run_at: nil) }
let!(:trigger_schedule) { create(:ci_trigger_schedule, :cron_nightly_build, next_run_at: nil) }
before do
worker.perform
......@@ -51,7 +51,7 @@ describe ScheduledTriggerWorker do
end
it 'do not reschedule next_run_at' do
expect(Ci::ScheduledTrigger.last.next_run_at).to eq(scheduled_trigger.next_run_at)
expect(Ci::TriggerSchedule.last.next_run_at).to eq(trigger_schedule.next_run_at)
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