2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Ci
|
2019-07-07 11:18:12 +05:30
|
|
|
class PipelineSchedule < ApplicationRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
extend Gitlab::Ci::Model
|
2017-08-17 22:00:37 +05:30
|
|
|
include Importable
|
2019-07-31 22:56:46 +05:30
|
|
|
include StripAttribute
|
2020-03-13 15:44:24 +05:30
|
|
|
include Schedulable
|
2020-05-24 23:13:21 +05:30
|
|
|
include Limitable
|
|
|
|
|
|
|
|
self.limit_name = 'ci_pipeline_schedules'
|
|
|
|
self.limit_scope = :project
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
belongs_to :project
|
|
|
|
belongs_to :owner, class_name: 'User'
|
|
|
|
has_one :last_pipeline, -> { order(id: :desc) }, class_name: 'Ci::Pipeline'
|
|
|
|
has_many :pipelines
|
2018-03-17 18:26:18 +05:30
|
|
|
has_many :variables, class_name: 'Ci::PipelineScheduleVariable', validate: false
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
validates :cron, unless: :importing?, cron: true, presence: { unless: :importing? }
|
|
|
|
validates :cron_timezone, cron_timezone: true, presence: { unless: :importing? }
|
|
|
|
validates :ref, presence: { unless: :importing? }
|
|
|
|
validates :description, presence: true
|
2021-03-11 19:13:27 +05:30
|
|
|
validates :variables, nested_attributes_duplicates: true
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
strip_attributes :cron
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
scope :active, -> { where(active: true) }
|
|
|
|
scope :inactive, -> { where(active: false) }
|
2020-03-13 15:44:24 +05:30
|
|
|
scope :preloaded, -> { preload(:owner, project: [:route]) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
accepts_nested_attributes_for :variables, allow_destroy: true
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
alias_attribute :real_next_run, :next_run_at
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def owned_by?(current_user)
|
|
|
|
owner == current_user
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def own!(user)
|
|
|
|
update(owner: user)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def inactive?
|
|
|
|
!active?
|
|
|
|
end
|
|
|
|
|
|
|
|
def deactivate!
|
|
|
|
update_attribute(:active, false)
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
##
|
|
|
|
# The `next_run_at` column is set to the actual execution date of `PipelineScheduleWorker`.
|
|
|
|
# This way, a schedule like `*/1 * * * *` won't be triggered in a short interval
|
|
|
|
# when PipelineScheduleWorker runs irregularly by Sidekiq Memory Killer.
|
2017-08-17 22:00:37 +05:30
|
|
|
def set_next_run_at
|
2019-09-30 21:07:59 +05:30
|
|
|
now = Time.zone.now
|
|
|
|
ideal_next_run = ideal_next_run_from(now)
|
|
|
|
|
|
|
|
self.next_run_at = if ideal_next_run == cron_worker_next_run_from(now)
|
|
|
|
ideal_next_run
|
|
|
|
else
|
|
|
|
cron_worker_next_run_from(ideal_next_run)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def job_variables
|
|
|
|
variables&.map(&:to_runner_variable) || []
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
def ideal_next_run_from(start_time)
|
2019-09-04 21:01:54 +05:30
|
|
|
Gitlab::Ci::CronParser.new(cron, cron_timezone)
|
2019-09-30 21:07:59 +05:30
|
|
|
.next_time_from(start_time)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cron_worker_next_run_from(start_time)
|
|
|
|
Gitlab::Ci::CronParser.new(Settings.cron_jobs['pipeline_schedule_worker']['cron'],
|
|
|
|
Time.zone.name)
|
|
|
|
.next_time_from(start_time)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
Ci::PipelineSchedule.prepend_if_ee('EE::Ci::PipelineSchedule')
|