Class: UV::Scheduler::CronLine
- Inherits:
-
Object
- Object
- UV::Scheduler::CronLine
- Defined in:
- lib/uv-rays/scheduler/cron.rb
Overview
A ‘cron line’ is a line in the sense of a crontab (man 5 crontab) file line.
Instance Attribute Summary collapse
-
#days ⇒ Object
readonly
Returns the value of attribute days.
-
#hours ⇒ Object
readonly
Returns the value of attribute hours.
-
#minutes ⇒ Object
readonly
Returns the value of attribute minutes.
-
#months ⇒ Object
readonly
Returns the value of attribute months.
-
#original ⇒ Object
readonly
The string used for creating this cronline instance.
-
#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.
Instance Method Summary collapse
-
#frequency ⇒ Object
Returns the shortest delta between two potential occurrences of the schedule described by this cronline.
-
#initialize(line) ⇒ CronLine
constructor
A new instance of CronLine.
-
#matches?(time) ⇒ Boolean
Returns true if the given time matches this cron line.
-
#next_time(from = Time.now) ⇒ Object
Returns the next time that this cron line is supposed to ‘fire’.
-
#previous_time(from = Time.now) ⇒ Object
Returns the previous the cronline matched.
-
#to_array ⇒ Object
Returns an array of 6 arrays (seconds, minutes, hours, days, months, weekdays).
Constructor Details
#initialize(line) ⇒ CronLine
Returns a new instance of CronLine.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/uv-rays/scheduler/cron.rb', line 49 def initialize(line) raise ArgumentError.new( "not a string: #{line.inspect}" ) unless line.is_a?(String) @original = line items = line.split @timezone = (TZInfo::Timezone.get(items.last) rescue nil) items.pop if @timezone raise ArgumentError.new( "not a valid cronline : '#{line}'" ) unless items.length == 5 or items.length == 6 offset = items.length - 5 @seconds = offset == 1 ? parse_item(items[0], 0, 59) : [ 0 ] @minutes = parse_item(items[0 + offset], 0, 59) @hours = parse_item(items[1 + offset], 0, 24) @days = parse_item(items[2 + offset], 1, 31) @months = parse_item(items[3 + offset], 1, 12) @weekdays, @monthdays = parse_weekdays(items[4 + offset]) [ @seconds, @minutes, @hours, @months ].each do |es| raise ArgumentError.new( "invalid cronline: '#{line}'" ) if es && es.find { |e| ! e.is_a?(Integer) } end end |
Instance Attribute Details
#days ⇒ Object (readonly)
Returns the value of attribute days.
43 44 45 |
# File 'lib/uv-rays/scheduler/cron.rb', line 43 def days @days end |
#hours ⇒ Object (readonly)
Returns the value of attribute hours.
42 43 44 |
# File 'lib/uv-rays/scheduler/cron.rb', line 42 def hours @hours end |
#minutes ⇒ Object (readonly)
Returns the value of attribute minutes.
41 42 43 |
# File 'lib/uv-rays/scheduler/cron.rb', line 41 def minutes @minutes end |
#months ⇒ Object (readonly)
Returns the value of attribute months.
44 45 46 |
# File 'lib/uv-rays/scheduler/cron.rb', line 44 def months @months end |
#original ⇒ Object (readonly)
The string used for creating this cronline instance.
38 39 40 |
# File 'lib/uv-rays/scheduler/cron.rb', line 38 def original @original end |
#seconds ⇒ Object (readonly)
Returns the value of attribute seconds.
40 41 42 |
# File 'lib/uv-rays/scheduler/cron.rb', line 40 def seconds @seconds end |
#timezone ⇒ Object (readonly)
Returns the value of attribute timezone.
47 48 49 |
# File 'lib/uv-rays/scheduler/cron.rb', line 47 def timezone @timezone end |
#weekdays ⇒ Object (readonly)
Returns the value of attribute weekdays.
45 46 47 |
# File 'lib/uv-rays/scheduler/cron.rb', line 45 def weekdays @weekdays end |
Instance Method Details
#frequency ⇒ Object
Returns the shortest delta between two potential occurrences of the schedule described by this cronline.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/uv-rays/scheduler/cron.rb', line 204 def frequency delta = 366 * DAY_S t0 = previous_time(Time.local(2000, 1, 1)) loop do break if delta <= 1 break if delta <= 60 && @seconds && @seconds.size == 1 t1 = next_time(t0) d = t1 - t0 delta = d if d < delta break if @months == nil && t1.month == 2 break if t1.year == 2001 t0 = t1 end delta end |
#matches?(time) ⇒ Boolean
Returns true if the given time matches this cron line.
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/uv-rays/scheduler/cron.rb', line 85 def matches?(time) time = Time.at(time) unless time.kind_of?(Time) time = @timezone.utc_to_local(time.getutc) if @timezone return false unless sub_match?(time, :sec, @seconds) return false unless sub_match?(time, :min, @minutes) return false unless sub_match?(time, :hour, @hours) return false unless date_match?(time) true end |
#next_time(from = Time.now) ⇒ Object
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. (Well, I was wrong, takes 0.001 sec on 1.8.7 and 1.9.1)
This method accepts an optional Time parameter. It’s the starting point for the ‘search’. By default, it’s Time.now
Note that the time instance returned will be in the same time zone that the given start point Time (thus a result in the local time zone will be passed if no start time is specified (search start time set to Time.now))
Rufus::Scheduler::CronLine.new('30 7 * * *').next_time(
Time.mktime(2008, 10, 24, 7, 29))
#=> Fri Oct 24 07:30:00 -0500 2008
Rufus::Scheduler::CronLine.new('30 7 * * *').next_time(
Time.utc(2008, 10, 24, 7, 29))
#=> Fri Oct 24 07:30:00 UTC 2008
Rufus::Scheduler::CronLine.new('30 7 * * *').next_time(
Time.utc(2008, 10, 24, 7, 29)).localtime
#=> Fri Oct 24 02:30:00 -0500 2008
(Thanks to K Liu for the note and the examples)
125 126 127 128 129 130 131 132 133 134 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 |
# File 'lib/uv-rays/scheduler/cron.rb', line 125 def next_time(from=Time.now) time = @timezone ? @timezone.utc_to_local(from.getutc) : from time = time.round # chop off subseconds time = time + 1 # start at the next second loop do unless date_match?(time) time += (24 - time.hour) * 3600 - time.min * 60 - time.sec; next end unless sub_match?(time, :hour, @hours) time += (60 - time.min) * 60 - time.sec; next end unless sub_match?(time, :min, @minutes) time += 60 - time.sec; next end unless sub_match?(time, :sec, @seconds) time += 1; next end break end if @timezone time = @timezone.local_to_utc(time) time = time.getlocal unless from.utc? end time end |
#previous_time(from = Time.now) ⇒ Object
Returns the previous the cronline matched. It’s like next_time, but for the past.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/uv-rays/scheduler/cron.rb', line 164 def previous_time(from=Time.now) # looks back by slices of two hours, # # finds for '* * * * sun', '* * 13 * *' and '0 12 13 * *' # starting 1970, 1, 1 in 1.8 to 2 seconds (says Rspec) start = current = from - 2 * 3600 result = nil loop do nex = next_time(current) return (result ? result : previous_time(start)) if nex > from result = current = nex end # never reached end |
#to_array ⇒ Object
Returns an array of 6 arrays (seconds, minutes, hours, days, months, weekdays). This method is used by the cronline unit tests.
187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/uv-rays/scheduler/cron.rb', line 187 def to_array [ @seconds, @minutes, @hours, @days, @months, @weekdays, @monthdays, @timezone ? @timezone.name : nil ] end |