summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/cron_parser.rb
blob: dad8c3cdf5bb7274d315757ffa9b01affee51b7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Gitlab
  module Ci
    class CronParser
      VALID_SYNTAX_SAMPLE_TIME_ZONE = 'UTC'.freeze
      VALID_SYNTAX_SAMPLE_CRON = '* * * * *'.freeze

      def initialize(cron, cron_timezone = 'UTC')
        @cron = cron
        @cron_timezone = ActiveSupport::TimeZone.find_tzinfo(cron_timezone).name
      end

      def next_time_from(time)
        @cron_line ||= try_parse_cron(@cron, @cron_timezone)
        @cron_line.next_time(time).in_time_zone(Time.zone) if @cron_line.present?
      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

      # NOTE:
      # cron_timezone can only accept timezones listed in TZInfo::Timezone.
      # Aliases of Timezones from ActiveSupport::TimeZone are NOT accepted,
      # because Rufus::Scheduler only supports TZInfo::Timezone.
      #
      # 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)
        cron_line = Rufus::Scheduler.parse("#{cron} #{cron_timezone}")
        cron_line if cron_line.is_a?(Rufus::Scheduler::CronLine)
      rescue
        # noop
      end
    end
  end
end