2017-04-04 09:44:25 +00:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class CronParser
|
|
|
|
VALID_SYNTAX_SAMPLE_TIME_ZONE = 'UTC'.freeze
|
|
|
|
VALID_SYNTAX_SAMPLE_CRON = '* * * * *'.freeze
|
|
|
|
|
2017-04-04 16:54:45 +00:00
|
|
|
def initialize(cron, cron_timezone = 'UTC')
|
2017-04-04 09:44:25 +00:00
|
|
|
@cron = cron
|
2017-04-04 16:54:45 +00:00
|
|
|
@cron_timezone = cron_timezone
|
2017-04-04 09:44:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def next_time_from(time)
|
2017-04-04 16:54:45 +00:00
|
|
|
cron_line = try_parse_cron(@cron, @cron_timezone)
|
2017-04-04 09:49:52 +00:00
|
|
|
cron_line.next_time(time).in_time_zone(Time.zone) if cron_line.present?
|
2017-04-04 09:44:25 +00:00
|
|
|
end
|
|
|
|
|
2017-04-04 10:14:49 +00:00
|
|
|
def cron_valid?
|
|
|
|
try_parse_cron(@cron, VALID_SYNTAX_SAMPLE_TIME_ZONE).present?
|
|
|
|
end
|
|
|
|
|
2017-04-04 16:54:45 +00:00
|
|
|
def cron_timezone_valid?
|
|
|
|
try_parse_cron(VALID_SYNTAX_SAMPLE_CRON, @cron_timezone).present?
|
2017-04-04 09:44:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-04 16:54:45 +00:00
|
|
|
def try_parse_cron(cron, cron_timezone)
|
|
|
|
Rufus::Scheduler.parse("#{cron} #{cron_timezone}")
|
2017-04-04 12:18:51 +00:00
|
|
|
rescue
|
|
|
|
# noop
|
2017-04-04 09:44:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|