rocketjob 5.2.0.beta1 → 5.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/rocket_job/batch.rb +1 -0
- data/lib/rocket_job/batch/io.rb +2 -2
- data/lib/rocket_job/batch/throttle.rb +1 -1
- data/lib/rocket_job/batch/throttle_running_workers.rb +14 -1
- data/lib/rocket_job/batch/throttle_windows.rb +72 -0
- data/lib/rocket_job/batch/worker.rb +64 -51
- data/lib/rocket_job/event.rb +0 -2
- data/lib/rocket_job/extensions/mongoid/clients/options.rb +0 -2
- data/lib/rocket_job/plugins/cron.rb +5 -12
- data/lib/rocket_job/plugins/job/throttle.rb +1 -1
- data/lib/rocket_job/plugins/job/throttle_running_jobs.rb +1 -1
- data/lib/rocket_job/plugins/processing_window.rb +7 -13
- data/lib/rocket_job/sliced/slice.rb +5 -7
- data/lib/rocket_job/version.rb +1 -1
- metadata +21 -8
- data/lib/rocket_job/plugins/rufus/cron_line.rb +0 -520
- data/lib/rocket_job/plugins/rufus/zo_time.rb +0 -524
@@ -25,8 +25,8 @@ module RocketJob
|
|
25
25
|
|
26
26
|
# The record number of the first record in this slice.
|
27
27
|
# Useful in knowing the line number of each record in this slice
|
28
|
-
# relative to the original file that was uploaded.
|
29
|
-
field :first_record_number, type: Integer
|
28
|
+
# relative to the original file that was uploaded. (1-based index)
|
29
|
+
field :first_record_number, type: Integer, default: 1
|
30
30
|
|
31
31
|
#
|
32
32
|
# Read-only attributes
|
@@ -41,7 +41,7 @@ module RocketJob
|
|
41
41
|
# Number of times that this job has failed to process
|
42
42
|
field :failure_count, type: Integer
|
43
43
|
|
44
|
-
# Number of the record within this slice (not the entire file/job) currently being processed. (
|
44
|
+
# Number of the record within this slice (not the entire file/job) currently being processed. (1-based index)
|
45
45
|
field :processing_record_number, type: Integer
|
46
46
|
|
47
47
|
# This name of the worker that this job is being processed by, or was processed by
|
@@ -112,7 +112,7 @@ module RocketJob
|
|
112
112
|
|
113
113
|
# Returns [Integer] the record number of the record currently being processed relative to the entire file.
|
114
114
|
def current_record_number
|
115
|
-
first_record_number
|
115
|
+
first_record_number + (processing_record_number || 1) - 1
|
116
116
|
end
|
117
117
|
|
118
118
|
# Before Fail save the exception to this slice.
|
@@ -128,9 +128,7 @@ module RocketJob
|
|
128
128
|
# Returns the failed record.
|
129
129
|
# Returns [nil] if there is no failed record
|
130
130
|
def failed_record
|
131
|
-
if exception && processing_record_number
|
132
|
-
at(processing_record_number - 1)
|
133
|
-
end
|
131
|
+
at(processing_record_number - 1) if exception && processing_record_number
|
134
132
|
end
|
135
133
|
|
136
134
|
# Returns [Hash] the slice as a Hash for storage purposes
|
data/lib/rocket_job/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rocketjob
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Reid Morrison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aasm
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '1.
|
47
|
+
version: '1.2'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '1.
|
54
|
+
version: '1.2'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: mongoid
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '4.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: fugit
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.3'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.3'
|
97
111
|
description:
|
98
112
|
email:
|
99
113
|
executables:
|
@@ -124,6 +138,7 @@ files:
|
|
124
138
|
- lib/rocket_job/batch/tabular/output.rb
|
125
139
|
- lib/rocket_job/batch/throttle.rb
|
126
140
|
- lib/rocket_job/batch/throttle_running_workers.rb
|
141
|
+
- lib/rocket_job/batch/throttle_windows.rb
|
127
142
|
- lib/rocket_job/batch/worker.rb
|
128
143
|
- lib/rocket_job/cli.rb
|
129
144
|
- lib/rocket_job/config.rb
|
@@ -162,8 +177,6 @@ files:
|
|
162
177
|
- lib/rocket_job/plugins/processing_window.rb
|
163
178
|
- lib/rocket_job/plugins/restart.rb
|
164
179
|
- lib/rocket_job/plugins/retry.rb
|
165
|
-
- lib/rocket_job/plugins/rufus/cron_line.rb
|
166
|
-
- lib/rocket_job/plugins/rufus/zo_time.rb
|
167
180
|
- lib/rocket_job/plugins/singleton.rb
|
168
181
|
- lib/rocket_job/plugins/state_machine.rb
|
169
182
|
- lib/rocket_job/plugins/transaction.rb
|
@@ -207,9 +220,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
207
220
|
version: '2.3'
|
208
221
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
209
222
|
requirements:
|
210
|
-
- - "
|
223
|
+
- - ">="
|
211
224
|
- !ruby/object:Gem::Version
|
212
|
-
version:
|
225
|
+
version: '0'
|
213
226
|
requirements: []
|
214
227
|
rubygems_version: 3.0.8
|
215
228
|
signing_key:
|
@@ -1,520 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
# Copyright (c) 2006-2017, John Mettraux, jmettraux@gmail.com
|
3
|
-
#
|
4
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
-
# of this software and associated documentation files (the "Software"), to deal
|
6
|
-
# in the Software without restriction, including without limitation the rights
|
7
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
-
# copies of the Software, and to permit persons to whom the Software is
|
9
|
-
# furnished to do so, subject to the following conditions:
|
10
|
-
#
|
11
|
-
# The above copyright notice and this permission notice shall be included in
|
12
|
-
# all copies or substantial portions of the Software.
|
13
|
-
#
|
14
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
-
# THE SOFTWARE.
|
21
|
-
#
|
22
|
-
# Made in Japan.
|
23
|
-
#++
|
24
|
-
#@formatter:off
|
25
|
-
|
26
|
-
require 'set'
|
27
|
-
|
28
|
-
module RocketJob::Plugins::Rufus
|
29
|
-
|
30
|
-
#
|
31
|
-
# A 'cron line' is a line in the sense of a crontab
|
32
|
-
# (man 5 crontab) file line.
|
33
|
-
#
|
34
|
-
class CronLine
|
35
|
-
|
36
|
-
# The max number of years in the future or the past before giving up
|
37
|
-
# searching for #next_time or #previous_time respectively
|
38
|
-
#
|
39
|
-
NEXT_TIME_MAX_YEARS = 14
|
40
|
-
|
41
|
-
# The string used for creating this cronline instance.
|
42
|
-
#
|
43
|
-
attr_reader :original
|
44
|
-
attr_reader :original_timezone
|
45
|
-
|
46
|
-
attr_reader :seconds
|
47
|
-
attr_reader :minutes
|
48
|
-
attr_reader :hours
|
49
|
-
attr_reader :days
|
50
|
-
attr_reader :months
|
51
|
-
#attr_reader :monthdays # reader defined below
|
52
|
-
attr_reader :weekdays
|
53
|
-
attr_reader :timezone
|
54
|
-
|
55
|
-
def initialize(line)
|
56
|
-
|
57
|
-
fail ArgumentError.new(
|
58
|
-
"not a string: #{line.inspect}"
|
59
|
-
) unless line.is_a?(String)
|
60
|
-
|
61
|
-
@original = line
|
62
|
-
@original_timezone = nil
|
63
|
-
|
64
|
-
items = line.split
|
65
|
-
|
66
|
-
if @timezone = RocketJob::Plugins::Rufus::ZoTime.get_tzone(items.last)
|
67
|
-
@original_timezone = items.pop
|
68
|
-
else
|
69
|
-
@timezone = RocketJob::Plugins::Rufus::ZoTime.get_tzone(:current)
|
70
|
-
end
|
71
|
-
|
72
|
-
fail ArgumentError.new(
|
73
|
-
"not a valid cronline : '#{line}'"
|
74
|
-
) unless items.length == 5 or items.length == 6
|
75
|
-
|
76
|
-
offset = items.length - 5
|
77
|
-
|
78
|
-
@seconds = offset == 1 ? parse_item(items[0], 0, 59) : [ 0 ]
|
79
|
-
@minutes = parse_item(items[0 + offset], 0, 59)
|
80
|
-
@hours = parse_item(items[1 + offset], 0, 24)
|
81
|
-
@days = parse_item(items[2 + offset], -30, 31)
|
82
|
-
@months = parse_item(items[3 + offset], 1, 12)
|
83
|
-
@weekdays, @monthdays = parse_weekdays(items[4 + offset])
|
84
|
-
|
85
|
-
[ @seconds, @minutes, @hours, @months ].each do |es|
|
86
|
-
|
87
|
-
fail ArgumentError.new(
|
88
|
-
"invalid cronline: '#{line}'"
|
89
|
-
) if es && es.find { |e| ! e.is_a?(Integer) }
|
90
|
-
end
|
91
|
-
|
92
|
-
if @days && @days.include?(0) # gh-221
|
93
|
-
|
94
|
-
fail ArgumentError.new('invalid day 0 in cronline')
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
# Returns true if the given time matches this cron line.
|
99
|
-
#
|
100
|
-
def matches?(time)
|
101
|
-
|
102
|
-
# FIXME Don't create a new ZoTime if time is already a ZoTime in same
|
103
|
-
# zone ...
|
104
|
-
# Wait, this seems only used in specs...
|
105
|
-
t = ZoTime.new(time.to_f, @timezone)
|
106
|
-
|
107
|
-
return false unless sub_match?(t, :sec, @seconds)
|
108
|
-
return false unless sub_match?(t, :min, @minutes)
|
109
|
-
return false unless sub_match?(t, :hour, @hours)
|
110
|
-
return false unless date_match?(t)
|
111
|
-
true
|
112
|
-
end
|
113
|
-
|
114
|
-
# Returns the next time that this cron line is supposed to 'fire'
|
115
|
-
#
|
116
|
-
# This is raw, 3 secs to iterate over 1 year on my macbook :( brutal.
|
117
|
-
# (Well, I was wrong, takes 0.001 sec on 1.8.7 and 1.9.1)
|
118
|
-
#
|
119
|
-
# This method accepts an optional Time parameter. It's the starting point
|
120
|
-
# for the 'search'. By default, it's Time.now
|
121
|
-
#
|
122
|
-
# Note that the time instance returned will be in the same time zone that
|
123
|
-
# the given start point Time (thus a result in the local time zone will
|
124
|
-
# be passed if no start time is specified (search start time set to
|
125
|
-
# Time.now))
|
126
|
-
#
|
127
|
-
# Rufus::Scheduler::CronLine.new('30 7 * * *').next_time(
|
128
|
-
# Time.mktime(2008, 10, 24, 7, 29))
|
129
|
-
# #=> Fri Oct 24 07:30:00 -0500 2008
|
130
|
-
#
|
131
|
-
# Rufus::Scheduler::CronLine.new('30 7 * * *').next_time(
|
132
|
-
# Time.utc(2008, 10, 24, 7, 29))
|
133
|
-
# #=> Fri Oct 24 07:30:00 UTC 2008
|
134
|
-
#
|
135
|
-
# Rufus::Scheduler::CronLine.new('30 7 * * *').next_time(
|
136
|
-
# Time.utc(2008, 10, 24, 7, 29)).localtime
|
137
|
-
# #=> Fri Oct 24 02:30:00 -0500 2008
|
138
|
-
#
|
139
|
-
# (Thanks to K Liu for the note and the examples)
|
140
|
-
#
|
141
|
-
def next_time(from=ZoTime.now)
|
142
|
-
|
143
|
-
nt = nil
|
144
|
-
zt = ZoTime.new(from.to_i + 1, @timezone)
|
145
|
-
maxy = from.year + NEXT_TIME_MAX_YEARS
|
146
|
-
|
147
|
-
loop do
|
148
|
-
|
149
|
-
nt = zt.dup
|
150
|
-
|
151
|
-
fail RangeError.new(
|
152
|
-
"failed to reach occurrence within " +
|
153
|
-
"#{NEXT_TIME_MAX_YEARS} years for '#{original}'"
|
154
|
-
) if nt.year > maxy
|
155
|
-
|
156
|
-
unless date_match?(nt)
|
157
|
-
zt.add((24 - nt.hour) * 3600 - nt.min * 60 - nt.sec)
|
158
|
-
next
|
159
|
-
end
|
160
|
-
unless sub_match?(nt, :hour, @hours)
|
161
|
-
zt.add((60 - nt.min) * 60 - nt.sec)
|
162
|
-
next
|
163
|
-
end
|
164
|
-
unless sub_match?(nt, :min, @minutes)
|
165
|
-
zt.add(60 - nt.sec)
|
166
|
-
next
|
167
|
-
end
|
168
|
-
unless sub_match?(nt, :sec, @seconds)
|
169
|
-
zt.add(next_second(nt))
|
170
|
-
next
|
171
|
-
end
|
172
|
-
|
173
|
-
break
|
174
|
-
end
|
175
|
-
|
176
|
-
nt
|
177
|
-
end
|
178
|
-
|
179
|
-
# Returns the previous time the cronline matched. It's like next_time, but
|
180
|
-
# for the past.
|
181
|
-
#
|
182
|
-
def previous_time(from=ZoTime.now)
|
183
|
-
|
184
|
-
pt = nil
|
185
|
-
zt = ZoTime.new(from.to_i - 1, @timezone)
|
186
|
-
miny = from.year - NEXT_TIME_MAX_YEARS
|
187
|
-
|
188
|
-
loop do
|
189
|
-
|
190
|
-
pt = zt.dup
|
191
|
-
|
192
|
-
fail RangeError.new(
|
193
|
-
"failed to reach occurrence within " +
|
194
|
-
"#{NEXT_TIME_MAX_YEARS} years for '#{original}'"
|
195
|
-
) if pt.year < miny
|
196
|
-
|
197
|
-
unless date_match?(pt)
|
198
|
-
zt.substract(pt.hour * 3600 + pt.min * 60 + pt.sec + 1)
|
199
|
-
next
|
200
|
-
end
|
201
|
-
unless sub_match?(pt, :hour, @hours)
|
202
|
-
zt.substract(pt.min * 60 + pt.sec + 1)
|
203
|
-
next
|
204
|
-
end
|
205
|
-
unless sub_match?(pt, :min, @minutes)
|
206
|
-
zt.substract(pt.sec + 1)
|
207
|
-
next
|
208
|
-
end
|
209
|
-
unless sub_match?(pt, :sec, @seconds)
|
210
|
-
zt.substract(prev_second(pt))
|
211
|
-
next
|
212
|
-
end
|
213
|
-
|
214
|
-
break
|
215
|
-
end
|
216
|
-
|
217
|
-
pt
|
218
|
-
end
|
219
|
-
|
220
|
-
# Returns an array of 6 arrays (seconds, minutes, hours, days,
|
221
|
-
# months, weekdays).
|
222
|
-
# This method is mostly used by the cronline specs.
|
223
|
-
#
|
224
|
-
def to_a
|
225
|
-
|
226
|
-
[
|
227
|
-
toa(@seconds),
|
228
|
-
toa(@minutes),
|
229
|
-
toa(@hours),
|
230
|
-
toa(@days),
|
231
|
-
toa(@months),
|
232
|
-
toa(@weekdays),
|
233
|
-
toa(@monthdays),
|
234
|
-
@timezone.name
|
235
|
-
]
|
236
|
-
end
|
237
|
-
alias to_array to_a
|
238
|
-
|
239
|
-
# Returns a quickly computed approximation of the frequency for this
|
240
|
-
# cron line.
|
241
|
-
#
|
242
|
-
# #brute_frequency, on the other hand, will compute the frequency by
|
243
|
-
# examining a whole year, that can take more than seconds for a seconds
|
244
|
-
# level cron...
|
245
|
-
#
|
246
|
-
def frequency
|
247
|
-
|
248
|
-
return brute_frequency unless @seconds && @seconds.length > 1
|
249
|
-
|
250
|
-
secs = toa(@seconds)
|
251
|
-
|
252
|
-
secs[1..-1].inject([ secs[0], 60 ]) { |(prev, delta), sec|
|
253
|
-
d = sec - prev
|
254
|
-
[ sec, d < delta ? d : delta ]
|
255
|
-
}[1]
|
256
|
-
end
|
257
|
-
|
258
|
-
# Caching facility. Currently only used for brute frequencies.
|
259
|
-
#
|
260
|
-
@cache = {}; class << self; attr_reader :cache; end
|
261
|
-
|
262
|
-
# Returns the shortest delta between two potential occurences of the
|
263
|
-
# schedule described by this cronline.
|
264
|
-
#
|
265
|
-
# .
|
266
|
-
#
|
267
|
-
# For a simple cronline like "*/5 * * * *", obviously the frequency is
|
268
|
-
# five minutes. Why does this method look at a whole year of #next_time ?
|
269
|
-
#
|
270
|
-
# Consider "* * * * sun#2,sun#3", the computed frequency is 1 week
|
271
|
-
# (the shortest delta is the one between the second sunday and the third
|
272
|
-
# sunday). This method takes no chance and runs next_time for the span
|
273
|
-
# of a whole year and keeps the shortest.
|
274
|
-
#
|
275
|
-
# Of course, this method can get VERY slow if you call on it a second-
|
276
|
-
# based cronline...
|
277
|
-
#
|
278
|
-
def brute_frequency
|
279
|
-
|
280
|
-
key = "brute_frequency:#{@original}"
|
281
|
-
|
282
|
-
delta = self.class.cache[key]
|
283
|
-
return delta if delta
|
284
|
-
|
285
|
-
delta = 366 * DAY_S
|
286
|
-
|
287
|
-
t0 = previous_time(Time.local(2000, 1, 1))
|
288
|
-
|
289
|
-
loop do
|
290
|
-
|
291
|
-
break if delta <= 1
|
292
|
-
break if delta <= 60 && @seconds && @seconds.size == 1
|
293
|
-
|
294
|
-
t1 = next_time(t0)
|
295
|
-
d = t1 - t0
|
296
|
-
delta = d if d < delta
|
297
|
-
break if @months.nil? && t1.month == 2
|
298
|
-
break if @months.nil? && @days.nil? && t1.day == 2
|
299
|
-
break if @months.nil? && @days.nil? && @hours.nil? && t1.hour == 1
|
300
|
-
break if @months.nil? && @days.nil? && @hours.nil? && @minutes.nil? && t1.min == 1
|
301
|
-
break if t1.year >= 2001
|
302
|
-
|
303
|
-
t0 = t1
|
304
|
-
end
|
305
|
-
|
306
|
-
self.class.cache[key] = delta
|
307
|
-
end
|
308
|
-
|
309
|
-
def next_second(time)
|
310
|
-
|
311
|
-
secs = toa(@seconds)
|
312
|
-
|
313
|
-
return secs.first + 60 - time.sec if time.sec > secs.last
|
314
|
-
|
315
|
-
secs.shift while secs.first < time.sec
|
316
|
-
|
317
|
-
secs.first - time.sec
|
318
|
-
end
|
319
|
-
|
320
|
-
def prev_second(time)
|
321
|
-
|
322
|
-
secs = toa(@seconds)
|
323
|
-
|
324
|
-
return time.sec + 60 - secs.last if time.sec < secs.first
|
325
|
-
|
326
|
-
secs.pop while time.sec < secs.last
|
327
|
-
|
328
|
-
time.sec - secs.last
|
329
|
-
end
|
330
|
-
|
331
|
-
protected
|
332
|
-
|
333
|
-
def sc_sort(a)
|
334
|
-
|
335
|
-
a.sort_by { |e| e.is_a?(String) ? 61 : e.to_i }
|
336
|
-
end
|
337
|
-
|
338
|
-
if RUBY_VERSION >= '1.9'
|
339
|
-
def toa(item)
|
340
|
-
item == nil ? nil : item.to_a
|
341
|
-
end
|
342
|
-
else
|
343
|
-
def toa(item)
|
344
|
-
item.is_a?(Set) ? sc_sort(item.to_a) : item
|
345
|
-
end
|
346
|
-
end
|
347
|
-
|
348
|
-
WEEKDAYS = %w[ sun mon tue wed thu fri sat ]
|
349
|
-
DAY_S = 24 * 3600
|
350
|
-
|
351
|
-
def parse_weekdays(item)
|
352
|
-
|
353
|
-
return nil if item == '*'
|
354
|
-
|
355
|
-
weekdays = nil
|
356
|
-
monthdays = nil
|
357
|
-
|
358
|
-
item.downcase.split(',').each do |it|
|
359
|
-
|
360
|
-
WEEKDAYS.each_with_index { |a, i| it.gsub!(/#{a}/, i.to_s) }
|
361
|
-
|
362
|
-
it = it.gsub(/([^#])l/, '\1#-1')
|
363
|
-
# "5L" == "5#-1" == the last Friday
|
364
|
-
|
365
|
-
if m = it.match(/\A(.+)#(l|-?[12345])\z/)
|
366
|
-
|
367
|
-
fail ArgumentError.new(
|
368
|
-
"ranges are not supported for monthdays (#{it})"
|
369
|
-
) if m[1].index('-')
|
370
|
-
|
371
|
-
it = it.gsub(/#l/, '#-1')
|
372
|
-
|
373
|
-
(monthdays ||= []) << it
|
374
|
-
|
375
|
-
else
|
376
|
-
|
377
|
-
fail ArgumentError.new(
|
378
|
-
"invalid weekday expression (#{item})"
|
379
|
-
) if it !~ /\A0*[0-7](-0*[0-7])?\z/
|
380
|
-
|
381
|
-
its = it.index('-') ? parse_range(it, 0, 7) : [ Integer(it) ]
|
382
|
-
its = its.collect { |i| i == 7 ? 0 : i }
|
383
|
-
|
384
|
-
(weekdays ||= []).concat(its)
|
385
|
-
end
|
386
|
-
end
|
387
|
-
|
388
|
-
weekdays = weekdays.uniq.sort if weekdays
|
389
|
-
|
390
|
-
[ weekdays, monthdays ]
|
391
|
-
end
|
392
|
-
|
393
|
-
def parse_item(item, min, max)
|
394
|
-
|
395
|
-
return nil if item == '*'
|
396
|
-
|
397
|
-
r = item.split(',').map { |i| parse_range(i.strip, min, max) }.flatten
|
398
|
-
|
399
|
-
fail ArgumentError.new(
|
400
|
-
"found duplicates in #{item.inspect}"
|
401
|
-
) if r.uniq.size < r.size
|
402
|
-
|
403
|
-
r = sc_sort(r)
|
404
|
-
|
405
|
-
Set.new(r)
|
406
|
-
end
|
407
|
-
|
408
|
-
RANGE_REGEX = /\A(\*|-?\d{1,2})(?:-(-?\d{1,2}))?(?:\/(\d{1,2}))?\z/
|
409
|
-
|
410
|
-
def parse_range(item, min, max)
|
411
|
-
|
412
|
-
return %w[ L ] if item == 'L'
|
413
|
-
|
414
|
-
item = '*' + item if item[0, 1] == '/'
|
415
|
-
|
416
|
-
m = item.match(RANGE_REGEX)
|
417
|
-
|
418
|
-
fail ArgumentError.new(
|
419
|
-
"cannot parse #{item.inspect}"
|
420
|
-
) unless m
|
421
|
-
|
422
|
-
mmin = min == -30 ? 1 : min # days
|
423
|
-
|
424
|
-
sta = m[1]
|
425
|
-
sta = sta == '*' ? mmin : sta.to_i
|
426
|
-
|
427
|
-
edn = m[2]
|
428
|
-
edn = edn ? edn.to_i : sta
|
429
|
-
edn = max if m[1] == '*'
|
430
|
-
|
431
|
-
inc = m[3]
|
432
|
-
inc = inc ? inc.to_i : 1
|
433
|
-
|
434
|
-
fail ArgumentError.new(
|
435
|
-
"#{item.inspect} positive/negative ranges not allowed"
|
436
|
-
) if (sta < 0 && edn > 0) || (sta > 0 && edn < 0)
|
437
|
-
|
438
|
-
fail ArgumentError.new(
|
439
|
-
"#{item.inspect} descending day ranges not allowed"
|
440
|
-
) if min == -30 && sta > edn
|
441
|
-
|
442
|
-
fail ArgumentError.new(
|
443
|
-
"#{item.inspect} is not in range #{min}..#{max}"
|
444
|
-
) if sta < min || edn > max
|
445
|
-
|
446
|
-
fail ArgumentError.new(
|
447
|
-
"#{item.inspect} increment must be greater than zero"
|
448
|
-
) if inc == 0
|
449
|
-
|
450
|
-
r = []
|
451
|
-
val = sta
|
452
|
-
|
453
|
-
loop do
|
454
|
-
v = val
|
455
|
-
v = 0 if max == 24 && v == 24 # hours
|
456
|
-
r << v
|
457
|
-
break if inc == 1 && val == edn
|
458
|
-
val += inc
|
459
|
-
break if inc > 1 && val > edn
|
460
|
-
val = min if val > max
|
461
|
-
end
|
462
|
-
|
463
|
-
r.uniq
|
464
|
-
end
|
465
|
-
|
466
|
-
# FIXME: Eventually split into day_match?, hour_match? and monthdays_match?o
|
467
|
-
#
|
468
|
-
def sub_match?(time, accessor, values)
|
469
|
-
|
470
|
-
return true if values.nil?
|
471
|
-
|
472
|
-
value = time.send(accessor)
|
473
|
-
|
474
|
-
if accessor == :day
|
475
|
-
|
476
|
-
values.each do |v|
|
477
|
-
return true if v == 'L' && (time + DAY_S).day == 1
|
478
|
-
return true if v.to_i < 0 && (time + (1 - v) * DAY_S).day == 1
|
479
|
-
end
|
480
|
-
end
|
481
|
-
|
482
|
-
if accessor == :hour
|
483
|
-
|
484
|
-
return true if value == 0 && values.include?(24)
|
485
|
-
end
|
486
|
-
|
487
|
-
if accessor == :monthdays
|
488
|
-
|
489
|
-
return true if (values & value).any?
|
490
|
-
end
|
491
|
-
|
492
|
-
values.include?(value)
|
493
|
-
end
|
494
|
-
|
495
|
-
# def monthday_match?(zt, values)
|
496
|
-
#
|
497
|
-
# return true if values.nil?
|
498
|
-
#
|
499
|
-
# today_values = monthdays(zt)
|
500
|
-
#
|
501
|
-
# (today_values & values).any?
|
502
|
-
# end
|
503
|
-
|
504
|
-
def date_match?(zt)
|
505
|
-
|
506
|
-
return false unless sub_match?(zt, :day, @days)
|
507
|
-
return false unless sub_match?(zt, :month, @months)
|
508
|
-
|
509
|
-
return true if (
|
510
|
-
(@weekdays && @monthdays) &&
|
511
|
-
(sub_match?(zt, :wday, @weekdays) ||
|
512
|
-
sub_match?(zt, :monthdays, @monthdays)))
|
513
|
-
|
514
|
-
return false unless sub_match?(zt, :wday, @weekdays)
|
515
|
-
return false unless sub_match?(zt, :monthdays, @monthdays)
|
516
|
-
|
517
|
-
true
|
518
|
-
end
|
519
|
-
end
|
520
|
-
end
|