Class: Ci::AbortPipelinesService

Inherits:
Object
  • Object
show all
Defined in:
app/services/ci/abort_pipelines_service.rb

Constant Summary collapse

ABORT_PIPELINE_BATCHING_LIMIT =
100_000
PipelinesAbortLimitExceededError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#execute(pipelines, failure_reason) ⇒ Object

NOTE: This call fails pipelines in bulk without running callbacks. Only for pipeline abandonment scenarios (examples: project delete)



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
# File 'app/services/ci/abort_pipelines_service.rb', line 10

def execute(pipelines, failure_reason)
  batch_size = 100
  processed_count = 0

  loop do
    processed_count += 1
    if processed_count > ABORT_PIPELINE_BATCHING_LIMIT
      raise PipelinesAbortLimitExceededError, "Exceeded the maximum batching limit to abort pipelines"
    end

    # Limit to 100 pipelines per batch - marking the cancelable pipelines as failed in the loop removes them from
    # subsequent queries which is more efficient than each_batch.
    pipeline_ids = pipelines.cancelable.limit(batch_size).pluck_primary_key
    now = Time.current

    basic_attributes = { status: :failed }
    all_attributes = basic_attributes.merge(failure_reason: failure_reason, finished_at: now)

    bulk_fail_for(Ci::Stage, pipeline_ids, basic_attributes)
    bulk_fail_for(CommitStatus, pipeline_ids, all_attributes)

    update_size = ::Ci::Pipeline.id_in(pipeline_ids).update_all(all_attributes)
    break if update_size < batch_size
  end

  ServiceResponse.success(message: 'Pipelines stopped')
end