debian-mirror-gitlab/app/services/ci/process_pipeline_service.rb

61 lines
2.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
module Ci
2020-01-01 13:55:28 +05:30
class ProcessPipelineService
2016-09-13 17:45:13 +05:30
attr_reader :pipeline
2020-01-01 13:55:28 +05:30
def initialize(pipeline)
2016-09-13 17:45:13 +05:30
@pipeline = pipeline
2020-01-01 13:55:28 +05:30
end
2016-09-13 17:45:13 +05:30
2020-03-13 15:44:24 +05:30
def execute(trigger_build_ids = nil, initial_process: false)
2017-08-17 22:00:37 +05:30
update_retried
2020-03-13 15:44:24 +05:30
ensure_scheduling_type_for_processables
2016-09-13 17:45:13 +05:30
2020-03-13 15:44:24 +05:30
if Feature.enabled?(:ci_atomic_processing, pipeline.project)
Ci::PipelineProcessing::AtomicProcessingService
.new(pipeline)
.execute
2019-10-12 21:52:04 +05:30
else
2020-03-13 15:44:24 +05:30
Ci::PipelineProcessing::LegacyProcessingService
.new(pipeline)
.execute(trigger_build_ids, initial_process: initial_process)
2019-10-12 21:52:04 +05:30
end
end
2020-03-13 15:44:24 +05:30
private
2017-08-17 22:00:37 +05:30
# 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-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def update_retried
# find the latest builds for each name
latest_statuses = pipeline.statuses.latest
.group(:name)
.having('count(*) > 1')
2019-09-30 21:07:59 +05:30
.pluck(Arel.sql('MAX(id)'), 'name')
2017-08-17 22:00:37 +05:30
# mark builds that are retried
pipeline.statuses.latest
.where(name: latest_statuses.map(&:second))
.where.not(id: latest_statuses.map(&:first))
.update_all(retried: true) if latest_statuses.any?
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2020-01-01 13:55:28 +05:30
2020-03-13 15:44:24 +05:30
# Set scheduling type of processables if they were created before scheduling_type
# data was deployed (https://gitlab.com/gitlab-org/gitlab/-/merge_requests/22246).
# Given that this service runs multiple times during the pipeline
# life cycle we need to ensure we populate the data once.
# See more: https://gitlab.com/gitlab-org/gitlab/issues/205426
def ensure_scheduling_type_for_processables
lease = Gitlab::ExclusiveLease.new("set-scheduling-types:#{pipeline.id}", timeout: 1.hour.to_i)
return unless lease.try_obtain
pipeline.processables.populate_scheduling_type!
2020-01-01 13:55:28 +05:30
end
2016-09-13 17:45:13 +05:30
end
end