debian-mirror-gitlab/lib/gitlab/ci/pipeline/chain/cancel_pending_pipelines.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

72 lines
2.4 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
module Pipeline
module Chain
class CancelPendingPipelines < Chain::Base
include Chain::Helpers
2021-11-11 11:23:49 +05:30
BATCH_SIZE = 25
# rubocop: disable CodeReuse/ActiveRecord
2020-10-24 23:57:45 +05:30
def perform!
2022-11-25 23:54:43 +05:30
ff_enabled = Feature.enabled?(:ci_skip_auto_cancelation_on_child_pipelines, project)
return if ff_enabled && pipeline.parent_pipeline? # skip if child pipeline
2020-10-24 23:57:45 +05:30
return unless project.auto_cancel_pending_pipelines?
2022-11-25 23:54:43 +05:30
Gitlab::OptimisticLocking.retry_lock(auto_cancelable_pipelines(ff_enabled), name: 'cancel_pending_pipelines') do |cancelables|
2021-11-11 11:23:49 +05:30
cancelables.select(:id).each_batch(of: BATCH_SIZE) do |cancelables_batch|
auto_cancel_interruptible_pipelines(cancelables_batch.ids)
2020-10-24 23:57:45 +05:30
end
end
end
2021-11-11 11:23:49 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2020-10-24 23:57:45 +05:30
def break?
false
end
private
2022-11-25 23:54:43 +05:30
def auto_cancelable_pipelines(ff_enabled)
relation = project.all_pipelines
.created_after(1.week.ago)
2021-11-11 11:23:49 +05:30
.ci_and_parent_sources
.for_ref(pipeline.ref)
.where_not_sha(project.commit(pipeline.ref).try(:id))
2020-10-24 23:57:45 +05:30
.alive_or_scheduled
2022-11-25 23:54:43 +05:30
if ff_enabled
relation.id_not_in(pipeline.id)
else
relation.id_not_in(pipeline.same_family_pipeline_ids)
end
2021-11-11 11:23:49 +05:30
end
def auto_cancel_interruptible_pipelines(pipeline_ids)
::Ci::Pipeline
.id_in(pipeline_ids)
2020-10-24 23:57:45 +05:30
.with_only_interruptible_builds
2022-08-27 11:52:29 +05:30
.each do |cancelable_pipeline|
2022-11-25 23:54:43 +05:30
Gitlab::AppLogger.info(
class: self.class.name,
message: "Pipeline #{pipeline.id} auto-canceling pipeline #{cancelable_pipeline.id}",
canceled_pipeline_id: cancelable_pipeline.id,
canceled_by_pipeline_id: pipeline.id,
canceled_by_pipeline_source: pipeline.source
)
2022-08-27 11:52:29 +05:30
# cascade_to_children not needed because we iterate through descendants here
cancelable_pipeline.cancel_running(
auto_canceled_by_pipeline_id: pipeline.id,
cascade_to_children: false
)
end
2020-10-24 23:57:45 +05:30
end
end
end
end
end
end