debian-mirror-gitlab/app/models/ci/pipeline_schedule.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
2.7 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Ci
2021-10-27 15:23:28 +05:30
class PipelineSchedule < Ci::ApplicationRecord
2021-09-04 01:27:46 +05:30
extend ::Gitlab::Utils::Override
2017-08-17 22:00:37 +05:30
include Importable
2019-07-31 22:56:46 +05:30
include StripAttribute
2021-06-08 01:23:25 +05:30
include CronSchedulable
2020-05-24 23:13:21 +05:30
include Limitable
2021-04-29 21:17:54 +05:30
include EachBatch
2023-05-27 22:25:52 +05:30
include BatchNullifyDependentAssociations
2020-05-24 23:13:21 +05:30
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'
2023-05-27 22:25:52 +05:30
has_one :last_pipeline, -> { order(id: :desc) }, class_name: 'Ci::Pipeline', inverse_of: :pipeline_schedule
has_many :pipelines, dependent: :nullify # rubocop:disable Cop/ActiveRecordDependent
2023-03-04 22:38:38 +05:30
has_many :variables, class_name: 'Ci::PipelineScheduleVariable'
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
2021-10-27 15:23:28 +05:30
strip_attributes! :cron
2019-07-31 22:56:46 +05:30
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]) }
2021-04-29 21:17:54 +05:30
scope :owned_by, ->(user) { where(owner: user) }
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
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
2021-09-04 01:27:46 +05:30
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
def daily_limit
project.actual_limits.limit_for(:ci_daily_pipeline_schedule_triggers)
end
2022-05-07 20:08:51 +05:30
def ref_for_display
return unless ref.present?
ref.gsub(%r{^refs/(heads|tags)/}, '')
end
def for_tag?
return false unless ref.present?
ref.start_with? 'refs/tags/'
end
2021-06-08 01:23:25 +05:30
def worker_cron_expression
Settings.cron_jobs['pipeline_schedule_worker']['cron']
2019-09-04 21:01:54 +05:30
end
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
# Using destroy instead of before_destroy as we want nullify_dependent_associations_in_batches
# to run first and not in a transaction block. This prevents timeouts for schedules with numerous pipelines
2023-05-27 22:25:52 +05:30
def destroy
nullify_dependent_associations_in_batches
super
end
2017-08-17 22:00:37 +05:30
end
end
2019-12-21 20:55:43 +05:30
2021-06-08 01:23:25 +05:30
Ci::PipelineSchedule.prepend_mod_with('Ci::PipelineSchedule')