2017-03-22 14:54:49 -04:00
|
|
|
module Ci
|
2017-03-29 07:49:47 -04:00
|
|
|
class TriggerSchedule < ActiveRecord::Base
|
2017-03-22 14:54:49 -04:00
|
|
|
extend Ci::Model
|
2017-04-04 03:55:14 -04:00
|
|
|
include Importable
|
2017-03-22 14:54:49 -04:00
|
|
|
|
|
|
|
acts_as_paranoid
|
|
|
|
|
|
|
|
belongs_to :project
|
2017-03-29 07:49:47 -04:00
|
|
|
belongs_to :trigger
|
2017-03-22 14:54:49 -04:00
|
|
|
|
2017-03-30 07:59:20 -04:00
|
|
|
delegate :ref, to: :trigger
|
|
|
|
|
2017-04-04 03:55:14 -04:00
|
|
|
validates :trigger, presence: { unless: :importing? }
|
|
|
|
validates :cron, cron: true, presence: { unless: :importing? }
|
2017-04-04 12:54:45 -04:00
|
|
|
validates :cron_timezone, cron_timezone: true, presence: { unless: :importing? }
|
2017-04-04 03:55:14 -04:00
|
|
|
validates :ref, presence: { unless: :importing? }
|
2017-03-29 14:33:23 -04:00
|
|
|
|
2017-04-06 05:52:48 -04:00
|
|
|
before_save :set_next_run_at
|
|
|
|
|
|
|
|
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 14:33:23 -04:00
|
|
|
|
2017-03-22 14:54:49 -04:00
|
|
|
def schedule_next_run!
|
2017-04-06 05:52:48 -04:00
|
|
|
save! # with set_next_run_at
|
2017-04-06 08:01:30 -04:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
2017-04-06 05:52:48 -04:00
|
|
|
update_attribute(:next_run_at, nil) # update without validation
|
2017-03-22 14:54:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|