Welcome to mirror list, hosted at ThFree Co, Russian Federation.

drop_pipeline_service.rb « ci « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 16d3abcbfa0c9210cde262322013c7bc312fdcd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

module Ci
  class DropPipelineService
    PRELOADED_COMMIT_STATUS_RELATIONS = [:project, :pipeline].freeze
    PRELOADED_CI_BUILD_RELATIONS = [:metadata, :deployment, :taggings].freeze

    # execute service asynchronously for each cancelable pipeline
    def execute_async_for_all(pipelines, failure_reason, context_user)
      pipelines.cancelable.select(:id).find_in_batches do |pipelines_batch|
        Ci::DropPipelineWorker.bulk_perform_async_with_contexts(
          pipelines_batch,
          arguments_proc: -> (pipeline) { [pipeline.id, failure_reason] },
          context_proc: -> (_) { { user: context_user } }
        )
      end
    end

    def execute(pipeline, failure_reason, retries: 3)
      Gitlab::OptimisticLocking.retry_lock(pipeline.cancelable_statuses, retries, name: 'ci_pipeline_drop_running') do |cancelables|
        cancelables.find_in_batches do |batch|
          preload_associations_for_drop(batch)

          batch.each do |job|
            job.drop(failure_reason)
          end
        end
      end
    end

    private

    # rubocop: disable CodeReuse/ActiveRecord
    def preload_associations_for_drop(commit_status_batch)
      ActiveRecord::Associations::Preloader.new.preload(commit_status_batch, PRELOADED_COMMIT_STATUS_RELATIONS)
      ActiveRecord::Associations::Preloader.new.preload(commit_status_batch.select { |job| job.is_a?(Ci::Build) }, PRELOADED_CI_BUILD_RELATIONS)
    end
    # rubocop: enable CodeReuse/ActiveRecord
  end
end