Class: CronParser

Inherits:
Object
  • Object
show all
Defined in:
lib/cron_parser.rb

Overview

Parses cron expressions and computes the next occurence of the “job”

Defined Under Namespace

Classes: InternalTime

Constant Summary collapse

SYMBOLS =
{
   "jan" => "1",
   "feb" => "2",
   "mar" => "3",
   "apr" => "4",
   "may" => "5",
   "jun" => "6",
   "jul" => "7",
   "aug" => "8",
   "sep" => "9",
   "oct" => "10",
   "nov" => "11",
   "dec" => "12",

   "sun" => "0",
   "mon" => "1",
   "tue" => "2",
   "wed" => "3",
   "thu" => "4",
   "fri" => "5",
   "sat" => "6"
}
SUBELEMENT_REGEX =
%r{^(\d+)(-(\d+)(/(\d+))?)?$}

Instance Method Summary collapse

Constructor Details

#initialize(source, time_source = Time) ⇒ CronParser

Returns a new instance of CronParser.



54
55
56
57
58
# File 'lib/cron_parser.rb', line 54

def initialize(source,time_source = Time)
  @source = interpret_vixieisms(source)
  @time_source = time_source
  validate_source
end

Instance Method Details

#interpret_vixieisms(spec) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cron_parser.rb', line 60

def interpret_vixieisms(spec)
  case spec
  when '@reboot'
    raise ArgumentError, "Can't predict last/next run of @reboot"
  when '@yearly', '@annually'
    '0 0 1 1 *'
  when '@monthly'
    '0 0 1 * *'
  when '@weekly'
    '0 0 * * 0'
  when '@daily', '@midnight'
    '0 0 * * *'
  when '@hourly'
    '0 * * * *'
  else
    spec
  end
end

#last(now = @time_source.now) ⇒ Object

returns the last occurence before the given date



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cron_parser.rb', line 105

def last(now = @time_source.now)
  t = InternalTime.new(now,@time_source)

  unless time_specs[:month][0].include?(t.month)
    nudge_month(t, :last)
    t.day = 32
  end

  if t.day == 32 || !interpolate_weekdays(t.year, t.month)[0].include?(t.day)
    nudge_date(t, :last)
    t.hour = 24
  end

  unless time_specs[:hour][0].include?(t.hour)
    nudge_hour(t, :last)
    t.min = 60
  end

  # always nudge the minute
  nudge_minute(t, :last)
  t.to_time
end

#next(now = @time_source.now) ⇒ Object

returns the next occurence after the given date



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cron_parser.rb', line 81

def next(now = @time_source.now)
  t = InternalTime.new(now, @time_source)

  unless time_specs[:month][0].include?(t.month)
    nudge_month(t)
    t.day = 0
  end

  unless interpolate_weekdays(t.year, t.month)[0].include?(t.day)
    nudge_date(t)
    t.hour = -1
  end

  unless time_specs[:hour][0].include?(t.hour)
    nudge_hour(t)
    t.min = -1
  end

  # always nudge the minute
  nudge_minute(t)
  t.to_time
end

#parse_element(elem, allowed_range) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cron_parser.rb', line 130

def parse_element(elem, allowed_range)
  values = elem.split(',').map do |subel|
    if subel =~ /^\*/
      step = subel.length > 1 ? subel[2..-1].to_i : 1
      stepped_range(allowed_range, step)
    else
      if SUBELEMENT_REGEX === subel
        if $5 # with range
          stepped_range($1.to_i..$3.to_i, $5.to_i)
        elsif $3 # range without step
          stepped_range($1.to_i..$3.to_i, 1)
        else # just a numeric
          [$1.to_i]
        end
      else
        raise ArgumentError, "Bad Vixie-style specification #{subel}"
      end
    end
  end.flatten.sort

  [Set.new(values), values, elem]
end