Commit 3af60284 authored by Fabio Pitino's avatar Fabio Pitino

Merge branch 'optimize-running-stuck-5' into 'master'

Optimize StuckCiJobsWorker running builds query

See merge request gitlab-org/gitlab!71013
parents 790071b3 b4d7ee7b
......@@ -16,7 +16,12 @@ module Ci
private
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
......
---
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
job.update!(job_attributes)
end
context 'when job is running' do
let(:status) { 'running' }
around do |example|
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
let(:updated_at) { 2.hours.ago }
context 'when job is fresh' do
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
context 'when job was updated in less than 1 hour ago' do
let(:updated_at) { 30.minutes.ago }
include_examples 'running builds'
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
include_examples 'running builds'
end
%w(success skipped failed canceled scheduled pending).each do |status|
......@@ -51,15 +78,4 @@ RSpec.describe Ci::StuckBuilds::DropRunningService do
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
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