2021-09-30 23:02:18 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
module Pipelines
|
|
|
|
class AddJobService
|
2021-10-27 15:23:28 +05:30
|
|
|
include ::Gitlab::ExclusiveLeaseHelpers
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
attr_reader :pipeline
|
|
|
|
|
|
|
|
def initialize(pipeline)
|
|
|
|
@pipeline = pipeline
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
raise ArgumentError, "Pipeline must be persisted for this service to be used" unless pipeline.persisted?
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute!(job, &block)
|
|
|
|
assign_pipeline_attributes(job)
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
in_lock("ci:pipelines:#{pipeline.id}:add-job", ttl: LOCK_TIMEOUT, sleep_sec: LOCK_SLEEP, retries: LOCK_RETRIES) do
|
2021-10-27 15:23:28 +05:30
|
|
|
Ci::Pipeline.transaction do
|
|
|
|
yield(job)
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
job.update_older_statuses_retried!
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
ServiceResponse.success(payload: { job: job })
|
|
|
|
rescue StandardError => e
|
|
|
|
ServiceResponse.error(message: e.message, payload: { job: job })
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
LOCK_TIMEOUT = 1.minute
|
|
|
|
LOCK_SLEEP = 0.5.seconds
|
|
|
|
LOCK_RETRIES = 20
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def assign_pipeline_attributes(job)
|
2021-10-27 15:23:28 +05:30
|
|
|
job.pipeline = pipeline
|
|
|
|
job.project = pipeline.project
|
|
|
|
job.ref = pipeline.ref
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
# update metadata since it might have been lazily initialised before this call
|
|
|
|
# metadata is present on `Ci::Processable`
|
|
|
|
if job.respond_to?(:metadata) && job.metadata
|
|
|
|
job.metadata.project = pipeline.project
|
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|