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

131 lines
4 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
class ProcessPipelineService < BaseService
attr_reader :pipeline
2019-10-12 21:52:04 +05:30
def execute(pipeline, trigger_build_ids = nil)
2016-09-13 17:45:13 +05:30
@pipeline = pipeline
2017-08-17 22:00:37 +05:30
update_retried
2016-09-13 17:45:13 +05:30
2019-10-12 21:52:04 +05:30
success = process_stages_without_needs
# we evaluate dependent needs,
# only when the another job has finished
success = process_builds_with_needs(trigger_build_ids) || success
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
@pipeline.update_status
2016-11-03 12:29:30 +05:30
2019-10-12 21:52:04 +05:30
success
2016-09-13 17:45:13 +05:30
end
private
2019-10-12 21:52:04 +05:30
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-09-13 17:45:13 +05:30
current_status = status_for_prior_stages(index)
2019-10-12 21:52:04 +05:30
return unless HasStatus::COMPLETED_STATUSES.include?(current_status)
2017-08-17 22:00:37 +05:30
2019-10-12 21:52:04 +05:30
created_processables_in_stage_without_needs(index).select do |build|
process_build(build, current_status)
end
end
def process_builds_with_needs(trigger_build_ids)
return false unless trigger_build_ids.present?
return false unless Feature.enabled?(:ci_dag_support, project, default_enabled: true)
# we find processables that are dependent:
# 1. because of current dependency,
trigger_build_names = pipeline.processables.latest
.for_ids(trigger_build_ids).names
# 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
created_processables
.with_needs(trigger_build_names)
.without_needs(incomplete_build_names)
.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)
process_build(build, current_status)
end
def process_build(build, current_status)
Gitlab::OptimisticLocking.retry_lock(build) do |subject|
Ci::ProcessBuildService.new(project, @user)
.execute(subject, current_status)
2016-09-13 17:45:13 +05:30
end
end
def status_for_prior_stages(index)
2019-10-12 21:52:04 +05:30
pipeline.processables.status_for_prior_stages(index)
2016-09-13 17:45:13 +05:30
end
2019-10-12 21:52:04 +05:30
def status_for_build_needs(needs)
pipeline.processables.status_for_names(needs)
2016-09-13 17:45:13 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2019-10-12 21:52:04 +05:30
def stage_indexes_of_created_processables_without_needs
created_processables_without_needs.order(:stage_idx)
.pluck(Arel.sql('DISTINCT stage_idx'))
2016-09-13 17:45:13 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
2019-10-12 21:52:04 +05:30
def created_processables_in_stage_without_needs(index)
created_processables_without_needs
.for_stage(index)
end
def created_processables_without_needs
if Feature.enabled?(:ci_dag_support, project, default_enabled: true)
pipeline.processables.created.without_needs
else
pipeline.processables.created
end
end
2019-03-02 22:35:43 +05:30
def created_processables
pipeline.processables.created
2016-09-13 17:45:13 +05:30
end
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
2016-09-13 17:45:13 +05:30
end
end