Class: Scheduled::CronParser

Inherits:
Object
  • Object
show all
Defined in:
lib/scheduled/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.



79
80
81
82
83
# File 'lib/scheduled/cron_parser.rb', line 79

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

Instance Method Details

#interpret_vixieisms(spec) ⇒ Object



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

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, num = 1) ⇒ Object

returns the last occurence before the given date



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/scheduled/cron_parser.rb', line 135

def last(now = @time_source.now, num=1)
  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 = t.to_time
  if num > 1
    recursive_calculate(:last,t,num)
  else
    t
  end
end

#next(now = @time_source.now, num = 1) ⇒ Object

returns the next occurence after the given date



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/scheduled/cron_parser.rb', line 106

def next(now = @time_source.now, num = 1)
  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 = t.to_time
  if num > 1
    recursive_calculate(:next,t,num)
  else
    t
  end
end

#parse_element(elem, allowed_range) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/scheduled/cron_parser.rb', line 165

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