2017-03-22 18:54:49 +00:00
|
|
|
module Ci
|
2017-03-29 11:49:47 +00:00
|
|
|
class TriggerSchedule < ActiveRecord::Base
|
2017-03-22 18:54:49 +00:00
|
|
|
extend Ci::Model
|
|
|
|
|
|
|
|
acts_as_paranoid
|
|
|
|
|
|
|
|
belongs_to :project
|
2017-03-29 11:49:47 +00:00
|
|
|
belongs_to :trigger
|
2017-03-22 18:54:49 +00:00
|
|
|
|
2017-03-30 11:59:20 +00:00
|
|
|
delegate :ref, to: :trigger
|
|
|
|
|
2017-03-29 18:33:23 +00:00
|
|
|
validates :trigger, presence: true
|
2017-03-31 17:02:26 +00:00
|
|
|
validates :cron, cron: true, presence: true
|
2017-03-29 18:33:23 +00:00
|
|
|
validates :cron_time_zone, presence: true
|
2017-03-31 17:02:26 +00:00
|
|
|
validates :ref, ref: true, presence: true
|
2017-04-01 14:27:24 +00:00
|
|
|
# validate :check_cron_frequency
|
2017-03-29 18:33:23 +00:00
|
|
|
|
|
|
|
after_create :schedule_next_run!
|
|
|
|
|
2017-03-22 18:54:49 +00:00
|
|
|
def schedule_next_run!
|
2017-03-31 10:08:39 +00:00
|
|
|
next_time = Ci::CronParser.new(cron, cron_time_zone).next_time_from(Time.now)
|
|
|
|
|
2017-04-01 14:27:24 +00:00
|
|
|
# if next_time.present? && !less_than_1_hour_from_now?(next_time)
|
|
|
|
if next_time.present?
|
2017-03-29 18:33:23 +00:00
|
|
|
update!(next_run_at: next_time)
|
2017-03-23 15:18:13 +00:00
|
|
|
end
|
2017-03-22 18:54:49 +00:00
|
|
|
end
|
|
|
|
|
2017-04-01 14:27:24 +00:00
|
|
|
# private
|
2017-03-29 18:33:23 +00:00
|
|
|
|
2017-04-01 14:27:24 +00:00
|
|
|
# def less_than_1_hour_from_now?(time)
|
|
|
|
# puts "diff: #{(time - Time.now).abs.inspect}"
|
|
|
|
# ((time - Time.now).abs < 1.hour) ? true : false
|
|
|
|
# end
|
2017-03-31 10:08:39 +00:00
|
|
|
|
2017-04-01 14:27:24 +00:00
|
|
|
# def check_cron_frequency
|
|
|
|
# next_time = Ci::CronParser.new(cron, cron_time_zone).next_time_from(Time.now)
|
2017-03-29 18:33:23 +00:00
|
|
|
|
2017-04-01 14:27:24 +00:00
|
|
|
# if less_than_1_hour_from_now?(next_time)
|
|
|
|
# self.errors.add(:cron, " can not be less than 1 hour")
|
|
|
|
# end
|
|
|
|
# end
|
2017-03-22 18:54:49 +00:00
|
|
|
end
|
|
|
|
end
|