debian-mirror-gitlab/app/services/ci/pipelines/add_job_service.rb

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

54 lines
1.5 KiB
Ruby
Raw Normal View History

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-10-11 01:57:18 +05:30
job.partition_id = pipeline.partition_id
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
2022-10-11 01:57:18 +05:30
job.metadata.partition_id = pipeline.partition_id
2022-03-02 08:16:31 +05:30
end
2021-09-30 23:02:18 +05:30
end
end
end
end