BigW Consortium Gitlab

pipeline_schedule.rb 1.58 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

13 14 15
    validates :cron, unless: :importing?, cron: true, presence: { unless: :importing? }
    validates :cron_timezone, cron_timezone: true, presence: { unless: :importing? }
    validates :ref, presence: { unless: :importing? }
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
    scope :inactive, -> { where(active: false) }

    def owned_by?(current_user)
      owner == current_user
    end

27
    def own!(user)
28
      update(owner: user)
29 30
    end

31 32 33
    def inactive?
      !active?
    end
Kamil Trzcinski committed
34

35 36 37 38 39 40 41 42
    def deactivate!
      update_attribute(:active, false)
    end

    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