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

42 lines
1.2 KiB
Ruby
Raw Normal View History

module Ci
class TriggerSchedule < ActiveRecord::Base
extend Ci::Model
include Importable
acts_as_paranoid
belongs_to :project
2017-04-07 12:47:29 +00:00
belongs_to :trigger
2017-03-30 11:59:20 +00:00
validates :trigger, presence: { unless: :importing? }
2017-04-07 12:47:29 +00:00
validates :cron, unless: :importing_or_inactive?, cron: true, presence: { unless: :importing_or_inactive? }
validates :cron_timezone, cron_timezone: true, presence: { unless: :importing_or_inactive? }
validates :ref, presence: { unless: :importing_or_inactive? }
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) }
def importing_or_inactive?
importing? || !active?
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
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
end
2017-04-06 19:02:19 +00:00
2017-04-07 15:15:28 +00:00
def real_next_run(
worker_cron: Settings.cron_jobs['trigger_schedule_worker']['cron'],
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
end
end