debian-mirror-gitlab/lib/gitlab/ci/cron_parser.rb

55 lines
1.8 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Gitlab
module Ci
class CronParser
2019-12-04 20:38:33 +05:30
VALID_SYNTAX_SAMPLE_TIME_ZONE = 'UTC'
VALID_SYNTAX_SAMPLE_CRON = '* * * * *'
2017-08-17 22:00:37 +05:30
def initialize(cron, cron_timezone = 'UTC')
@cron = cron
2018-10-15 14:42:47 +05:30
@cron_timezone = timezone_name(cron_timezone)
2017-08-17 22:00:37 +05:30
end
def next_time_from(time)
@cron_line ||= try_parse_cron(@cron, @cron_timezone)
2017-09-10 17:25:29 +05:30
@cron_line.next_time(time).utc.in_time_zone(Time.zone) if @cron_line.present?
2017-08-17 22:00:37 +05:30
end
def cron_valid?
try_parse_cron(@cron, VALID_SYNTAX_SAMPLE_TIME_ZONE).present?
end
def cron_timezone_valid?
try_parse_cron(VALID_SYNTAX_SAMPLE_CRON, @cron_timezone).present?
end
private
2018-10-15 14:42:47 +05:30
def timezone_name(timezone)
ActiveSupport::TimeZone.find_tzinfo(timezone).name
rescue TZInfo::InvalidTimezoneIdentifier
timezone
end
2017-08-17 22:00:37 +05:30
# NOTE:
# cron_timezone can only accept timezones listed in TZInfo::Timezone.
# Aliases of Timezones from ActiveSupport::TimeZone are NOT accepted,
2019-03-02 22:35:43 +05:30
# because Fugit::Cron only supports TZInfo::Timezone.
2017-08-17 22:00:37 +05:30
#
# For example, those codes have the same effect.
# Time.zone = 'Pacific Time (US & Canada)' (ActiveSupport::TimeZone)
# Time.zone = 'America/Los_Angeles' (TZInfo::Timezone)
#
# However, try_parse_cron only accepts the latter format.
# try_parse_cron('* * * * *', 'Pacific Time (US & Canada)') -> Doesn't work
# try_parse_cron('* * * * *', 'America/Los_Angeles') -> Works
# If you want to know more, please take a look
# https://github.com/rails/rails/blob/master/activesupport/lib/active_support/values/time_zone.rb
def try_parse_cron(cron, cron_timezone)
2019-03-02 22:35:43 +05:30
Fugit::Cron.parse("#{cron} #{cron_timezone}")
2017-08-17 22:00:37 +05:30
end
end
end
end