From 8d1cdc940142590eab34d190def2ac3bcd51167f Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Tue, 7 Feb 2017 14:00:46 +0100 Subject: [PATCH 1/9] Update stuck and outdated builds cleanup worker --- app/workers/stuck_ci_builds_worker.rb | 40 ++++++-- spec/workers/stuck_ci_builds_worker_spec.rb | 100 ++++++++++++++------ 2 files changed, 103 insertions(+), 37 deletions(-) diff --git a/app/workers/stuck_ci_builds_worker.rb b/app/workers/stuck_ci_builds_worker.rb index b70df5a1afa..b9dac807b23 100644 --- a/app/workers/stuck_ci_builds_worker.rb +++ b/app/workers/stuck_ci_builds_worker.rb @@ -2,18 +2,42 @@ class StuckCiBuildsWorker include Sidekiq::Worker include CronjobQueue - BUILD_STUCK_TIMEOUT = 1.day + BUILD_RUNNING_OUTDATED_TIMEOUT = 1.hour + BUILD_PENDING_OUTDATED_TIMEOUT = 1.day + BUILD_PENDING_STUCK_TIMEOUT = 1.hour def perform Rails.logger.info 'Cleaning stuck builds' - builds = Ci::Build.joins(:project).running_or_pending.where('ci_builds.updated_at < ?', BUILD_STUCK_TIMEOUT.ago) - builds.find_each(batch_size: 50).each do |build| - Rails.logger.debug "Dropping stuck #{build.status} build #{build.id} for runner #{build.runner_id}" - build.drop - end + drop :running, BUILD_RUNNING_OUTDATED_TIMEOUT + drop :pending, BUILD_PENDING_OUTDATED_TIMEOUT + drop_stuck :pending, BUILD_PENDING_STUCK_TIMEOUT + end - # Update builds that failed to drop - builds.update_all(status: 'failed') + private + + def drop(status, timeout) + search(status, timeout) do |build| + drop_build :outdated, build, status, timeout + end + end + + def drop_stuck(status, timeout) + search(status, timeout) do |build| + return unless build.stuck? + drop_build :stuck, build, status, timeout + end + end + + def search(status, timeout) + builds = Ci::Build.where(status: status).where('ci_builds.updated_at < ?', timeout.ago) + builds.joins(:project).find_each(batch_size: 50).each do |build| + yield(build) + end + end + + def drop_build(type, build, status, timeout) + Rails.logger.info "#{self.class}: Dropping #{type.to_s} build #{build.id} for runner #{build.runner_id} (status: #{status}, timeout: #{timeout})" + build.drop end end diff --git a/spec/workers/stuck_ci_builds_worker_spec.rb b/spec/workers/stuck_ci_builds_worker_spec.rb index 801fa31b45d..e2386336447 100644 --- a/spec/workers/stuck_ci_builds_worker_spec.rb +++ b/spec/workers/stuck_ci_builds_worker_spec.rb @@ -1,7 +1,8 @@ -require "spec_helper" +require 'spec_helper' describe StuckCiBuildsWorker do - let!(:build) { create :ci_build } + let!(:runner) { create :ci_runner } + let!(:build) { create :ci_build, runner: runner } let(:worker) { described_class.new } subject do @@ -9,47 +10,88 @@ describe StuckCiBuildsWorker do build.status end - %w(pending running).each do |status| - context "#{status} build" do - before do - build.update!(status: status) + before { build.update!(status: status, updated_at: updated_at) } + + shared_examples 'build is dropped' do + it 'changes status' do + worker.perform + is_expected.to eq('failed') + end + end + + shared_examples 'build is unchanged' do + it "doesn't change status" do + worker.perform + is_expected.to eq(status) + end + end + + context 'when build is pending' do + let(:status) { 'pending' } + + context 'when build is not stuck' do + before { allow_any_instance_of(Ci::Build).to receive(:stuck?).and_return(false) } + + context 'when build was not updated for more than 1 day ago' do + let(:updated_at) { 2.days.ago } + it_behaves_like 'build is dropped' end - it 'gets dropped if it was updated over 2 days ago' do - build.update!(updated_at: 2.days.ago) - worker.perform - is_expected.to eq('failed') + context 'when build was updated in less than 1 day ago' do + let(:updated_at) { 6.hours.ago } + it_behaves_like 'build is unchanged' end - it "is still #{status}" do - build.update!(updated_at: 1.minute.ago) - worker.perform - is_expected.to eq(status) + context 'when build was not updated for more than 1 hour ago' do + let(:updated_at) { 2.hours.ago } + it_behaves_like 'build is unchanged' + end + end + + context 'when build is stuck' do + before { allow_any_instance_of(Ci::Build).to receive(:stuck?).and_return(true) } + + context 'when build was not updated for more than 1 hour ago' do + let(:updated_at) { 2.hours.ago } + it_behaves_like 'build is dropped' + end + + context 'when build was updated in less than 1 hour ago' do + let(:updated_at) { 30.minutes.ago } + it_behaves_like 'build is unchanged' end end end - %w(success failed canceled).each do |status| - context "#{status} build" do - before do - build.update!(status: status) - end + context 'when build is running' do + let(:status) { 'running' } - it "is still #{status}" do - build.update!(updated_at: 2.days.ago) - worker.perform - is_expected.to eq(status) - end + context 'when build was not updated for more than 1 hour ago' do + let(:updated_at) { 2.hours.ago } + it_behaves_like 'build is dropped' + end + + context 'when build was updated in less than 1 hour ago' do + let(:updated_at) { 30.minutes.ago } + it_behaves_like 'build is unchanged' end end - context "for deleted project" do - before do - build.update!(status: :running, updated_at: 2.days.ago) - build.project.update(pending_delete: true) + %w(success skipped failed canceled).each do |status| + context "when build is #{status}" do + let(:status) { status } + let(:updated_at) { 2.days.ago } + it_behaves_like 'build is unchanged' end + end - it "does not drop build" do + context 'for deleted project' do + let(:status) { 'running' } + let(:updated_at) { 2.days.ago } + + before { build.project.update(pending_delete: true) } + + it 'does not drop build' do expect_any_instance_of(Ci::Build).not_to receive(:drop) worker.perform end From a4e996d77315a9d2598e32fab42436b1a04fadcb Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Tue, 7 Feb 2017 14:10:48 +0100 Subject: [PATCH 2/9] Update default configuration of stuck_ci_builds_worker cron interval --- .../27523-make-stuck-build-detection-more-performant.yml | 4 ++++ config/gitlab.yml.example | 2 +- config/initializers/1_settings.rb | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 changelogs/unreleased/27523-make-stuck-build-detection-more-performant.yml diff --git a/changelogs/unreleased/27523-make-stuck-build-detection-more-performant.yml b/changelogs/unreleased/27523-make-stuck-build-detection-more-performant.yml new file mode 100644 index 00000000000..a4ef2b23aaa --- /dev/null +++ b/changelogs/unreleased/27523-make-stuck-build-detection-more-performant.yml @@ -0,0 +1,4 @@ +--- +title: Make stuck builds detection more performant +merge_request: 9025 +author: diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example index a82ff605a70..b4f47b30622 100644 --- a/config/gitlab.yml.example +++ b/config/gitlab.yml.example @@ -179,7 +179,7 @@ production: &base cron_jobs: # Flag stuck CI builds as failed stuck_ci_builds_worker: - cron: "0 0 * * *" + cron: "0 * * * *" # Remove expired build artifacts expire_build_artifacts_worker: cron: "50 * * * *" diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb index c64ae15fa92..3ec57c5bb52 100644 --- a/config/initializers/1_settings.rb +++ b/config/initializers/1_settings.rb @@ -309,7 +309,7 @@ Settings.gravatar['host'] = Settings.host_without_www(Settings.gravatar[ # Settings['cron_jobs'] ||= Settingslogic.new({}) Settings.cron_jobs['stuck_ci_builds_worker'] ||= Settingslogic.new({}) -Settings.cron_jobs['stuck_ci_builds_worker']['cron'] ||= '0 0 * * *' +Settings.cron_jobs['stuck_ci_builds_worker']['cron'] ||= '0 * * * *' Settings.cron_jobs['stuck_ci_builds_worker']['job_class'] = 'StuckCiBuildsWorker' Settings.cron_jobs['expire_build_artifacts_worker'] ||= Settingslogic.new({}) Settings.cron_jobs['expire_build_artifacts_worker']['cron'] ||= '50 * * * *' From 0ba385b363879f270c767aab6c1799a8e675a263 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Tue, 7 Feb 2017 23:06:16 +0100 Subject: [PATCH 3/9] Add exclusive lease for stuck_ci_builds_worker --- app/workers/stuck_ci_builds_worker.rb | 13 +++++++++++-- spec/workers/stuck_ci_builds_worker_spec.rb | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/workers/stuck_ci_builds_worker.rb b/app/workers/stuck_ci_builds_worker.rb index b9dac807b23..4eba311d063 100644 --- a/app/workers/stuck_ci_builds_worker.rb +++ b/app/workers/stuck_ci_builds_worker.rb @@ -7,7 +7,9 @@ class StuckCiBuildsWorker BUILD_PENDING_STUCK_TIMEOUT = 1.hour def perform - Rails.logger.info 'Cleaning stuck builds' + return unless try_obtain_lease + + Rails.logger.info "#{self.class}: Cleaning stuck builds" drop :running, BUILD_RUNNING_OUTDATED_TIMEOUT drop :pending, BUILD_PENDING_OUTDATED_TIMEOUT @@ -16,6 +18,13 @@ class StuckCiBuildsWorker private + def try_obtain_lease + Gitlab::ExclusiveLease.new( + 'stuck_ci_builds_worker_lease', + timeout: 30.minutes + ).try_obtain + end + def drop(status, timeout) search(status, timeout) do |build| drop_build :outdated, build, status, timeout @@ -37,7 +46,7 @@ class StuckCiBuildsWorker end def drop_build(type, build, status, timeout) - Rails.logger.info "#{self.class}: Dropping #{type.to_s} build #{build.id} for runner #{build.runner_id} (status: #{status}, timeout: #{timeout})" + Rails.logger.info "#{self.class}: Dropping #{type} build #{build.id} for runner #{build.runner_id} (status: #{status}, timeout: #{timeout})" build.drop end end diff --git a/spec/workers/stuck_ci_builds_worker_spec.rb b/spec/workers/stuck_ci_builds_worker_spec.rb index e2386336447..c88a0dfa4a6 100644 --- a/spec/workers/stuck_ci_builds_worker_spec.rb +++ b/spec/workers/stuck_ci_builds_worker_spec.rb @@ -10,7 +10,10 @@ describe StuckCiBuildsWorker do build.status end - before { build.update!(status: status, updated_at: updated_at) } + before do + build.update!(status: status, updated_at: updated_at) + allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true) + end shared_examples 'build is dropped' do it 'changes status' do @@ -96,4 +99,16 @@ describe StuckCiBuildsWorker do worker.perform end end + + describe 'exclusive lease' do + let(:status) { 'running' } + let(:updated_at) { 2.days.ago } + + it 'is guard by exclusive lease' do + worker.perform + allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(false) + expect(worker).not_to receive(:drop) + worker.perform + end + end end From e9f83c4bd9d6b596d647f9347d0014b3ab2cca3e Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Tue, 7 Feb 2017 23:08:02 +0100 Subject: [PATCH 4/9] Add Gitlab::OptimisticLocking for build dropping --- app/workers/stuck_ci_builds_worker.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/workers/stuck_ci_builds_worker.rb b/app/workers/stuck_ci_builds_worker.rb index 4eba311d063..3b18fe3dd26 100644 --- a/app/workers/stuck_ci_builds_worker.rb +++ b/app/workers/stuck_ci_builds_worker.rb @@ -47,6 +47,8 @@ class StuckCiBuildsWorker def drop_build(type, build, status, timeout) Rails.logger.info "#{self.class}: Dropping #{type} build #{build.id} for runner #{build.runner_id} (status: #{status}, timeout: #{timeout})" - build.drop + Gitlab::OptimisticLocking.retry_lock(build, 3) do |b| + b.drop + end end end From 48cb391cf7f11bae785fb89208fb5cf0303e326f Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Tue, 7 Feb 2017 23:22:14 +0100 Subject: [PATCH 5/9] Improve builds search query --- app/workers/stuck_ci_builds_worker.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/workers/stuck_ci_builds_worker.rb b/app/workers/stuck_ci_builds_worker.rb index 3b18fe3dd26..1c1e71cca23 100644 --- a/app/workers/stuck_ci_builds_worker.rb +++ b/app/workers/stuck_ci_builds_worker.rb @@ -40,7 +40,7 @@ class StuckCiBuildsWorker def search(status, timeout) builds = Ci::Build.where(status: status).where('ci_builds.updated_at < ?', timeout.ago) - builds.joins(:project).find_each(batch_size: 50).each do |build| + builds.joins(:project).includes(project: :namespace).includes(:tags).includes(:runner).find_each(batch_size: 50).each do |build| yield(build) end end From 6db7c232f402b677e9a613161e419141017232b9 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Thu, 9 Feb 2017 03:29:38 +0100 Subject: [PATCH 6/9] Add minor updates --- app/workers/stuck_ci_builds_worker.rb | 18 +++++++++++----- spec/workers/stuck_ci_builds_worker_spec.rb | 23 +++++++++++++++++---- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/app/workers/stuck_ci_builds_worker.rb b/app/workers/stuck_ci_builds_worker.rb index 1c1e71cca23..e9bd3f180b7 100644 --- a/app/workers/stuck_ci_builds_worker.rb +++ b/app/workers/stuck_ci_builds_worker.rb @@ -2,6 +2,8 @@ class StuckCiBuildsWorker include Sidekiq::Worker include CronjobQueue + EXCLUSIVE_LEASE_KEY = 'stuck_ci_builds_worker_lease' + BUILD_RUNNING_OUTDATED_TIMEOUT = 1.hour BUILD_PENDING_OUTDATED_TIMEOUT = 1.day BUILD_PENDING_STUCK_TIMEOUT = 1.hour @@ -11,20 +13,26 @@ class StuckCiBuildsWorker Rails.logger.info "#{self.class}: Cleaning stuck builds" - drop :running, BUILD_RUNNING_OUTDATED_TIMEOUT - drop :pending, BUILD_PENDING_OUTDATED_TIMEOUT + drop :running, BUILD_RUNNING_OUTDATED_TIMEOUT + drop :pending, BUILD_PENDING_OUTDATED_TIMEOUT drop_stuck :pending, BUILD_PENDING_STUCK_TIMEOUT + + remove_lease end private def try_obtain_lease - Gitlab::ExclusiveLease.new( - 'stuck_ci_builds_worker_lease', - timeout: 30.minutes + @uuid = Gitlab::ExclusiveLease.new( + EXCLUSIVE_LEASE_KEY, + timeout: 30.minutes ).try_obtain end + def remove_lease + Gitlab::ExclusiveLease.cancel(EXCLUSIVE_LEASE_KEY, @uuid) + end + def drop(status, timeout) search(status, timeout) do |build| drop_build :outdated, build, status, timeout diff --git a/spec/workers/stuck_ci_builds_worker_spec.rb b/spec/workers/stuck_ci_builds_worker_spec.rb index c88a0dfa4a6..5b87718a795 100644 --- a/spec/workers/stuck_ci_builds_worker_spec.rb +++ b/spec/workers/stuck_ci_builds_worker_spec.rb @@ -4,6 +4,7 @@ describe StuckCiBuildsWorker do let!(:runner) { create :ci_runner } let!(:build) { create :ci_build, runner: runner } let(:worker) { described_class.new } + let(:exclusive_lease_uuid) { SecureRandom.uuid } subject do build.reload @@ -12,7 +13,7 @@ describe StuckCiBuildsWorker do before do build.update!(status: status, updated_at: updated_at) - allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true) + allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(exclusive_lease_uuid) end shared_examples 'build is dropped' do @@ -103,12 +104,26 @@ describe StuckCiBuildsWorker do describe 'exclusive lease' do let(:status) { 'running' } let(:updated_at) { 2.days.ago } + let(:worker2) { described_class.new } - it 'is guard by exclusive lease' do + it 'is guard by exclusive lease when executed concurrently' do + expect(worker).to receive(:drop).at_least(:once) + expect(worker2).not_to receive(:drop) worker.perform allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(false) - expect(worker).not_to receive(:drop) + worker2.perform + end + + it 'can be executed in sequence' do + expect(worker).to receive(:drop).at_least(:once) + expect(worker2).to receive(:drop).at_least(:once) + worker.perform + worker2.perform + end + + it 'cancels exclusive lease after worker perform' do + expect(Gitlab::ExclusiveLease).to receive(:cancel).with(described_class::EXCLUSIVE_LEASE_KEY, exclusive_lease_uuid) worker.perform end end -end +end \ No newline at end of file From 44ef6806cab0b4bca3d94f9254fd1c34f350ed72 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Wed, 1 Mar 2017 13:56:54 +0100 Subject: [PATCH 7/9] Fix rubocop offenses --- app/workers/stuck_ci_builds_worker.rb | 7 ++----- spec/workers/stuck_ci_builds_worker_spec.rb | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/app/workers/stuck_ci_builds_worker.rb b/app/workers/stuck_ci_builds_worker.rb index e9bd3f180b7..c89c5832afd 100644 --- a/app/workers/stuck_ci_builds_worker.rb +++ b/app/workers/stuck_ci_builds_worker.rb @@ -2,7 +2,7 @@ class StuckCiBuildsWorker include Sidekiq::Worker include CronjobQueue - EXCLUSIVE_LEASE_KEY = 'stuck_ci_builds_worker_lease' + EXCLUSIVE_LEASE_KEY = 'stuck_ci_builds_worker_lease'.freeze BUILD_RUNNING_OUTDATED_TIMEOUT = 1.hour BUILD_PENDING_OUTDATED_TIMEOUT = 1.day @@ -23,10 +23,7 @@ class StuckCiBuildsWorker private def try_obtain_lease - @uuid = Gitlab::ExclusiveLease.new( - EXCLUSIVE_LEASE_KEY, - timeout: 30.minutes - ).try_obtain + @uuid = Gitlab::ExclusiveLease.new(EXCLUSIVE_LEASE_KEY, timeout: 30.minutes).try_obtain end def remove_lease diff --git a/spec/workers/stuck_ci_builds_worker_spec.rb b/spec/workers/stuck_ci_builds_worker_spec.rb index 5b87718a795..82bdc2b14f3 100644 --- a/spec/workers/stuck_ci_builds_worker_spec.rb +++ b/spec/workers/stuck_ci_builds_worker_spec.rb @@ -126,4 +126,4 @@ describe StuckCiBuildsWorker do worker.perform end end -end \ No newline at end of file +end From b66fe22a09d3969cd44c7eadab67eee8370fb998 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Wed, 1 Mar 2017 13:57:13 +0100 Subject: [PATCH 8/9] Make 'joins' line shorter --- app/workers/stuck_ci_builds_worker.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/workers/stuck_ci_builds_worker.rb b/app/workers/stuck_ci_builds_worker.rb index c89c5832afd..0c51c34a47f 100644 --- a/app/workers/stuck_ci_builds_worker.rb +++ b/app/workers/stuck_ci_builds_worker.rb @@ -45,7 +45,7 @@ class StuckCiBuildsWorker def search(status, timeout) builds = Ci::Build.where(status: status).where('ci_builds.updated_at < ?', timeout.ago) - builds.joins(:project).includes(project: :namespace).includes(:tags).includes(:runner).find_each(batch_size: 50).each do |build| + builds.joins(:project).includes(:tags, :runner, project: :namespace).find_each(batch_size: 50).each do |build| yield(build) end end From 4d4e99a2f163408de44d39ea98131a4231667c24 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Fri, 3 Mar 2017 00:43:39 +0100 Subject: [PATCH 9/9] Renable StuckCiBuildsWorker to StucjCiJobsWorker --- ...ilds_worker.rb => stuck_ci_jobs_worker.rb} | 2 +- config/gitlab.yml.example | 4 +- config/initializers/1_settings.rb | 6 +- ...r_spec.rb => stuck_ci_jobs_worker_spec.rb} | 58 +++++++++---------- 4 files changed, 35 insertions(+), 35 deletions(-) rename app/workers/{stuck_ci_builds_worker.rb => stuck_ci_jobs_worker.rb} (98%) rename spec/workers/{stuck_ci_builds_worker_spec.rb => stuck_ci_jobs_worker_spec.rb} (63%) diff --git a/app/workers/stuck_ci_builds_worker.rb b/app/workers/stuck_ci_jobs_worker.rb similarity index 98% rename from app/workers/stuck_ci_builds_worker.rb rename to app/workers/stuck_ci_jobs_worker.rb index 0c51c34a47f..ae8c980c9e4 100644 --- a/app/workers/stuck_ci_builds_worker.rb +++ b/app/workers/stuck_ci_jobs_worker.rb @@ -1,4 +1,4 @@ -class StuckCiBuildsWorker +class StuckCiJobsWorker include Sidekiq::Worker include CronjobQueue diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example index b4f47b30622..8f99a4d541f 100644 --- a/config/gitlab.yml.example +++ b/config/gitlab.yml.example @@ -177,8 +177,8 @@ production: &base # Periodically executed jobs, to self-heal Gitlab, do external synchronizations, etc. # Please read here for more information: https://github.com/ondrejbartas/sidekiq-cron#adding-cron-job cron_jobs: - # Flag stuck CI builds as failed - stuck_ci_builds_worker: + # Flag stuck CI jobs as failed + stuck_ci_jobs_worker: cron: "0 * * * *" # Remove expired build artifacts expire_build_artifacts_worker: diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb index 3ec57c5bb52..5aeaa7eab81 100644 --- a/config/initializers/1_settings.rb +++ b/config/initializers/1_settings.rb @@ -308,9 +308,9 @@ Settings.gravatar['host'] = Settings.host_without_www(Settings.gravatar[ # Cron Jobs # Settings['cron_jobs'] ||= Settingslogic.new({}) -Settings.cron_jobs['stuck_ci_builds_worker'] ||= Settingslogic.new({}) -Settings.cron_jobs['stuck_ci_builds_worker']['cron'] ||= '0 * * * *' -Settings.cron_jobs['stuck_ci_builds_worker']['job_class'] = 'StuckCiBuildsWorker' +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['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' diff --git a/spec/workers/stuck_ci_builds_worker_spec.rb b/spec/workers/stuck_ci_jobs_worker_spec.rb similarity index 63% rename from spec/workers/stuck_ci_builds_worker_spec.rb rename to spec/workers/stuck_ci_jobs_worker_spec.rb index 82bdc2b14f3..8434b0c8e5b 100644 --- a/spec/workers/stuck_ci_builds_worker_spec.rb +++ b/spec/workers/stuck_ci_jobs_worker_spec.rb @@ -1,91 +1,91 @@ require 'spec_helper' -describe StuckCiBuildsWorker do +describe StuckCiJobsWorker do let!(:runner) { create :ci_runner } - let!(:build) { create :ci_build, runner: runner } + let!(:job) { create :ci_build, runner: runner } let(:worker) { described_class.new } let(:exclusive_lease_uuid) { SecureRandom.uuid } subject do - build.reload - build.status + job.reload + job.status end before do - build.update!(status: status, updated_at: updated_at) + job.update!(status: status, updated_at: updated_at) allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(exclusive_lease_uuid) end - shared_examples 'build is dropped' do + shared_examples 'job is dropped' do it 'changes status' do worker.perform is_expected.to eq('failed') end end - shared_examples 'build is unchanged' do + shared_examples 'job is unchanged' do it "doesn't change status" do worker.perform is_expected.to eq(status) end end - context 'when build is pending' do + context 'when job is pending' do let(:status) { 'pending' } - context 'when build is not stuck' do + context 'when job is not stuck' do before { allow_any_instance_of(Ci::Build).to receive(:stuck?).and_return(false) } - context 'when build was not updated for more than 1 day ago' do + context 'when job was not updated for more than 1 day ago' do let(:updated_at) { 2.days.ago } - it_behaves_like 'build is dropped' + it_behaves_like 'job is dropped' end - context 'when build was updated in less than 1 day ago' do + context 'when job was updated in less than 1 day ago' do let(:updated_at) { 6.hours.ago } - it_behaves_like 'build is unchanged' + it_behaves_like 'job is unchanged' end - context 'when build was not updated for more than 1 hour ago' do + context 'when job was not updated for more than 1 hour ago' do let(:updated_at) { 2.hours.ago } - it_behaves_like 'build is unchanged' + it_behaves_like 'job is unchanged' end end - context 'when build is stuck' do + context 'when job is stuck' do before { allow_any_instance_of(Ci::Build).to receive(:stuck?).and_return(true) } - context 'when build was not updated for more than 1 hour ago' do + context 'when job was not updated for more than 1 hour ago' do let(:updated_at) { 2.hours.ago } - it_behaves_like 'build is dropped' + it_behaves_like 'job is dropped' end - context 'when build was updated in less than 1 hour ago' do + context 'when job was updated in less than 1 hour ago' do let(:updated_at) { 30.minutes.ago } - it_behaves_like 'build is unchanged' + it_behaves_like 'job is unchanged' end end end - context 'when build is running' do + context 'when job is running' do let(:status) { 'running' } - context 'when build was not updated for more than 1 hour ago' do + context 'when job was not updated for more than 1 hour ago' do let(:updated_at) { 2.hours.ago } - it_behaves_like 'build is dropped' + it_behaves_like 'job is dropped' end - context 'when build was updated in less than 1 hour ago' do + context 'when job was updated in less than 1 hour ago' do let(:updated_at) { 30.minutes.ago } - it_behaves_like 'build is unchanged' + it_behaves_like 'job is unchanged' end end %w(success skipped failed canceled).each do |status| - context "when build is #{status}" do + context "when job is #{status}" do let(:status) { status } let(:updated_at) { 2.days.ago } - it_behaves_like 'build is unchanged' + it_behaves_like 'job is unchanged' end end @@ -93,9 +93,9 @@ describe StuckCiBuildsWorker do let(:status) { 'running' } let(:updated_at) { 2.days.ago } - before { build.project.update(pending_delete: true) } + before { job.project.update(pending_delete: true) } - it 'does not drop build' do + it 'does not drop job' do expect_any_instance_of(Ci::Build).not_to receive(:drop) worker.perform end