BigW Consortium Gitlab

pipeline_schedule.rb 1.64 KB
Newer Older
1
module Ci
2
  class PipelineSchedule < ActiveRecord::Base
3
    extend 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

Kamil Trzcinski committed
13 14 15
    validates :cron, unless: :importing_or_inactive?, cron: true, presence: { unless: :importing_or_inactive? }
    validates :cron_timezone, cron_timezone: true, presence: { unless: :importing_or_inactive? }
    validates :ref, presence: { unless: :importing_or_inactive? }
16
    validates :description, presence: true
17

18 19
    before_save :set_next_run_at

Kamil Trzcinski committed
20
    scope :active, -> { where(active: true) }
21 22 23 24 25 26 27 28 29
    scope :inactive, -> { where(active: false) }

    def owned_by?(current_user)
      owner == current_user
    end

    def inactive?
      !active?
    end
Kamil Trzcinski committed
30

31 32 33 34
    def deactivate!
      update_attribute(:active, false)
    end

Kamil Trzcinski committed
35
    def importing_or_inactive?
36
      importing? || inactive?
37 38
    end

39 40 41 42
    def runnable_by_owner?
      Ability.allowed?(owner, :create_pipeline, project)
    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
59 60
  end
end