Commit b4d7ee7b authored by Allison Browne's avatar Allison Browne Committed by Fabio Pitino

Optimize StuckCiJobsWorker running builds query

parent 72148118
...@@ -16,7 +16,12 @@ module Ci ...@@ -16,7 +16,12 @@ module Ci
private private
def running_timed_out_builds def running_timed_out_builds
Ci::Build.running.updated_at_before(BUILD_RUNNING_OUTDATED_TIMEOUT.ago) if Feature.enabled?(:ci_new_query_for_running_stuck_jobs, default_enabled: :yaml)
running_builds = Ci::Build.running.created_at_before(BUILD_RUNNING_OUTDATED_TIMEOUT.ago).order(created_at: :asc, project_id: :asc) # rubocop: disable CodeReuse/ActiveRecord
Ci::Build.id_in(running_builds).updated_at_before(BUILD_RUNNING_OUTDATED_TIMEOUT.ago)
else
Ci::Build.running.updated_at_before(BUILD_RUNNING_OUTDATED_TIMEOUT.ago)
end
end end
end end
end end
......
---
name: ci_new_query_for_running_stuck_jobs
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/71013
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/339264
milestone: '14.4'
type: development
group: group::pipeline execution
default_enabled: false
...@@ -17,20 +17,47 @@ RSpec.describe Ci::StuckBuilds::DropRunningService do ...@@ -17,20 +17,47 @@ RSpec.describe Ci::StuckBuilds::DropRunningService do
job.update!(job_attributes) job.update!(job_attributes)
end end
context 'when job is running' do around do |example|
let(:status) { 'running' } freeze_time { example.run }
end
shared_examples 'running builds' do
context 'when job is running' do
let(:status) { 'running' }
let(:outdated_time) { described_class::BUILD_RUNNING_OUTDATED_TIMEOUT.ago - 30.minutes }
let(:fresh_time) { described_class::BUILD_RUNNING_OUTDATED_TIMEOUT.ago + 30.minutes }
context 'when job is outdated' do
let(:created_at) { outdated_time }
let(:updated_at) { outdated_time }
it_behaves_like 'job is dropped'
end
context 'when job was updated_at more than an hour ago' do context 'when job is fresh' do
let(:updated_at) { 2.hours.ago } let(:created_at) { fresh_time }
let(:updated_at) { fresh_time }
it_behaves_like 'job is dropped' it_behaves_like 'job is unchanged'
end
context 'when job freshly updated' do
let(:created_at) { outdated_time }
let(:updated_at) { fresh_time }
it_behaves_like 'job is unchanged'
end
end end
end
context 'when job was updated in less than 1 hour ago' do include_examples 'running builds'
let(:updated_at) { 30.minutes.ago }
it_behaves_like 'job is unchanged' context 'when ci_new_query_for_running_stuck_jobs flag is disabled' do
before do
stub_feature_flags(ci_new_query_for_running_stuck_jobs: false)
end end
include_examples 'running builds'
end end
%w(success skipped failed canceled scheduled pending).each do |status| %w(success skipped failed canceled scheduled pending).each do |status|
...@@ -51,15 +78,4 @@ RSpec.describe Ci::StuckBuilds::DropRunningService do ...@@ -51,15 +78,4 @@ RSpec.describe Ci::StuckBuilds::DropRunningService do
end end
end end
end end
context 'for deleted project' do
let(:status) { 'running' }
let(:updated_at) { 2.days.ago }
before do
job.project.update!(pending_delete: true)
end
it_behaves_like 'job is dropped'
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