2021-07-08 11:10:06 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
module Pipelines
|
|
|
|
class AddJobService
|
2021-08-11 14:10:52 -04:00
|
|
|
include ::Gitlab::ExclusiveLeaseHelpers
|
|
|
|
|
2021-07-08 11:10:06 -04:00
|
|
|
attr_reader :pipeline
|
|
|
|
|
|
|
|
def initialize(pipeline)
|
|
|
|
@pipeline = pipeline
|
|
|
|
|
2021-08-11 14:10:52 -04:00
|
|
|
raise ArgumentError, "Pipeline must be persisted for this service to be used" unless pipeline.persisted?
|
2021-07-08 11:10:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute!(job, &block)
|
|
|
|
assign_pipeline_attributes(job)
|
|
|
|
|
2021-10-05 14:13:27 -04:00
|
|
|
in_lock("ci:pipelines:#{pipeline.id}:add-job", ttl: LOCK_TIMEOUT, sleep_sec: LOCK_SLEEP, retries: LOCK_RETRIES) do
|
2021-08-11 14:10:52 -04:00
|
|
|
Ci::Pipeline.transaction do
|
|
|
|
yield(job)
|
2021-07-08 11:10:06 -04:00
|
|
|
|
2021-09-06 08:11:14 -04:00
|
|
|
job.update_older_statuses_retried!
|
2021-08-11 14:10:52 -04:00
|
|
|
end
|
2021-07-08 11:10:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
ServiceResponse.success(payload: { job: job })
|
|
|
|
rescue StandardError => e
|
|
|
|
ServiceResponse.error(message: e.message, payload: { job: job })
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-08-11 14:10:52 -04:00
|
|
|
LOCK_TIMEOUT = 1.minute
|
|
|
|
LOCK_SLEEP = 0.5.seconds
|
|
|
|
LOCK_RETRIES = 20
|
|
|
|
|
2021-07-08 11:10:06 -04:00
|
|
|
def assign_pipeline_attributes(job)
|
2021-08-11 14:10:52 -04:00
|
|
|
job.pipeline = pipeline
|
|
|
|
job.project = pipeline.project
|
|
|
|
job.ref = pipeline.ref
|
2021-07-08 11:10:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|