pipeline_schedule_worker.rb 1.33 KB
Newer Older
1 2
# frozen_string_literal: true

3
class PipelineScheduleWorker
4
  include ApplicationWorker
5 6
  include CronjobQueue

7
  # rubocop: disable CodeReuse/ActiveRecord
8
  def perform
9 10
    Ci::PipelineSchedule.active.where("next_run_at < ?", Time.now)
      .preload(:owner, :project).find_each do |schedule|
Nick Thomas's avatar
Nick Thomas committed
11 12 13 14 15 16 17 18 19

      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)
    ensure
      schedule.schedule_next_run!
20 21
    end
  end
22
  # rubocop: enable CodeReuse/ActiveRecord
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

  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
43
end