2018-07-16 16:31:01 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
module Ci
|
2019-11-19 12:06:00 +00:00
|
|
|
class ProcessPipelineService
|
2016-08-11 13:22:35 +00:00
|
|
|
attr_reader :pipeline
|
|
|
|
|
2019-11-19 12:06:00 +00:00
|
|
|
def initialize(pipeline)
|
2016-08-11 13:22:35 +00:00
|
|
|
@pipeline = pipeline
|
2019-11-19 12:06:00 +00:00
|
|
|
end
|
2016-08-11 13:22:35 +00:00
|
|
|
|
2020-08-13 15:10:03 +00:00
|
|
|
def execute
|
2020-07-15 12:09:26 +00:00
|
|
|
increment_processing_counter
|
|
|
|
|
2017-04-16 11:14:39 +00:00
|
|
|
update_retried
|
|
|
|
|
2020-08-13 15:10:03 +00:00
|
|
|
Ci::PipelineProcessing::AtomicProcessingService
|
|
|
|
.new(pipeline)
|
|
|
|
.execute
|
2016-08-11 13:22:35 +00:00
|
|
|
end
|
|
|
|
|
2020-07-15 12:09:26 +00:00
|
|
|
def metrics
|
2021-04-15 15:09:11 +00:00
|
|
|
@metrics ||= ::Gitlab::Ci::Pipeline::Metrics
|
2020-07-15 12:09:26 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
private
|
|
|
|
|
2017-04-16 11:14:39 +00:00
|
|
|
# This method is for compatibility and data consistency and should be removed with 9.3 version of GitLab
|
|
|
|
# This replicates what is db/post_migrate/20170416103934_upate_retried_for_ci_build.rb
|
|
|
|
# and ensures that functionality will not be broken before migration is run
|
|
|
|
# this updates only when there are data that needs to be updated, there are two groups with no retried flag
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-04-16 11:14:39 +00:00
|
|
|
def update_retried
|
2021-03-09 12:08:52 +00:00
|
|
|
return if Feature.enabled?(:ci_remove_update_retried_from_process_pipeline, pipeline.project, default_enabled: :yaml)
|
|
|
|
|
2017-04-16 11:14:39 +00:00
|
|
|
# find the latest builds for each name
|
2020-09-28 09:09:35 +00:00
|
|
|
latest_statuses = pipeline.latest_statuses
|
2017-04-16 11:14:39 +00:00
|
|
|
.group(:name)
|
|
|
|
.having('count(*) > 1')
|
2019-06-25 17:54:42 +00:00
|
|
|
.pluck(Arel.sql('MAX(id)'), 'name')
|
2017-04-16 11:14:39 +00:00
|
|
|
|
|
|
|
# mark builds that are retried
|
2020-09-02 18:10:40 +00:00
|
|
|
if latest_statuses.any?
|
2021-02-04 18:09:22 +00:00
|
|
|
updated_count = pipeline.latest_statuses
|
|
|
|
.where(name: latest_statuses.map(&:second))
|
|
|
|
.where.not(id: latest_statuses.map(&:first))
|
|
|
|
.update_all(retried: true)
|
|
|
|
|
|
|
|
# This counter is temporary. It will be used to check whether if we still use this method or not
|
|
|
|
# after setting correct value of `GenericCommitStatus#retried`.
|
|
|
|
# More info: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/50465#note_491657115
|
2021-05-02 15:09:53 +00:00
|
|
|
if updated_count > 0
|
|
|
|
Gitlab::AppJsonLogger.info(event: 'update_retried_is_used',
|
|
|
|
project_id: pipeline.project.id,
|
|
|
|
pipeline_id: pipeline.id)
|
|
|
|
|
|
|
|
metrics.legacy_update_jobs_counter.increment
|
|
|
|
end
|
2020-09-02 18:10:40 +00:00
|
|
|
end
|
2017-04-16 11:14:39 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2020-07-15 12:09:26 +00:00
|
|
|
|
|
|
|
def increment_processing_counter
|
|
|
|
metrics.pipeline_processing_events_counter.increment
|
|
|
|
end
|
2016-08-11 13:22:35 +00:00
|
|
|
end
|
|
|
|
end
|