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

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-10-27 15:23:28 +05:30
if Feature.enabled?(:ci_pipeline_add_job_with_lock, pipeline.project, default_enabled: :yaml)
in_lock("ci:pipelines:#{pipeline.id}:add-job", ttl: LOCK_TIMEOUT, sleep_sec: LOCK_SLEEP, retries: LOCK_RETRIES) do
Ci::Pipeline.transaction do
yield(job)
job.update_older_statuses_retried! if Feature.enabled?(:ci_fix_commit_status_retried, pipeline.project, default_enabled: :yaml)
end
end
else
Ci::Pipeline.transaction do
yield(job)
2021-09-30 23:02:18 +05:30
2021-10-27 15:23:28 +05:30
job.update_older_statuses_retried! if Feature.enabled?(:ci_fix_commit_status_retried, pipeline.project, default_enabled: :yaml)
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
2021-09-30 23:02:18 +05:30
end
end
end
end