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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
2.7 KiB
Ruby
Raw Normal View History

2022-06-21 17:19:12 +05:30
# frozen_string_literal: true
module Ci
class RetryJobService < ::BaseService
include Gitlab::Utils::StrongMemoize
2022-08-27 11:52:29 +05:30
def execute(job, variables: [])
2022-06-21 17:19:12 +05:30
if job.retryable?
job.ensure_scheduling_type!
2022-08-27 11:52:29 +05:30
new_job = retry_job(job, variables: variables)
2022-06-21 17:19:12 +05:30
ServiceResponse.success(payload: { job: new_job })
else
ServiceResponse.error(
message: 'Job cannot be retried',
payload: { job: job, reason: :not_retryable }
)
end
end
# rubocop: disable CodeReuse/ActiveRecord
2023-01-13 00:05:48 +05:30
def clone!(job, variables: [], enqueue_if_actionable: false)
2022-06-21 17:19:12 +05:30
# Cloning a job requires a strict type check to ensure
# the attributes being used for the clone are taken straight
# from the model and not overridden by other abstractions.
2022-07-16 23:28:13 +05:30
raise TypeError unless job.instance_of?(Ci::Build) || job.instance_of?(Ci::Bridge)
2022-06-21 17:19:12 +05:30
check_access!(job)
2022-08-27 11:52:29 +05:30
new_job = job.clone(current_user: current_user, new_job_variables_attributes: variables)
2023-01-13 00:05:48 +05:30
if Feature.enabled?(:ci_retry_job_fix, project) && enqueue_if_actionable && new_job.action?
new_job.set_enqueue_immediately!
end
2022-06-21 17:19:12 +05:30
new_job.run_after_commit do
::Ci::CopyCrossDatabaseAssociationsService.new.execute(job, new_job)
::Deployments::CreateForBuildService.new.execute(new_job)
::MergeRequests::AddTodoWhenBuildFailsService
.new(project: project)
.close(new_job)
end
::Ci::Pipelines::AddJobService.new(job.pipeline).execute!(new_job) do |processable|
BulkInsertableAssociations.with_bulk_insert do
processable.save!
end
end
job.reset # refresh the data to get new values of `retried` and `processed`.
new_job
end
# rubocop: enable CodeReuse/ActiveRecord
private
2022-07-16 23:28:13 +05:30
def check_assignable_runners!(job); end
2022-08-27 11:52:29 +05:30
def retry_job(job, variables: [])
2023-01-13 00:05:48 +05:30
clone!(job, variables: variables, enqueue_if_actionable: true).tap do |new_job|
2022-07-16 23:28:13 +05:30
check_assignable_runners!(new_job) if new_job.is_a?(Ci::Build)
2022-06-21 17:19:12 +05:30
next if new_job.failed?
2023-01-13 00:05:48 +05:30
Gitlab::OptimisticLocking.retry_lock(new_job, name: 'retry_build', &:enqueue) if Feature.disabled?(
:ci_retry_job_fix, project)
2022-06-21 17:19:12 +05:30
AfterRequeueJobService.new(project, current_user).execute(job)
2023-01-13 00:05:48 +05:30
if Feature.enabled?(:ci_retry_job_fix, project)
Ci::PipelineCreation::StartPipelineService.new(job.pipeline).execute
new_job.reset
end
2022-06-21 17:19:12 +05:30
end
end
def check_access!(job)
unless can?(current_user, :update_build, job)
raise Gitlab::Access::AccessDeniedError, '403 Forbidden'
end
end
end
end
Ci::RetryJobService.prepend_mod_with('Ci::RetryJobService')