Fix StuckCiJobsWorker and added tests

This commit is contained in:
Shinya Maeda 2018-09-27 18:17:43 +09:00 committed by Alessio Caiazza
parent 71fc37c9cf
commit 587560757f
3 changed files with 43 additions and 2 deletions

View file

@ -84,7 +84,7 @@ class CommitStatus < ActiveRecord::Base
end
event :drop do
transition [:created, :pending, :running] => :failed
transition [:created, :pending, :running, :scheduled] => :failed
end
event :success do

View file

@ -68,7 +68,7 @@ class StuckCiJobsWorker
# `ci_builds` table has a partial index on `id` with `scheduled_at <> NULL` condition.
# Therefore this query's first step uses Index Search, and the following expensive
# filter `scheduled_at < ?` will only perform on a small subset (max: 100 rows)
Ci::Build.include(EachBatch).where('scheduled_at <> NULL').each_batch(of: 100) do |relation|
Ci::Build.include(EachBatch).where('scheduled_at IS NOT NULL').each_batch(of: 100) do |relation|
relation.where('scheduled_at < ?', BUILD_SCHEDULED_OUTDATED_TIMEOUT.ago).find_each do |build|
drop_build(:outdated, build, :scheduled, BUILD_SCHEDULED_OUTDATED_TIMEOUT, :schedule_expired)
end

View file

@ -127,6 +127,47 @@ describe StuckCiJobsWorker do
end
end
describe 'drop_stale_scheduled_builds' do
let(:status) { 'scheduled' }
let(:updated_at) { }
context 'when scheduled at 2 hours ago but it is not executed yet' do
let!(:job) { create(:ci_build, :scheduled, scheduled_at: 2.hours.ago) }
it 'drops the stale scheduled build' do
expect(Ci::Build.scheduled.count).to eq(1)
expect(job).to be_scheduled
worker.perform
job.reload
expect(Ci::Build.scheduled.count).to eq(0)
expect(job).to be_failed
expect(job).to be_schedule_expired
end
end
context 'when scheduled at 30 minutes ago but it is not executed yet' do
let!(:job) { create(:ci_build, :scheduled, scheduled_at: 30.minutes.ago) }
it 'does not drop the stale scheduled build yet' do
expect(Ci::Build.scheduled.count).to eq(1)
expect(job).to be_scheduled
worker.perform
expect(Ci::Build.scheduled.count).to eq(1)
expect(job).to be_scheduled
end
end
context 'when there are no stale scheduled builds' do
it 'does not drop the stale scheduled build yet' do
expect { worker.perform }.not_to raise_error
end
end
end
describe 'exclusive lease' do
let(:status) { 'running' }
let(:updated_at) { 2.days.ago }