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
|
|
|
|
|
|
|
|
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-03-29 14:33:23 -04:00
|
|
|
validates :trigger, presence: true
|
2017-03-31 13:02:26 -04:00
|
|
|
validates :cron, cron: true, presence: true
|
2017-03-29 14:33:23 -04:00
|
|
|
validates :cron_time_zone, presence: true
|
2017-03-31 13:02:26 -04:00
|
|
|
validates :ref, ref: true, presence: true
|
|
|
|
validate :check_cron_frequency
|
2017-03-29 14:33:23 -04:00
|
|
|
|
|
|
|
after_create :schedule_next_run!
|
|
|
|
|
2017-03-22 14:54:49 -04:00
|
|
|
def schedule_next_run!
|
2017-03-31 06:08:39 -04:00
|
|
|
next_time = Ci::CronParser.new(cron, cron_time_zone).next_time_from(Time.now)
|
|
|
|
|
|
|
|
if next_time.present? && !less_than_1_hour_from_now?(next_time)
|
2017-03-29 14:33:23 -04:00
|
|
|
update!(next_run_at: next_time)
|
2017-03-23 11:18:13 -04:00
|
|
|
end
|
2017-03-22 14:54:49 -04:00
|
|
|
end
|
|
|
|
|
2017-03-29 14:33:23 -04:00
|
|
|
private
|
|
|
|
|
2017-03-31 06:08:39 -04:00
|
|
|
def less_than_1_hour_from_now?(time)
|
|
|
|
((time - Time.now).abs < 1.hour) ? true : false
|
|
|
|
end
|
|
|
|
|
2017-03-31 13:02:26 -04:00
|
|
|
def check_cron_frequency
|
|
|
|
next_time = Ci::CronParser.new(cron, cron_time_zone).next_time_from(Time.now)
|
2017-03-29 14:33:23 -04:00
|
|
|
|
2017-03-31 13:02:26 -04:00
|
|
|
if less_than_1_hour_from_now?(next_time)
|
2017-03-29 14:33:23 -04:00
|
|
|
self.errors.add(:cron, " can not be less than 1 hour")
|
|
|
|
end
|
|
|
|
end
|
2017-03-22 14:54:49 -04:00
|
|
|
end
|
|
|
|
end
|