Class: DelayedCronJob::Cronline

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

Overview

A ‘cron line’ is a line in the sense of a crontab (man 5 crontab) file line.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ Cronline

Returns a new instance of Cronline.

Raises:

  • (ArgumentError)


47
48
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
# File 'lib/delayed_cron_job/cronline.rb', line 47

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

#daysObject (readonly)

Returns the value of attribute days.



41
42
43
# File 'lib/delayed_cron_job/cronline.rb', line 41

def days
  @days
end

#hoursObject (readonly)

Returns the value of attribute hours.



40
41
42
# File 'lib/delayed_cron_job/cronline.rb', line 40

def hours
  @hours
end

#minutesObject (readonly)

Returns the value of attribute minutes.



39
40
41
# File 'lib/delayed_cron_job/cronline.rb', line 39

def minutes
  @minutes
end

#monthsObject (readonly)

Returns the value of attribute months.



42
43
44
# File 'lib/delayed_cron_job/cronline.rb', line 42

def months
  @months
end

#originalObject (readonly)

The string used for creating this cronline instance.



36
37
38
# File 'lib/delayed_cron_job/cronline.rb', line 36

def original
  @original
end

#secondsObject (readonly)

Returns the value of attribute seconds.



38
39
40
# File 'lib/delayed_cron_job/cronline.rb', line 38

def seconds
  @seconds
end

#timezoneObject (readonly)

Returns the value of attribute timezone.



45
46
47
# File 'lib/delayed_cron_job/cronline.rb', line 45

def timezone
  @timezone
end

#weekdaysObject (readonly)

Returns the value of attribute weekdays.



43
44
45
# File 'lib/delayed_cron_job/cronline.rb', line 43

def weekdays
  @weekdays
end

Instance Method Details

#brute_frequencyObject

Returns the shortest delta between two potential occurences of the schedule described by this cronline.

.

For a simple cronline like “*/5 * * * *”, obviously the frequency is five minutes. Why does this method look at a whole year of #next_time ?

Consider “* * * * sun#2,sun#3”, the computed frequency is 1 week (the shortest delta is the one between the second sunday and the third sunday). This method takes no chance and runs next_time for the span of a whole year and keeps the shortest.

Of course, this method can get VERY slow if you call on it a second- based cronline…

Since it’s a rarely used method, I haven’t taken the time to make it smarter/faster.

One obvious improvement would be to cache the result once computed…

See github.com/jmettraux/rufus-scheduler/issues/89 for a discussion about this method.



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/delayed_cron_job/cronline.rb', line 257

def brute_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

#frequencyObject

Returns a quickly computed approximation of the frequency for this cron line.

#brute_frequency, on the other hand, will compute the frequency by examining a whole, that can take more than seconds for a seconds level cron…



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/delayed_cron_job/cronline.rb', line 218

def frequency

  return brute_frequency unless @seconds && @seconds.length > 1

  delta = 60
  prev = @seconds[0]

  @seconds[1..-1].each do |sec|
    d = sec - prev
    delta = d if d < delta
  end

  delta
end

#matches?(time) ⇒ Boolean

Returns true if the given time matches this cron line.

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/delayed_cron_job/cronline.rb', line 83

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)



123
124
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
# File 'lib/delayed_cron_job/cronline.rb', line 123

def next_time(from=Time.now)

  time = local_time(from)
  time = round_to_seconds(time)

  # start at the next second
  time = time + 1

  loop do
    unless date_match?(time)
      dst = time.isdst
      time += (24 - time.hour) * 3600 - time.min * 60 - time.sec
      time -= 3600 if time.isdst != dst # not necessary for winter, but...
      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

  global_time(time, from.utc?)

rescue TZInfo::PeriodNotFound

  next_time(from + 3600)
end

#previous_time(from = Time.now) ⇒ Object

Returns the previous time the cronline matched. It’s like next_time, but for the past.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/delayed_cron_job/cronline.rb', line 161

def previous_time(from=Time.now)

  time = local_time(from)
  time = round_to_seconds(time)

  # start at the previous second
  time = time - 1

  loop do
    unless date_match?(time)
      time -= time.hour * 3600 + time.min * 60 + time.sec + 1; next
    end
    unless sub_match?(time, :hour, @hours)
      time -= time.min * 60 + time.sec + 1; next
    end
    unless sub_match?(time, :min, @minutes)
      time -= time.sec + 1; next
    end
    unless sub_match?(time, :sec, @seconds)
      time -= 1; next
    end

    break
  end

  global_time(time, from.utc?)

rescue TZInfo::PeriodNotFound

  previous_time(time)
end

#to_arrayObject

Returns an array of 6 arrays (seconds, minutes, hours, days, months, weekdays). This method is used by the cronline unit tests.



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/delayed_cron_job/cronline.rb', line 197

def to_array

  [
    @seconds,
    @minutes,
    @hours,
    @days,
    @months,
    @weekdays,
    @monthdays,
    @timezone ? @timezone.name : nil
  ]
end