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