BigW Consortium Gitlab

pipeline_schedule.rb 1.77 KB
Newer Older
1
module Ci
2
  class PipelineSchedule < ActiveRecord::Base
3
    extend Gitlab::Ci::Model
4
    include Importable
5 6 7 8

    acts_as_paranoid

    belongs_to :project
9 10 11
    belongs_to :owner, class_name: 'User'
    has_one :last_pipeline, -> { order(id: :desc) }, class_name: 'Ci::Pipeline'
    has_many :pipelines
12
    has_many :variables, class_name: 'Ci::PipelineScheduleVariable', validate: false
13

14 15 16
    validates :cron, unless: :importing?, cron: true, presence: { unless: :importing? }
    validates :cron_timezone, cron_timezone: true, presence: { unless: :importing? }
    validates :ref, presence: { unless: :importing? }
17
    validates :description, presence: true
18
    validates :variables, variable_duplicates: true
19

20 21
    before_save :set_next_run_at

Kamil Trzcinski committed
22
    scope :active, -> { where(active: true) }
23 24
    scope :inactive, -> { where(active: false) }

25 26
    accepts_nested_attributes_for :variables, allow_destroy: true

27 28 29 30
    def owned_by?(current_user)
      owner == current_user
    end

31
    def own!(user)
32
      update(owner: user)
33 34
    end

35 36 37
    def inactive?
      !active?
    end
Kamil Trzcinski committed
38

39 40 41 42
    def deactivate!
      update_attribute(:active, false)
    end

43 44 45
    def set_next_run_at
      self.next_run_at = Gitlab::Ci::CronParser.new(cron, cron_timezone).next_time_from(Time.now)
    end
46

47
    def schedule_next_run!
48
      save! # with set_next_run_at
Shinya Maeda committed
49
    rescue ActiveRecord::RecordInvalid
50
      update_attribute(:next_run_at, nil) # update without validation
51
    end
52

Kamil Trzcinski committed
53
    def real_next_run(
54
        worker_cron: Settings.cron_jobs['pipeline_schedule_worker']['cron'],
Kamil Trzcinski committed
55
        worker_time_zone: Time.zone.name)
56 57 58
      Gitlab::Ci::CronParser.new(worker_cron, worker_time_zone)
                            .next_time_from(next_run_at)
    end
Shinya Maeda committed
59 60 61 62

    def job_variables
      variables&.map(&:to_runner_variable) || []
    end
63 64
  end
end