Commit 3335bd82 authored by Fabio Pitino's avatar Fabio Pitino

Merge branch 'fix/gb/avoid-inflating-memory-when-aborting-pipelines' into 'master'

Avoid inflating Redis memory when aborting pipelines

See merge request gitlab-org/gitlab!59018
parents 9c314c39 5db533f6
...@@ -5,26 +5,29 @@ module Ci ...@@ -5,26 +5,29 @@ module Ci
# Danger: Cancels in bulk without callbacks # Danger: Cancels in bulk without callbacks
# Only for pipeline abandonment scenarios (examples: project delete, user block) # Only for pipeline abandonment scenarios (examples: project delete, user block)
def execute(pipelines) def execute(pipelines)
bulk_abort!(pipelines.cancelable, status: :canceled) @time = Time.current
bulk_abort!(pipelines.cancelable, { status: :canceled })
ServiceResponse.success(message: 'Pipelines canceled') ServiceResponse.success(message: 'Pipelines canceled')
end end
private private
def bulk_abort!(pipelines, status:) def bulk_abort!(pipelines, attributes)
pipelines.each_batch(of: 100) do |pipeline_batch| pipelines.each_batch(of: 100) do |pipeline_batch|
update_status_for(Ci::Stage, pipeline_batch, status) update_status_for(Ci::Stage, pipeline_batch, attributes)
update_status_for(CommitStatus, pipeline_batch, status) update_status_for(CommitStatus, pipeline_batch, attributes.merge(finished_at: @time))
pipeline_batch.update_all(status: status, finished_at: Time.current)
pipeline_batch.update_all(attributes.merge(finished_at: @time))
end end
end end
def update_status_for(klass, pipelines, status) def update_status_for(klass, pipelines, attributes)
klass.in_pipelines(pipelines) klass.in_pipelines(pipelines)
.cancelable .cancelable
.in_batches(of: 150) # rubocop:disable Cop/InBatches .in_batches(of: 150) # rubocop:disable Cop/InBatches
.update_all(status: status) .update_all(attributes)
end end
end end
end end
---
title: Avoid inflating Redis memory when aborting pipelines
merge_request: 59018
author:
type: fixed
...@@ -45,6 +45,22 @@ RSpec.describe Ci::AbortPipelinesService do ...@@ -45,6 +45,22 @@ RSpec.describe Ci::AbortPipelinesService do
expect { described_class.new.execute(project_pipelines) }.not_to exceed_query_limit(control_count) expect { described_class.new.execute(project_pipelines) }.not_to exceed_query_limit(control_count)
end end
context 'with live build logs' do
before do
create(:ci_build_trace_chunk, build: cancelable_build)
end
it 'makes canceled builds with stale trace visible' do
expect(Ci::Build.with_stale_live_trace.count).to eq 0
travel_to(2.days.ago) do
described_class.new.execute(project.all_pipelines)
end
expect(Ci::Build.with_stale_live_trace.count).to eq 1
end
end
end end
context 'with user pipelines' do context 'with user pipelines' do
......
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