2018-07-16 16:31:01 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
module Ci
|
|
|
|
class ProcessPipelineService < BaseService
|
|
|
|
attr_reader :pipeline
|
|
|
|
|
|
|
|
def execute(pipeline)
|
|
|
|
@pipeline = pipeline
|
|
|
|
|
2017-04-16 11:14:39 +00:00
|
|
|
update_retried
|
|
|
|
|
2016-10-20 07:33:44 +00:00
|
|
|
new_builds =
|
|
|
|
stage_indexes_of_created_builds.map do |index|
|
|
|
|
process_stage(index)
|
|
|
|
end
|
2016-08-29 14:47:31 +00:00
|
|
|
|
2016-10-20 07:33:44 +00:00
|
|
|
@pipeline.update_status
|
2016-10-07 12:52:30 +00:00
|
|
|
|
2016-10-20 07:33:44 +00:00
|
|
|
new_builds.flatten.any?
|
2016-08-11 13:22:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def process_stage(index)
|
|
|
|
current_status = status_for_prior_stages(index)
|
|
|
|
|
2018-09-27 04:52:15 +00:00
|
|
|
return if HasStatus::BLOCKED_STATUS.include?(current_status)
|
2017-03-03 11:51:23 +00:00
|
|
|
|
2016-10-20 07:33:44 +00:00
|
|
|
if HasStatus::COMPLETED_STATUSES.include?(current_status)
|
|
|
|
created_builds_in_stage(index).select do |build|
|
2016-10-27 11:34:09 +00:00
|
|
|
Gitlab::OptimisticLocking.retry_lock(build) do |subject|
|
2018-09-27 07:10:55 +00:00
|
|
|
Ci::ProcessBuildService.new(project, @user)
|
|
|
|
.execute(build, current_status)
|
2016-10-20 07:33:44 +00:00
|
|
|
end
|
2016-09-12 10:49:58 +00:00
|
|
|
end
|
2016-08-11 13:22:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-11 13:22:35 +00:00
|
|
|
def status_for_prior_stages(index)
|
|
|
|
pipeline.builds.where('stage_idx < ?', index).latest.status || 'success'
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-08-11 13:22:35 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-11 13:22:35 +00:00
|
|
|
def stage_indexes_of_created_builds
|
|
|
|
created_builds.order(:stage_idx).pluck('distinct stage_idx')
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-08-11 13:22:35 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-11 13:22:35 +00:00
|
|
|
def created_builds_in_stage(index)
|
|
|
|
created_builds.where(stage_idx: index)
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-08-11 13:22:35 +00:00
|
|
|
|
|
|
|
def created_builds
|
|
|
|
pipeline.builds.created
|
|
|
|
end
|
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
|
|
|
|
# find the latest builds for each name
|
|
|
|
latest_statuses = pipeline.statuses.latest
|
|
|
|
.group(:name)
|
|
|
|
.having('count(*) > 1')
|
|
|
|
.pluck('max(id)', 'name')
|
|
|
|
|
|
|
|
# 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 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-08-11 13:22:35 +00:00
|
|
|
end
|
|
|
|
end
|