2021-03-22 20:09:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
class AbortPipelinesService
|
2021-04-12 11:09:30 -04:00
|
|
|
# NOTE: This call fails pipelines in bulk without running callbacks.
|
2021-04-15 14:09:01 -04:00
|
|
|
# Only for pipeline abandonment scenarios (examples: project delete)
|
2021-04-12 11:09:30 -04:00
|
|
|
def execute(pipelines, failure_reason)
|
|
|
|
pipelines.cancelable.each_batch(of: 100) do |pipeline_batch|
|
|
|
|
now = Time.current
|
2021-04-09 11:09:10 -04:00
|
|
|
|
2021-04-12 11:09:30 -04:00
|
|
|
basic_attributes = { status: :failed }
|
|
|
|
all_attributes = basic_attributes.merge(failure_reason: failure_reason, finished_at: now)
|
2021-03-22 20:09:09 -04:00
|
|
|
|
2021-04-12 11:09:30 -04:00
|
|
|
bulk_fail_for(Ci::Stage, pipeline_batch, basic_attributes)
|
|
|
|
bulk_fail_for(CommitStatus, pipeline_batch, all_attributes)
|
2021-04-09 11:09:10 -04:00
|
|
|
|
2021-04-12 11:09:30 -04:00
|
|
|
pipeline_batch.update_all(all_attributes)
|
2021-03-22 20:09:09 -04:00
|
|
|
end
|
2021-04-12 11:09:30 -04:00
|
|
|
|
|
|
|
ServiceResponse.success(message: 'Pipelines stopped')
|
2021-03-22 20:09:09 -04:00
|
|
|
end
|
|
|
|
|
2021-04-12 11:09:30 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def bulk_fail_for(klass, pipelines, attributes)
|
2021-03-22 20:09:09 -04:00
|
|
|
klass.in_pipelines(pipelines)
|
|
|
|
.cancelable
|
|
|
|
.in_batches(of: 150) # rubocop:disable Cop/InBatches
|
2021-04-09 11:09:10 -04:00
|
|
|
.update_all(attributes)
|
2021-03-22 20:09:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|