2018-11-08 19:23:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
class RunPipelineScheduleWorker # rubocop:disable Scalability/IdempotentWorker
|
2018-03-17 18:26:18 +05:30
|
|
|
include ApplicationWorker
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
data_consistency :always
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
sidekiq_options retry: 3
|
2018-03-17 18:26:18 +05:30
|
|
|
include PipelineQueue
|
|
|
|
|
|
|
|
queue_namespace :pipeline_creation
|
2019-12-21 20:55:43 +05:30
|
|
|
feature_category :continuous_integration
|
2023-03-04 22:38:38 +05:30
|
|
|
deduplicate :until_executed
|
|
|
|
idempotent!
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def perform(schedule_id, user_id, options = {})
|
2021-12-11 22:18:48 +05:30
|
|
|
schedule = Ci::PipelineSchedule.find_by_id(schedule_id)
|
|
|
|
user = User.find_by_id(user_id)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
return unless schedule && schedule.project && user
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
options.symbolize_keys!
|
|
|
|
|
|
|
|
if options[:scheduling]
|
2023-03-17 16:20:25 +05:30
|
|
|
return if schedule.next_run_at > Time.current
|
|
|
|
|
|
|
|
update_next_run_at_for(schedule)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
run_pipeline_schedule(schedule, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_pipeline_schedule(schedule, user)
|
2023-01-13 00:05:48 +05:30
|
|
|
response = Ci::CreatePipelineService
|
|
|
|
.new(schedule.project, user, ref: schedule.ref)
|
|
|
|
.execute(:schedule, ignore_skip_ci: true, save_on_errors: false, schedule: schedule)
|
|
|
|
|
|
|
|
return response if response.payload.persisted?
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
# This is a user operation error such as corrupted .gitlab-ci.yml. Log the error for debugging purpose.
|
2023-01-13 00:05:48 +05:30
|
|
|
log_extra_metadata_on_done(:pipeline_creation_error, response.message)
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError => e
|
2019-09-04 21:01:54 +05:30
|
|
|
error(schedule, e)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def update_next_run_at_for(schedule)
|
|
|
|
# Ensure `next_run_at` is set properly before creating a pipeline.
|
|
|
|
# Otherwise, multiple pipelines could be created in a short interval.
|
|
|
|
schedule.schedule_next_run!
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def error(schedule, error)
|
|
|
|
failed_creation_counter.increment
|
2021-11-18 22:05:49 +05:30
|
|
|
log_error(schedule, error)
|
|
|
|
track_error(schedule, error)
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
def log_error(schedule, error)
|
2020-11-24 15:15:51 +05:30
|
|
|
Gitlab::AppLogger.error "Failed to create a scheduled pipeline. " \
|
2019-09-04 21:01:54 +05:30
|
|
|
"schedule_id: #{schedule.id} message: #{error.message}"
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
def track_error(schedule, error)
|
2023-06-20 00:43:36 +05:30
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(
|
|
|
|
error,
|
|
|
|
issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/41231',
|
|
|
|
schedule_id: schedule.id
|
|
|
|
)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def failed_creation_counter
|
2023-06-20 00:43:36 +05:30
|
|
|
@failed_creation_counter ||= Gitlab::Metrics.counter(
|
|
|
|
:pipeline_schedule_creation_failed_total,
|
|
|
|
"Counter of failed attempts of pipeline schedule creation"
|
|
|
|
)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|