Class: Fugit::Cron
- Inherits:
-
Object
- Object
- Fugit::Cron
- Defined in:
- lib/fugit/cron.rb
Defined Under Namespace
Modules: Parser Classes: Frequency, TimeCursor
Constant Summary collapse
- SPECIALS =
{ '@reboot' => :reboot, '@yearly' => '0 0 1 1 *', '@annually' => '0 0 1 1 *', '@monthly' => '0 0 1 * *', '@weekly' => '0 0 * * 0', '@daily' => '0 0 * * *', '@midnight' => '0 0 * * *', '@hourly' => '0 * * * *' }
- MAXDAYS =
[ nil, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
- MAX_ITERATION_COUNT =
2048- SLOTS =
[ [ :seconds, 1, 60 ], [ :minutes, 60, 60 ], [ :hours, 3600, 24 ], [ :days, 24 * 3600, 365 ] ]
Instance Attribute Summary collapse
-
#hours ⇒ Object
readonly
Returns the value of attribute hours.
-
#minutes ⇒ Object
readonly
Returns the value of attribute minutes.
-
#monthdays ⇒ Object
readonly
Returns the value of attribute monthdays.
-
#months ⇒ Object
readonly
Returns the value of attribute months.
-
#original ⇒ Object
readonly
Returns the value of attribute original.
-
#seconds ⇒ Object
readonly
Returns the value of attribute seconds.
-
#timezone ⇒ Object
readonly
Returns the value of attribute timezone.
-
#weekdays ⇒ Object
readonly
Returns the value of attribute weekdays.
-
#zone ⇒ Object
readonly
Returns the value of attribute zone.
Class Method Summary collapse
Instance Method Summary collapse
- #==(o) ⇒ Object (also: #eql?)
-
#brute_frequency(year = 2017) ⇒ Object
Mostly used as a #next_time sanity check.
- #day_match?(nt) ⇒ Boolean
- #hash ⇒ Object
- #hour_match?(nt) ⇒ Boolean
- #match?(t) ⇒ Boolean
- #min_match?(nt) ⇒ Boolean
- #month_match?(nt) ⇒ Boolean
- #monthday_match?(nt) ⇒ Boolean
-
#next_time(from = ::EtOrbi::EoTime.now) ⇒ Object
See gh-15 and tst/iteration_count.rb.
- #previous_time(from = ::EtOrbi::EoTime.now) ⇒ Object
- #rough_frequency ⇒ Object
- #sec_match?(nt) ⇒ Boolean
- #to_a ⇒ Object
- #to_cron_s ⇒ Object
- #to_h ⇒ Object
- #weekday_hash_match?(nt, hsh) ⇒ Boolean
- #weekday_match?(nt) ⇒ Boolean
- #weekday_modulo_match?(nt, mod) ⇒ Boolean
Instance Attribute Details
#hours ⇒ Object (readonly)
Returns the value of attribute hours.
20 21 22 |
# File 'lib/fugit/cron.rb', line 20 def hours @hours end |
#minutes ⇒ Object (readonly)
Returns the value of attribute minutes.
20 21 22 |
# File 'lib/fugit/cron.rb', line 20 def minutes @minutes end |
#monthdays ⇒ Object (readonly)
Returns the value of attribute monthdays.
20 21 22 |
# File 'lib/fugit/cron.rb', line 20 def monthdays @monthdays end |
#months ⇒ Object (readonly)
Returns the value of attribute months.
20 21 22 |
# File 'lib/fugit/cron.rb', line 20 def months @months end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
18 19 20 |
# File 'lib/fugit/cron.rb', line 18 def original @original end |
#seconds ⇒ Object (readonly)
Returns the value of attribute seconds.
20 21 22 |
# File 'lib/fugit/cron.rb', line 20 def seconds @seconds end |
#timezone ⇒ Object (readonly)
Returns the value of attribute timezone.
20 21 22 |
# File 'lib/fugit/cron.rb', line 20 def timezone @timezone end |
#weekdays ⇒ Object (readonly)
Returns the value of attribute weekdays.
20 21 22 |
# File 'lib/fugit/cron.rb', line 20 def weekdays @weekdays end |
#zone ⇒ Object (readonly)
Returns the value of attribute zone.
18 19 20 |
# File 'lib/fugit/cron.rb', line 18 def zone @zone end |
Class Method Details
.do_parse(s) ⇒ Object
46 47 48 49 50 |
# File 'lib/fugit/cron.rb', line 46 def do_parse(s) parse(s) || fail(ArgumentError.new("invalid cron string #{s.inspect}")) end |
.new(original) ⇒ Object
25 26 27 28 |
# File 'lib/fugit/cron.rb', line 25 def new(original) parse(original) end |
.parse(s) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/fugit/cron.rb', line 30 def parse(s) return s if s.is_a?(self) s = SPECIALS[s] || s return nil unless s.is_a?(String) #p s; Raabro.pp(Parser.parse(s, debug: 3), colors: true) h = Parser.parse(s) return nil unless h self.allocate.send(:init, s, h) end |
Instance Method Details
#==(o) ⇒ Object Also known as: eql?
392 393 394 395 |
# File 'lib/fugit/cron.rb', line 392 def ==(o) o.is_a?(::Fugit::Cron) && o.to_a == to_a end |
#brute_frequency(year = 2017) ⇒ Object
Mostly used as a #next_time sanity check. Avoid for “business” use, it’s slow.
2017 is a non leap year (though it is preceded by a leap second on 2016-12-31)
Nota bene: cron with seconds are not supported.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/fugit/cron.rb', line 291 def brute_frequency(year=2017) FREQUENCY_CACHE["#{to_cron_s}|#{year}"] ||= begin deltas = [] t = EtOrbi.make_time("#{year}-01-01") - 1 t0 = nil t1 = nil loop do t1 = next_time(t) deltas << (t1 - t).to_i if t0 t0 ||= t1 break if deltas.any? && t1.year > year break if t1.year - t0.year > 7 t = t1 end Frequency.new(deltas, t1 - t0) end end |
#day_match?(nt) ⇒ Boolean
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/fugit/cron.rb', line 187 def day_match?(nt) return weekday_match?(nt) || monthday_match?(nt) \ if @weekdays && @monthdays return false unless weekday_match?(nt) return false unless monthday_match?(nt) true end |
#hash ⇒ Object
398 399 400 401 |
# File 'lib/fugit/cron.rb', line 398 def hash to_a.hash end |
#hour_match?(nt) ⇒ Boolean
140 |
# File 'lib/fugit/cron.rb', line 140 def hour_match?(nt); ( ! @hours) || @hours.include?(nt.hour); end |
#match?(t) ⇒ Boolean
198 199 200 201 202 203 204 |
# File 'lib/fugit/cron.rb', line 198 def match?(t) t = Fugit.do_parse_at(t) month_match?(t) && day_match?(t) && hour_match?(t) && min_match?(t) && sec_match?(t) end |
#min_match?(nt) ⇒ Boolean
141 |
# File 'lib/fugit/cron.rb', line 141 def min_match?(nt); ( ! @minutes) || @minutes.include?(nt.min); end |
#month_match?(nt) ⇒ Boolean
139 |
# File 'lib/fugit/cron.rb', line 139 def month_match?(nt); ( ! @months) || @months.include?(nt.month); end |
#monthday_match?(nt) ⇒ Boolean
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/fugit/cron.rb', line 176 def monthday_match?(nt) return true if @monthdays.nil? last = (TimeCursor.new(self, nt).inc_month.time - 24 * 3600).day + 1 @monthdays .collect { |d| d < 1 ? last + d : d } .include?(nt.day) end |
#next_time(from = ::EtOrbi::EoTime.now) ⇒ Object
See gh-15 and tst/iteration_count.rb
Initially set to 1024 after seeing the worst case for #next_time at 167 iterations, I placed it at 2048 after experimenting with gh-18 and noticing some > 1024 for some experiments. 2048 should be ok.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/fugit/cron.rb', line 215 def next_time(from=::EtOrbi::EoTime.now) from = ::EtOrbi.make_time(from) sfrom = from.strftime('%F|%T') ifrom = from.to_i i = 0 t = TimeCursor.new(self, from.translate(@timezone)) # # the translation occurs in the timezone of # this Fugit::Cron instance loop do fail RuntimeError.new( "too many loops for #{@original.inspect} #next_time, breaking, " + "cron expression most likely invalid (Feb 30th like?), " + "please fill an issue at https://git.io/fjJC9" ) if (i += 1) > MAX_ITERATION_COUNT (ifrom == t.to_i) && (t.inc(1); next) month_match?(t) || (t.inc_month; next) day_match?(t) || (t.inc_day; next) hour_match?(t) || (t.inc_hour; next) min_match?(t) || (t.inc_min; next) sec_match?(t) || (t.inc_sec; next) st = t.time.strftime('%F|%T') (from, sfrom, ifrom = t.time, st, t.to_i; next) if st == sfrom # # when transitioning out of DST, this prevents #next_time from # yielding the same literal time twice in a row, see gh-6 break end t.time.translate(from.zone) # # the answer time is in the same timezone as the `from` # starting point end |
#previous_time(from = ::EtOrbi::EoTime.now) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/fugit/cron.rb', line 257 def previous_time(from=::EtOrbi::EoTime.now) from = ::EtOrbi.make_time(from) i = 0 t = TimeCursor.new(self, (from - 1).translate(@timezone)) loop do fail RuntimeError.new( "too many loops for #{@original.inspect} #previous_time, breaking, " + "cron expression most likely invalid (Feb 30th like?), " + "please fill an issue at https://git.io/fjJCQ" ) if (i += 1) > MAX_ITERATION_COUNT month_match?(t) || (t.dec_month; next) day_match?(t) || (t.dec_day; next) hour_match?(t) || (t.dec_hour; next) min_match?(t) || (t.dec_min; next) sec_match?(t) || (t.dec_sec; next) break end t.time.translate(from.zone) end |
#rough_frequency ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/fugit/cron.rb', line 321 def rough_frequency slots = SLOTS .collect { |k, v0, v1| a = (k == :days) ? rough_days : instance_variable_get("@#{k}") [ k, v0, v1, a ] } slots.each do |k, v0, _, a| next if a == [ 0 ] break if a != nil return v0 if a == nil end slots.each do |k, v0, v1, a| next unless a && a.length > 1 return (a + [ a.first + v1 ]) .each_cons(2) .collect { |a0, a1| a1 - a0 } .min * v0 end slots.reverse.each do |k, v0, v1, a| return v0 * v1 if a && a.length == 1 end 1 # second end |
#sec_match?(nt) ⇒ Boolean
142 |
# File 'lib/fugit/cron.rb', line 142 def sec_match?(nt); ( ! @seconds) || @seconds.include?(nt.sec); end |
#to_a ⇒ Object
377 378 379 380 |
# File 'lib/fugit/cron.rb', line 377 def to_a [ @seconds, @minutes, @hours, @monthdays, @months, @weekdays ] end |
#to_cron_s ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/fugit/cron.rb', line 53 def to_cron_s @cron_s ||= [ @seconds == [ 0 ] ? nil : (@seconds || [ '*' ]).join(','), (@minutes || [ '*' ]).join(','), (@hours || [ '*' ]).join(','), (@monthdays || [ '*' ]).join(','), (@months || [ '*' ]).join(','), (@weekdays || [ [ '*' ] ]).map { |d| d.compact.join('#') }.join(','), @timezone ? @timezone.name : nil ].compact.join(' ') end |
#to_h ⇒ Object
382 383 384 385 386 387 388 389 390 |
# File 'lib/fugit/cron.rb', line 382 def to_h { seconds: @seconds, minutes: @minutes, hours: @hours, monthdays: @monthdays, months: @months, weekdays: @weekdays } end |
#weekday_hash_match?(nt, hsh) ⇒ Boolean
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/fugit/cron.rb', line 144 def weekday_hash_match?(nt, hsh) phsh, nhsh = nt.wday_in_month if hsh > 0 hsh == phsh # positive wday, from the beginning of the month else hsh == nhsh # negative wday, from the end of the month, -1 == last end end |
#weekday_match?(nt) ⇒ Boolean
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/fugit/cron.rb', line 160 def weekday_match?(nt) return true if @weekdays.nil? wd, hom = @weekdays.find { |d, _| d == nt.wday } return false unless wd return true if hom.nil? if hom.is_a?(Array) weekday_modulo_match?(nt, hom) else weekday_hash_match?(nt, hom) end end |
#weekday_modulo_match?(nt, mod) ⇒ Boolean
155 156 157 158 |
# File 'lib/fugit/cron.rb', line 155 def weekday_modulo_match?(nt, mod) nt.rweek % mod[0] == mod[1] end |