From c12d959af376dd429fc22b2d738f790ad905bd7f Mon Sep 17 00:00:00 2001
From: drew stachon <730684-drew@users.noreply.gitlab.com>
Date: Fri, 25 Oct 2024 15:03:46 +0000
Subject: [PATCH] Delay writes from redundant pipeline service

---
 .../cancel_redundant_pipelines_service.rb     | 45 +++++++++++--------
 1 file changed, 26 insertions(+), 19 deletions(-)

diff --git a/app/services/ci/pipeline_creation/cancel_redundant_pipelines_service.rb b/app/services/ci/pipeline_creation/cancel_redundant_pipelines_service.rb
index 775336e89a2b7..b398153cb0007 100644
--- a/app/services/ci/pipeline_creation/cancel_redundant_pipelines_service.rb
+++ b/app/services/ci/pipeline_creation/cancel_redundant_pipelines_service.rb
@@ -25,7 +25,11 @@ def execute
         return if pipeline.parent_pipeline? # skip if child pipeline
         return unless project.auto_cancel_pending_pipelines?
 
-        auto_cancel_all_pipelines_with_cancelable_statuses
+        cancelable_pipelines.each do |cancelable_pipe|
+          configured_cancellation_for(cancelable_pipe)
+        end
+
+        log_cancelable_pipeline_outcomes
       end
 
       private
@@ -49,8 +53,8 @@ def ref_head_sha
       end
       strong_memoize_attr :ref_head_sha
 
-      def auto_cancel_all_pipelines_with_cancelable_statuses
-        cancelable_status_pipeline_pks.each_slice(PK_BATCH_SIZE) do |pks_batch|
+      def cancelable_pipelines
+        cancelable_status_pipeline_pks.each_slice(PK_BATCH_SIZE).with_object([]) do |pks_batch, cancelables|
           Ci::Pipeline.primary_key_in(pks_batch).order_id_asc.each do |cancelable|
             case cancelable.source.to_sym
             when *Enums::Ci::Pipeline.ci_sources.keys
@@ -74,24 +78,11 @@ def auto_cancel_all_pipelines_with_cancelable_statuses
               next
             end
 
-            # Cancel method based on configured strategy
-            configured_cancellation_for(cancelable)
+            # Keep the actual Pipeline instantiated
+            # so we can cancel it directly.
+            cancelables << cancelable
           end
         end
-
-        Gitlab::AppLogger.info(
-          class: self.class.name,
-          message: "Canceling redundant pipelines",
-          cancellable_count: cancelable_status_pipeline_pks.count,
-          skipped_for_old_age: @skipped_for_old_age,
-          conservatively_cancelled: @conservatively_cancelled,
-          aggressively_cancelled: @aggressively_cancelled,
-          configured_to_not_cancel: @configured_to_not_cancel,
-          canceled_by_pipeline_id: pipeline.id,
-          project_id: pipeline.project_id,
-          ref: pipeline.ref,
-          sha: pipeline.sha
-        )
       end
 
       def configured_cancellation_for(cancelable)
@@ -143,6 +134,22 @@ def cancel_pipeline(cancelable_pipeline, safe_cancellation:)
         ).force_execute
       end
 
+      def log_cancelable_pipeline_outcomes
+        Gitlab::AppLogger.info(
+          class: self.class.name,
+          message: "Canceling redundant pipelines",
+          cancellable_count: cancelable_status_pipeline_pks.count,
+          skipped_for_old_age: @skipped_for_old_age,
+          conservatively_cancelled: @conservatively_cancelled,
+          aggressively_cancelled: @aggressively_cancelled,
+          configured_to_not_cancel: @configured_to_not_cancel,
+          canceled_by_pipeline_id: pipeline.id,
+          project_id: pipeline.project_id,
+          ref: pipeline.ref,
+          sha: pipeline.sha
+        )
+      end
+
       def pipelines_created_after
         7.days.ago
       end
-- 
GitLab