2018-06-27 03:23:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-07 18:35:56 -04:00
|
|
|
class PipelineScheduleWorker
|
2017-11-28 11:08:30 -05:00
|
|
|
include ApplicationWorker
|
2017-05-07 18:35:56 -04:00
|
|
|
include CronjobQueue
|
2019-04-27 23:27:08 -04:00
|
|
|
include ::Gitlab::ExclusiveLeaseHelpers
|
|
|
|
|
|
|
|
EXCLUSIVE_LOCK_KEY = 'pipeline_schedules:run:lock'
|
|
|
|
LOCK_TIMEOUT = 50.minutes
|
2017-05-07 18:35:56 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-07 18:35:56 -04:00
|
|
|
def perform
|
2019-04-27 23:27:08 -04:00
|
|
|
in_lock(EXCLUSIVE_LOCK_KEY, ttl: LOCK_TIMEOUT, retries: 1) do
|
|
|
|
Ci::PipelineSchedule.active.where("next_run_at < ?", Time.now)
|
|
|
|
.preload(:owner, :project).find_each do |schedule|
|
|
|
|
|
|
|
|
schedule.schedule_next_run!
|
|
|
|
|
|
|
|
Ci::CreatePipelineService.new(schedule.project,
|
|
|
|
schedule.owner,
|
|
|
|
ref: schedule.ref)
|
|
|
|
.execute!(:schedule, ignore_skip_ci: true, save_on_errors: true, schedule: schedule)
|
|
|
|
rescue => e
|
|
|
|
error(schedule, e)
|
|
|
|
end
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-11-30 02:32:30 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def error(schedule, error)
|
|
|
|
failed_creation_counter.increment
|
|
|
|
|
|
|
|
Rails.logger.error "Failed to create a scheduled pipeline. " \
|
|
|
|
"schedule_id: #{schedule.id} message: #{error.message}"
|
|
|
|
|
|
|
|
Gitlab::Sentry
|
|
|
|
.track_exception(error,
|
|
|
|
issue_url: 'https://gitlab.com/gitlab-org/gitlab-ce/issues/41231',
|
|
|
|
extra: { schedule_id: schedule.id })
|
|
|
|
end
|
|
|
|
|
|
|
|
def failed_creation_counter
|
|
|
|
@failed_creation_counter ||=
|
|
|
|
Gitlab::Metrics.counter(:pipeline_schedule_creation_failed_total,
|
|
|
|
"Counter of failed attempts of pipeline schedule creation")
|
|
|
|
end
|
2017-05-07 18:35:56 -04:00
|
|
|
end
|