2017-02-13 14:14:40 +00:00
|
|
|
module Ci
|
2017-02-14 09:38:17 +00:00
|
|
|
class RetryPipelineService < ::BaseService
|
|
|
|
def execute(pipeline)
|
2017-02-13 14:14:40 +00:00
|
|
|
@pipeline = pipeline
|
|
|
|
|
2017-02-14 09:38:17 +00:00
|
|
|
unless can?(current_user, :update_pipeline, pipeline)
|
2017-02-13 14:14:40 +00:00
|
|
|
raise Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
|
2017-02-14 10:38:19 +00:00
|
|
|
pipeline.mark_as_processable_after_stage(resume_stage.index)
|
|
|
|
|
2017-02-14 11:20:02 +00:00
|
|
|
retryable_builds_in_subsequent_stages do |build|
|
|
|
|
Ci::RetryBuildService.new(project, current_user)
|
|
|
|
.reprocess(build)
|
|
|
|
end
|
|
|
|
|
|
|
|
retryable_builds_in_first_unsuccessful_stage do |build|
|
|
|
|
Ci::RetryBuildService.new(project, current_user)
|
2017-02-14 09:38:17 +00:00
|
|
|
.retry(build)
|
2017-02-13 15:38:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-02-14 11:20:02 +00:00
|
|
|
def retryable_builds_in_subsequent_stages
|
|
|
|
relation = @pipeline.builds
|
|
|
|
.after_stage(resume_stage.index)
|
|
|
|
.failed_or_canceled
|
|
|
|
|
|
|
|
each_retryable_build_with_locking(relation) do |build|
|
|
|
|
yield build
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def retryable_builds_in_first_unsuccessful_stage
|
|
|
|
relation = resume_stage.builds.failed_or_canceled
|
|
|
|
|
|
|
|
each_retryable_build_with_locking(relation) do |build|
|
|
|
|
yield build
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_retryable_build_with_locking(relation)
|
|
|
|
Gitlab::OptimisticLocking.retry_lock(relation) do |builds|
|
|
|
|
builds.find_each do |build|
|
|
|
|
next unless build.retryable?
|
|
|
|
yield build
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-13 15:38:08 +00:00
|
|
|
def resume_stage
|
|
|
|
@resume_stage ||= @pipeline.stages.find do |stage|
|
|
|
|
stage.failed? || stage.canceled?
|
2017-02-13 14:14:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|