Class: Gitlab::Ci::CronParser
- Inherits:
-
Object
- Object
- Gitlab::Ci::CronParser
- Defined in:
- lib/gitlab/ci/cron_parser.rb
Constant Summary collapse
- VALID_SYNTAX_SAMPLE_TIME_ZONE =
'UTC'
- VALID_SYNTAX_SAMPLE_CRON =
'* * * * *'
Class Method Summary collapse
- .fall_in_months(offset, start_date) ⇒ Object
- .parse_natural(expression, cron_timezone = 'UTC') ⇒ Object
-
.parse_natural_with_timestamp(starts_at, cadence) ⇒ Object
This method generates compatible expressions that can be parsed by Fugit::Nat.parse to generate a cron line.
Instance Method Summary collapse
- #cron_timezone_valid? ⇒ Boolean
- #cron_valid? ⇒ Boolean
-
#initialize(cron, cron_timezone = 'UTC') ⇒ CronParser
constructor
A new instance of CronParser.
- #match?(time) ⇒ Boolean
- #next_time_from(time) ⇒ Object
- #previous_time_from(time) ⇒ Object
Constructor Details
#initialize(cron, cron_timezone = 'UTC') ⇒ CronParser
Returns a new instance of CronParser.
45 46 47 48 |
# File 'lib/gitlab/ci/cron_parser.rb', line 45 def initialize(cron, cron_timezone = 'UTC') @cron = cron @cron_timezone = timezone_name(cron_timezone) end |
Class Method Details
.fall_in_months(offset, start_date) ⇒ Object
40 41 42 |
# File 'lib/gitlab/ci/cron_parser.rb', line 40 def fall_in_months(offset, start_date) (1..(12 / offset)).map { |i| start_date.next_month(offset * i).month }.join(',') end |
.parse_natural(expression, cron_timezone = 'UTC') ⇒ Object
10 11 12 |
# File 'lib/gitlab/ci/cron_parser.rb', line 10 def parse_natural(expression, cron_timezone = 'UTC') new(Fugit::Nat.parse(expression)&.original, cron_timezone) end |
.parse_natural_with_timestamp(starts_at, cadence) ⇒ Object
This method generates compatible expressions that can be parsed by Fugit::Nat.parse to generate a cron line. It takes start date of the cron and cadence in the following format: cadence =
unit: 'day/week/month/year'
duration: 1
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/gitlab/ci/cron_parser.rb', line 21 def (starts_at, cadence) case cadence[:unit] when 'day' # Currently supports only 'every 1 day'. "#{starts_at.min} #{starts_at.hour} * * *" when 'week' # Currently supports only 'every 1 week'. "#{starts_at.min} #{starts_at.hour} * * #{starts_at.wday}" when 'month' unless [1, 3, 6, 12].include?(cadence[:duration]) raise NotImplementedError, "The cadence #{cadence} is not supported" end "#{starts_at.min} #{starts_at.hour} #{starts_at.mday} #{fall_in_months(cadence[:duration], starts_at)} *" when 'year' # Currently supports only 'every 1 year'. "#{starts_at.min} #{starts_at.hour} #{starts_at.mday} #{starts_at.month} *" else raise NotImplementedError, "The cadence unit #{cadence[:unit]} is not implemented" end end |
Instance Method Details
#cron_timezone_valid? ⇒ Boolean
62 63 64 |
# File 'lib/gitlab/ci/cron_parser.rb', line 62 def cron_timezone_valid? try_parse_cron(VALID_SYNTAX_SAMPLE_CRON, @cron_timezone).present? end |
#cron_valid? ⇒ Boolean
58 59 60 |
# File 'lib/gitlab/ci/cron_parser.rb', line 58 def cron_valid? try_parse_cron(@cron, VALID_SYNTAX_SAMPLE_TIME_ZONE).present? end |
#match?(time) ⇒ Boolean
66 67 68 |
# File 'lib/gitlab/ci/cron_parser.rb', line 66 def match?(time) cron_line.match?(time) end |
#next_time_from(time) ⇒ Object
50 51 52 |
# File 'lib/gitlab/ci/cron_parser.rb', line 50 def next_time_from(time) cron_line.next_time(time).utc.in_time_zone(Time.zone) if cron_line.present? end |
#previous_time_from(time) ⇒ Object
54 55 56 |
# File 'lib/gitlab/ci/cron_parser.rb', line 54 def previous_time_from(time) cron_line.previous_time(time).utc.in_time_zone(Time.zone) if cron_line.present? end |