Class: Rufus::CronLine
A ‘cron line’ is a line in the sense of a crontab (man 5 crontab) file line.
Constants
| Name | Value |
|---|---|
| WDS | [ "sun", "mon", "tue", "wed", "thu", "fri", "sat" ] |
Attributes
| Name | Read/write? |
|---|---|
| days | R |
| hours | R |
| minutes | R |
| months | R |
| original | R |
| seconds | R |
| weekdays | R |
Public Class Methods
new (line)
# File lib/rufus/cronline.rb, line 52 52: def initialize (line) 53: 54: super() 55: 56: @original = line 57: 58: items = line.split 59: 60: unless [ 5, 6 ].include?(items.length) 61: raise \ 62: "cron '#{line}' string should hold 5 or 6 items, " + 63: "not #{items.length}" \ 64: end 65: 66: offset = items.length - 5 67: 68: @seconds = if offset == 1 69: parse_item(items[0], 0, 59) 70: else 71: [ 0 ] 72: end 73: @minutes = parse_item(items[0+offset], 0, 59) 74: @hours = parse_item(items[1+offset], 0, 24) 75: @days = parse_item(items[2+offset], 1, 31) 76: @months = parse_item(items[3+offset], 1, 12) 77: @weekdays = parse_weekdays(items[4+offset]) 78: 79: #adjust_arrays() 80: end
Public Instance Methods
matches? (time)
Returns true if the given time matches this cron line.
(the precision is passed as well to determine if it‘s worth checking seconds and minutes)
# File lib/rufus/cronline.rb, line 88 88: def matches? (time) 89: #def matches? (time, precision) 90: 91: time = Time.at(time) unless time.kind_of?(Time) 92: 93: return false \ 94: if no_match?(time.sec, @seconds) 95: #if precision <= 1 and no_match?(time.sec, @seconds) 96: return false \ 97: if no_match?(time.min, @minutes) 98: #if precision <= 60 and no_match?(time.min, @minutes) 99: return false \ 100: if no_match?(time.hour, @hours) 101: return false \ 102: if no_match?(time.day, @days) 103: return false \ 104: if no_match?(time.month, @months) 105: return false \ 106: if no_match?(time.wday, @weekdays) 107: 108: true 109: end
next_time (now = Time.now)
Returns the next time that this cron line is supposed to ‘fire‘
This is raw, 3 secs to iterate over 1 year on my macbook :( brutal.
# File lib/rufus/cronline.rb, line 126 126: def next_time (now = Time.now) 127: 128: # 129: # position now to the next cron second 130: 131: if @seconds 132: next_sec = @seconds.find { |s| s > now.sec } || 60 + @seconds.first 133: now += next_sec - now.sec 134: else 135: now += 1 136: end 137: 138: # 139: # prepare sec jump array 140: 141: sjarray = nil 142: 143: if @seconds 144: 145: sjarray = [] 146: 147: i = @seconds.index(now.sec) 148: ii = i 149: 150: loop do 151: cur = @seconds[ii] 152: ii += 1 153: ii = 0 if ii == @seconds.size 154: nxt = @seconds[ii] 155: nxt += 60 if ii == 0 156: sjarray << (nxt - cur) 157: break if ii == i 158: end 159: 160: else 161: 162: sjarray = [ 1 ] 163: end 164: 165: # 166: # ok, seek... 167: 168: i = 0 169: 170: loop do 171: return now if matches?(now) 172: now += sjarray[i] 173: i += 1 174: i = 0 if i == sjarray.size 175: # danger... potentially no exit... 176: end 177: 178: nil 179: end
to_array ()
Returns an array of 6 arrays (seconds, minutes, hours, days, months, weekdays). This method is used by the cronline unit tests.
# File lib/rufus/cronline.rb, line 116 116: def to_array 117: 118: [ @seconds, @minutes, @hours, @days, @months, @weekdays ] 119: end