2018-08-03 03:15:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-22 14:54:49 -04:00
|
|
|
module Ci
|
2021-08-03 02:08:50 -04:00
|
|
|
class PipelineSchedule < Ci::ApplicationRecord
|
2021-06-11 14:10:13 -04:00
|
|
|
extend ::Gitlab::Utils::Override
|
2017-04-04 03:55:14 -04:00
|
|
|
include Importable
|
2019-04-30 08:10:39 -04:00
|
|
|
include StripAttribute
|
2021-05-13 11:10:20 -04:00
|
|
|
include CronSchedulable
|
2020-04-22 11:09:27 -04:00
|
|
|
include Limitable
|
2021-03-16 14:11:53 -04:00
|
|
|
include EachBatch
|
2020-04-22 11:09:27 -04:00
|
|
|
|
|
|
|
self.limit_name = 'ci_pipeline_schedules'
|
|
|
|
self.limit_scope = :project
|
2017-03-22 14:54:49 -04:00
|
|
|
|
|
|
|
belongs_to :project
|
2017-05-07 18:35:56 -04:00
|
|
|
belongs_to :owner, class_name: 'User'
|
|
|
|
has_one :last_pipeline, -> { order(id: :desc) }, class_name: 'Ci::Pipeline'
|
|
|
|
has_many :pipelines
|
2017-08-17 12:43:17 -04:00
|
|
|
has_many :variables, class_name: 'Ci::PipelineScheduleVariable', validate: false
|
2017-03-30 07:59:20 -04:00
|
|
|
|
2017-05-26 05:00:42 -04:00
|
|
|
validates :cron, unless: :importing?, cron: true, presence: { unless: :importing? }
|
|
|
|
validates :cron_timezone, cron_timezone: true, presence: { unless: :importing? }
|
|
|
|
validates :ref, presence: { unless: :importing? }
|
2017-05-07 18:35:56 -04:00
|
|
|
validates :description, presence: true
|
2021-01-26 10:08:58 -05:00
|
|
|
validates :variables, nested_attributes_duplicates: true
|
2017-03-29 14:33:23 -04:00
|
|
|
|
2021-08-16 20:10:22 -04:00
|
|
|
strip_attributes! :cron
|
2019-04-30 08:10:39 -04:00
|
|
|
|
2017-04-07 08:47:29 -04:00
|
|
|
scope :active, -> { where(active: true) }
|
2017-05-07 18:35:56 -04:00
|
|
|
scope :inactive, -> { where(active: false) }
|
2020-02-11 19:09:00 -05:00
|
|
|
scope :preloaded, -> { preload(:owner, project: [:route]) }
|
2021-03-16 14:11:53 -04:00
|
|
|
scope :owned_by, ->(user) { where(owner: user) }
|
2017-05-07 18:35:56 -04:00
|
|
|
|
2017-06-22 14:57:13 -04:00
|
|
|
accepts_nested_attributes_for :variables, allow_destroy: true
|
|
|
|
|
2019-05-17 08:10:44 -04:00
|
|
|
alias_attribute :real_next_run, :next_run_at
|
|
|
|
|
2017-05-07 18:35:56 -04:00
|
|
|
def owned_by?(current_user)
|
|
|
|
owner == current_user
|
|
|
|
end
|
|
|
|
|
2017-05-12 14:37:09 -04:00
|
|
|
def own!(user)
|
2017-05-16 10:58:22 -04:00
|
|
|
update(owner: user)
|
2017-05-12 14:37:09 -04:00
|
|
|
end
|
|
|
|
|
2017-05-07 18:35:56 -04:00
|
|
|
def inactive?
|
|
|
|
!active?
|
|
|
|
end
|
2017-04-07 08:47:29 -04:00
|
|
|
|
2017-05-10 04:04:25 -04:00
|
|
|
def deactivate!
|
|
|
|
update_attribute(:active, false)
|
|
|
|
end
|
|
|
|
|
2017-06-27 03:55:27 -04:00
|
|
|
def job_variables
|
|
|
|
variables&.map(&:to_runner_variable) || []
|
|
|
|
end
|
2019-05-17 08:10:44 -04:00
|
|
|
|
2021-06-11 14:10:13 -04:00
|
|
|
override :set_next_run_at
|
|
|
|
def set_next_run_at
|
|
|
|
self.next_run_at = ::Ci::PipelineSchedules::CalculateNextRunService # rubocop: disable CodeReuse/ServiceClass
|
|
|
|
.new(project)
|
|
|
|
.execute(self, fallback_method: method(:calculate_next_run_at))
|
|
|
|
end
|
|
|
|
|
2021-06-14 11:09:48 -04:00
|
|
|
def daily_limit
|
|
|
|
project.actual_limits.limit_for(:ci_daily_pipeline_schedule_triggers)
|
|
|
|
end
|
|
|
|
|
2019-05-17 08:10:44 -04:00
|
|
|
private
|
|
|
|
|
2021-05-13 11:10:20 -04:00
|
|
|
def worker_cron_expression
|
|
|
|
Settings.cron_jobs['pipeline_schedule_worker']['cron']
|
2019-05-17 08:10:44 -04:00
|
|
|
end
|
2017-03-22 14:54:49 -04:00
|
|
|
end
|
|
|
|
end
|
2019-10-02 20:05:59 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Ci::PipelineSchedule.prepend_mod_with('Ci::PipelineSchedule')
|