2017-03-22 18:54:49 +00:00
|
|
|
module Ci
|
2017-05-07 22:35:56 +00:00
|
|
|
class PipelineSchedule < ActiveRecord::Base
|
2017-03-22 18:54:49 +00:00
|
|
|
extend Ci::Model
|
2017-04-04 07:55:14 +00:00
|
|
|
include Importable
|
2017-03-22 18:54:49 +00:00
|
|
|
|
|
|
|
acts_as_paranoid
|
|
|
|
|
|
|
|
belongs_to :project
|
2017-05-07 22:35:56 +00:00
|
|
|
belongs_to :owner, class_name: 'User'
|
|
|
|
has_one :last_pipeline, -> { order(id: :desc) }, class_name: 'Ci::Pipeline'
|
|
|
|
has_many :pipelines
|
2017-03-30 11:59:20 +00:00
|
|
|
|
2017-05-26 09:00:42 +00:00
|
|
|
validates :cron, unless: :importing?, cron: true, presence: { unless: :importing? }
|
|
|
|
validates :cron_timezone, cron_timezone: true, presence: { unless: :importing? }
|
|
|
|
validates :ref, presence: { unless: :importing? }
|
2017-05-07 22:35:56 +00:00
|
|
|
validates :description, presence: true
|
2017-03-29 18:33:23 +00:00
|
|
|
|
2017-04-06 09:52:48 +00:00
|
|
|
before_save :set_next_run_at
|
|
|
|
|
2017-04-07 12:47:29 +00:00
|
|
|
scope :active, -> { where(active: true) }
|
2017-05-07 22:35:56 +00:00
|
|
|
scope :inactive, -> { where(active: false) }
|
|
|
|
|
|
|
|
def owned_by?(current_user)
|
|
|
|
owner == current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def inactive?
|
|
|
|
!active?
|
|
|
|
end
|
2017-04-07 12:47:29 +00:00
|
|
|
|
2017-05-10 08:04:25 +00:00
|
|
|
def deactivate!
|
|
|
|
update_attribute(:active, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def runnable_by_owner?
|
|
|
|
Ability.allowed?(owner, :create_pipeline, project)
|
|
|
|
end
|
|
|
|
|
2017-04-06 09:52:48 +00:00
|
|
|
def set_next_run_at
|
|
|
|
self.next_run_at = Gitlab::Ci::CronParser.new(cron, cron_timezone).next_time_from(Time.now)
|
|
|
|
end
|
2017-03-29 18:33:23 +00:00
|
|
|
|
2017-05-11 19:12:04 +00:00
|
|
|
def last_pipeline
|
|
|
|
self.pipelines&.last
|
|
|
|
end
|
|
|
|
|
2017-03-22 18:54:49 +00:00
|
|
|
def schedule_next_run!
|
2017-04-06 09:52:48 +00:00
|
|
|
save! # with set_next_run_at
|
2017-04-06 12:01:30 +00:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
2017-04-06 09:52:48 +00:00
|
|
|
update_attribute(:next_run_at, nil) # update without validation
|
2017-03-22 18:54:49 +00:00
|
|
|
end
|
2017-04-06 19:02:19 +00:00
|
|
|
|
2017-04-07 15:15:28 +00:00
|
|
|
def real_next_run(
|
2017-05-07 22:35:56 +00:00
|
|
|
worker_cron: Settings.cron_jobs['pipeline_schedule_worker']['cron'],
|
2017-04-07 15:15:28 +00:00
|
|
|
worker_time_zone: Time.zone.name)
|
2017-04-06 19:02:19 +00:00
|
|
|
Gitlab::Ci::CronParser.new(worker_cron, worker_time_zone)
|
|
|
|
.next_time_from(next_run_at)
|
|
|
|
end
|
2017-03-22 18:54:49 +00:00
|
|
|
end
|
|
|
|
end
|