2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
module Ci
|
2019-11-19 07:06:00 -05:00
|
|
|
class ProcessPipelineService
|
2019-10-01 08:05:59 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
attr_reader :pipeline
|
|
|
|
|
2019-11-19 07:06:00 -05:00
|
|
|
def initialize(pipeline)
|
2016-08-11 09:22:35 -04:00
|
|
|
@pipeline = pipeline
|
2019-11-19 07:06:00 -05:00
|
|
|
end
|
2016-08-11 09:22:35 -04:00
|
|
|
|
2019-11-19 07:06:00 -05:00
|
|
|
def execute(trigger_build_ids = nil)
|
2017-04-16 07:14:39 -04:00
|
|
|
update_retried
|
|
|
|
|
2019-08-02 12:12:24 -04:00
|
|
|
success = process_stages_without_needs
|
2019-08-01 10:26:49 -04:00
|
|
|
|
|
|
|
# we evaluate dependent needs,
|
|
|
|
# only when the another job has finished
|
2019-08-02 13:31:01 -04:00
|
|
|
success = process_builds_with_needs(trigger_build_ids) || success
|
2016-08-29 10:47:31 -04:00
|
|
|
|
2016-10-20 03:33:44 -04:00
|
|
|
@pipeline.update_status
|
2016-10-07 08:52:30 -04:00
|
|
|
|
2019-08-01 10:26:49 -04:00
|
|
|
success
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-08-02 12:12:24 -04:00
|
|
|
def process_stages_without_needs
|
|
|
|
stage_indexes_of_created_processables_without_needs.flat_map do |index|
|
|
|
|
process_stage_without_needs(index)
|
|
|
|
end.any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_stage_without_needs(index)
|
2016-08-11 09:22:35 -04:00
|
|
|
current_status = status_for_prior_stages(index)
|
|
|
|
|
2019-08-02 12:12:24 -04:00
|
|
|
return unless HasStatus::COMPLETED_STATUSES.include?(current_status)
|
2017-03-03 06:51:23 -05:00
|
|
|
|
2019-10-01 08:05:59 -04:00
|
|
|
created_processables_in_stage_without_needs(index).find_each.select do |build|
|
2019-08-02 12:12:24 -04:00
|
|
|
process_build(build, current_status)
|
2019-10-01 08:05:59 -04:00
|
|
|
end.any?
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
2019-08-02 13:31:01 -04:00
|
|
|
def process_builds_with_needs(trigger_build_ids)
|
|
|
|
return false unless trigger_build_ids.present?
|
2019-08-14 09:59:44 -04:00
|
|
|
return false unless Feature.enabled?(:ci_dag_support, project, default_enabled: true)
|
2019-08-01 10:26:49 -04:00
|
|
|
|
2019-08-13 09:14:33 -04:00
|
|
|
# we find processables that are dependent:
|
|
|
|
# 1. because of current dependency,
|
|
|
|
trigger_build_names = pipeline.processables.latest
|
|
|
|
.for_ids(trigger_build_ids).names
|
2019-08-02 13:31:01 -04:00
|
|
|
|
2019-08-13 09:14:33 -04:00
|
|
|
# 2. does not have builds that not yet complete
|
|
|
|
incomplete_build_names = pipeline.processables.latest
|
|
|
|
.incomplete.names
|
|
|
|
|
|
|
|
# Each found processable is guaranteed here to have completed status
|
2019-08-01 10:26:49 -04:00
|
|
|
created_processables
|
2019-08-02 13:31:01 -04:00
|
|
|
.with_needs(trigger_build_names)
|
2019-08-13 09:14:33 -04:00
|
|
|
.without_needs(incomplete_build_names)
|
2019-08-01 10:26:49 -04:00
|
|
|
.find_each
|
|
|
|
.map(&method(:process_build_with_needs))
|
|
|
|
.any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_build_with_needs(build)
|
|
|
|
current_status = status_for_build_needs(build.needs.map(&:name))
|
|
|
|
|
|
|
|
return unless HasStatus::COMPLETED_STATUSES.include?(current_status)
|
|
|
|
|
2019-08-02 12:12:24 -04:00
|
|
|
process_build(build, current_status)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_build(build, current_status)
|
2019-08-01 10:26:49 -04:00
|
|
|
Gitlab::OptimisticLocking.retry_lock(build) do |subject|
|
2019-11-19 07:06:00 -05:00
|
|
|
Ci::ProcessBuildService.new(project, build.user)
|
2019-08-01 10:26:49 -04:00
|
|
|
.execute(subject, current_status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
def status_for_prior_stages(index)
|
2019-08-13 09:14:33 -04:00
|
|
|
pipeline.processables.status_for_prior_stages(index)
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
2019-08-01 10:26:49 -04:00
|
|
|
def status_for_build_needs(needs)
|
2019-08-13 09:14:33 -04:00
|
|
|
pipeline.processables.status_for_names(needs)
|
2019-08-01 10:26:49 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2019-08-02 12:12:24 -04:00
|
|
|
def stage_indexes_of_created_processables_without_needs
|
|
|
|
created_processables_without_needs.order(:stage_idx)
|
|
|
|
.pluck(Arel.sql('DISTINCT stage_idx'))
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-08-11 09:22:35 -04:00
|
|
|
|
2019-08-02 12:12:24 -04:00
|
|
|
def created_processables_in_stage_without_needs(index)
|
|
|
|
created_processables_without_needs
|
2019-10-01 08:05:59 -04:00
|
|
|
.with_preloads
|
2019-08-13 09:14:33 -04:00
|
|
|
.for_stage(index)
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
2019-08-02 12:12:24 -04:00
|
|
|
def created_processables_without_needs
|
2019-08-14 09:59:44 -04:00
|
|
|
if Feature.enabled?(:ci_dag_support, project, default_enabled: true)
|
2019-08-02 12:12:24 -04:00
|
|
|
pipeline.processables.created.without_needs
|
|
|
|
else
|
|
|
|
pipeline.processables.created
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-23 05:48:46 -05:00
|
|
|
def created_processables
|
2019-01-22 07:54:59 -05:00
|
|
|
pipeline.processables.created
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
2017-04-16 07:14:39 -04: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 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-04-16 07:14:39 -04:00
|
|
|
def update_retried
|
|
|
|
# find the latest builds for each name
|
|
|
|
latest_statuses = pipeline.statuses.latest
|
|
|
|
.group(:name)
|
|
|
|
.having('count(*) > 1')
|
2019-06-25 13:54:42 -04:00
|
|
|
.pluck(Arel.sql('MAX(id)'), 'name')
|
2017-04-16 07:14:39 -04:00
|
|
|
|
|
|
|
# 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-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2019-11-19 07:06:00 -05:00
|
|
|
|
|
|
|
def project
|
|
|
|
pipeline.project
|
|
|
|
end
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
end
|