gitlab-org--gitlab-foss/app/models/ci/trigger_schedule.rb

43 lines
1.0 KiB
Ruby
Raw Normal View History

module Ci
class TriggerSchedule < ActiveRecord::Base
extend Ci::Model
acts_as_paranoid
belongs_to :project
belongs_to :trigger
2017-03-30 07:59:20 -04:00
delegate :ref, to: :trigger
validates :trigger, presence: true
2017-03-31 13:02:26 -04:00
validates :cron, cron: true, presence: true
validates :cron_time_zone, presence: true
2017-03-31 13:02:26 -04:00
validates :ref, ref: true, presence: true
validate :check_cron_frequency
after_create :schedule_next_run!
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)
update!(next_run_at: next_time)
2017-03-23 11:18:13 -04:00
end
end
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-31 13:02:26 -04:00
if less_than_1_hour_from_now?(next_time)
self.errors.add(:cron, " can not be less than 1 hour")
end
end
end
end