vrinek-periodicity 0.1.1 → 0.2
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.
- data/README.rdoc +1 -3
- data/lib/periodicity/period.rb +36 -45
- data/lib/periodicity.rb +1 -1
- data/spec/periodicity_spec.rb +80 -29
- metadata +1 -1
data/README.rdoc
CHANGED
|
@@ -12,7 +12,6 @@ Helps calculate the next run for schedulers using a human readable syntax.
|
|
|
12
12
|
means: every 2 hours at :20 from 10:00 to 15:00 (10:20, 12:20, 14:20)
|
|
13
13
|
|
|
14
14
|
period.next_run # returns the next calculated time as a Time object
|
|
15
|
-
period.next_run(1) # skips 1 run (as of 0.1, it behaves buggy with from and to limits)
|
|
16
15
|
|
|
17
16
|
Period.new.every(:half).hour # every 30 minutes
|
|
18
17
|
Period.every.week # every week
|
|
@@ -29,5 +28,4 @@ means: every 2 hours at :20 from 10:00 to 15:00 (10:20, 12:20, 14:20)
|
|
|
29
28
|
|
|
30
29
|
== TODO:
|
|
31
30
|
|
|
32
|
-
*
|
|
33
|
-
Period.every.hour.from(20).to(2)
|
|
31
|
+
* add <em>sub-sub-time</em> precision to at(time) (e.g. '5:30')
|
data/lib/periodicity/period.rb
CHANGED
|
@@ -92,8 +92,6 @@ class Period
|
|
|
92
92
|
every(2).hours.from(8) # means "every 2 hours beginning from 8:00"
|
|
93
93
|
=end
|
|
94
94
|
def from(time)
|
|
95
|
-
raise "From can't be after to" if @to and @to < time
|
|
96
|
-
|
|
97
95
|
@from = time
|
|
98
96
|
return self
|
|
99
97
|
end
|
|
@@ -103,8 +101,6 @@ class Period
|
|
|
103
101
|
every(2).hours.to(12) # means "every 2 hours until 12:00"
|
|
104
102
|
=end
|
|
105
103
|
def to(time)
|
|
106
|
-
raise "To can't be before from" if @from and time < @from
|
|
107
|
-
|
|
108
104
|
@to = time
|
|
109
105
|
return self
|
|
110
106
|
end
|
|
@@ -139,9 +135,9 @@ class Period
|
|
|
139
135
|
now = "Aug 05 14:40:23 2009".to_time
|
|
140
136
|
Period.new(now).every(2).hours.next_run # => Wed Aug 05 16:00:00 UTC 2009
|
|
141
137
|
|
|
142
|
-
Note that it
|
|
138
|
+
Note that it rounds all "sub-time" to 0 by default (can be overriden with at)
|
|
143
139
|
=end
|
|
144
|
-
def next_run
|
|
140
|
+
def next_run
|
|
145
141
|
return Time.now unless @last_run
|
|
146
142
|
|
|
147
143
|
@next_run = @last_run
|
|
@@ -152,12 +148,6 @@ class Period
|
|
|
152
148
|
calc_precision
|
|
153
149
|
|
|
154
150
|
calc_limits
|
|
155
|
-
|
|
156
|
-
unless skip.zero?
|
|
157
|
-
@next_run += @interval * @scope * skip
|
|
158
|
-
|
|
159
|
-
calc_limits
|
|
160
|
-
end
|
|
161
151
|
else
|
|
162
152
|
raise 'No proper period specified'
|
|
163
153
|
end
|
|
@@ -186,43 +176,31 @@ class Period
|
|
|
186
176
|
|
|
187
177
|
def calc_precision(scope = nil)
|
|
188
178
|
@at = 0 if scope
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
@next_run += (@at - @next_run.sec)
|
|
194
|
-
end
|
|
195
|
-
when 1.hour
|
|
196
|
-
unless @next_run.min == @at
|
|
197
|
-
@next_run += (@at - @next_run.min).minutes
|
|
198
|
-
end
|
|
199
|
-
calc_precision 1.minute
|
|
200
|
-
when 1.day
|
|
201
|
-
unless @next_run.hour == @at
|
|
202
|
-
@next_run += (@at - @next_run.hour).hours
|
|
203
|
-
end
|
|
204
|
-
calc_precision 1.hour
|
|
205
|
-
when 1.week
|
|
206
|
-
unless @next_run.hour == @at
|
|
207
|
-
@next_run += (@at - @next_run.hour).hours
|
|
179
|
+
|
|
180
|
+
if down = downtime(scope || @scope)
|
|
181
|
+
unless now(down) == @at
|
|
182
|
+
@next_run += (@at - now(down)) * down
|
|
208
183
|
end
|
|
209
|
-
calc_precision
|
|
184
|
+
calc_precision down
|
|
210
185
|
end
|
|
211
186
|
end
|
|
212
187
|
|
|
213
188
|
def calc_limits
|
|
214
|
-
if @from
|
|
215
|
-
now
|
|
216
|
-
|
|
217
|
-
@
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
@next_run.day
|
|
189
|
+
if @from and @to and @from > @to # aka overnight limits
|
|
190
|
+
if now < @from
|
|
191
|
+
@next_run += (
|
|
192
|
+
if @to < now + (now(downtime)*(downtime / @scope.to_f) || 0)
|
|
193
|
+
@from - now
|
|
194
|
+
else
|
|
195
|
+
0
|
|
196
|
+
end
|
|
197
|
+
) * @scope
|
|
224
198
|
end
|
|
225
|
-
|
|
199
|
+
|
|
200
|
+
if now > @to and @from < @to
|
|
201
|
+
@next_run += (@from - now) * @scope + uptime
|
|
202
|
+
end
|
|
203
|
+
elsif @from or @to
|
|
226
204
|
if @from and now < @from
|
|
227
205
|
@next_run += (@from - now) * @scope
|
|
228
206
|
elsif @to and now > @to
|
|
@@ -231,6 +209,19 @@ class Period
|
|
|
231
209
|
end
|
|
232
210
|
end
|
|
233
211
|
|
|
212
|
+
def now(scope = nil)
|
|
213
|
+
case scope || @scope
|
|
214
|
+
when 1.second
|
|
215
|
+
@next_run.sec
|
|
216
|
+
when 1.minute
|
|
217
|
+
@next_run.min
|
|
218
|
+
when 1.hour
|
|
219
|
+
@next_run.hour
|
|
220
|
+
when 1.day
|
|
221
|
+
@next_run.day
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
234
225
|
def uptime
|
|
235
226
|
case @scope
|
|
236
227
|
when 1.second
|
|
@@ -246,8 +237,8 @@ class Period
|
|
|
246
237
|
end
|
|
247
238
|
end
|
|
248
239
|
|
|
249
|
-
def downtime
|
|
250
|
-
case @scope
|
|
240
|
+
def downtime(scope = nil)
|
|
241
|
+
case scope || @scope
|
|
251
242
|
when 1.minute
|
|
252
243
|
1.second
|
|
253
244
|
when 1.hour
|
data/lib/periodicity.rb
CHANGED
data/spec/periodicity_spec.rb
CHANGED
|
@@ -28,9 +28,9 @@ describe Period do
|
|
|
28
28
|
period.every.day.at(:noon).next_run.should == "Aug 06 12:00:00 2009".to_time
|
|
29
29
|
period.every.day.at(:afternoon).next_run.should == "Aug 06 19:00:00 2009".to_time
|
|
30
30
|
|
|
31
|
-
period.every.day.at(:midnight).next_run.should == "Aug 06 00:00:00 2009".to_time
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
(n = period.every.day.at(:midnight).next_run).should == "Aug 06 00:00:00 2009".to_time
|
|
32
|
+
(n = Period.new(n).every.day.at(:midnight).next_run).should == "Aug 07 00:00:00 2009".to_time
|
|
33
|
+
(n = Period.new(n).every.day.at(:midnight).next_run).should == "Aug 08 00:00:00 2009".to_time
|
|
34
34
|
|
|
35
35
|
period.every.day.at('5').next_run.should == "Aug 06 05:00:00 2009".to_time
|
|
36
36
|
period.every.day.at('5:00').next_run.should == "Aug 06 05:00:00 2009".to_time
|
|
@@ -43,9 +43,9 @@ describe Period do
|
|
|
43
43
|
period.every.week.at(20).next_run.should == "Aug 12 20:00:00 2009".to_time
|
|
44
44
|
period.every.day.at(14).next_run.should == "Aug 06 14:00:00 2009".to_time
|
|
45
45
|
|
|
46
|
-
period.every.hour.at(30).next_run.should == "Aug 05 15:30:00 2009".to_time
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
(n = period.every.hour.at(30).next_run).should == "Aug 05 15:30:00 2009".to_time
|
|
47
|
+
(n = Period.new(n).every.hour.at(30).next_run).should == "Aug 05 16:30:00 2009".to_time
|
|
48
|
+
(n = Period.new(n).every.hour.at(30).next_run).should == "Aug 05 17:30:00 2009".to_time
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
it "should take into account the limits" do
|
|
@@ -53,39 +53,90 @@ describe Period do
|
|
|
53
53
|
period.every.hour.from(13).next_run.should == "Aug 05 15:00:00 2009".to_time
|
|
54
54
|
period.every(2).hours.from(12).next_run.should == "Aug 05 16:00:00 2009".to_time
|
|
55
55
|
|
|
56
|
-
period.every(2).hours.from(21).next_run.should == "Aug 05 21:00:00 2009".to_time
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
(n = period.every(2).hours.from(21).next_run).should == "Aug 05 21:00:00 2009".to_time
|
|
57
|
+
(n = Period.new(n).every(2).hours.from(21).next_run).should == "Aug 05 23:00:00 2009".to_time
|
|
58
|
+
(n = Period.new(n).every(2).hours.from(21).next_run).should == "Aug 06 21:00:00 2009".to_time
|
|
59
59
|
|
|
60
|
-
period.every(2).hours.to(12).next_run.should == "Aug 06 00:00:00 2009".to_time
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
(n = period.every(2).hours.to(12).next_run).should == "Aug 06 00:00:00 2009".to_time
|
|
61
|
+
(n = Period.new(n).every(2).hours.to(12).next_run).should == "Aug 06 02:00:00 2009".to_time
|
|
62
|
+
(n = Period.new(n).every(2).hours.to(12).next_run).should == "Aug 06 04:00:00 2009".to_time
|
|
63
63
|
|
|
64
|
-
period.every(5).minutes.from(15).to(50).next_run
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
64
|
+
(n = period.every(5).minutes.from(15).to(50).next_run).should == "Aug 05 14:45:00 2009".to_time
|
|
65
|
+
(n = Period.new(n).every(5).minutes.from(15).to(50).next_run).should == "Aug 05 14:50:00 2009".to_time
|
|
66
|
+
(n = Period.new(n).every(5).minutes.from(15).to(50).next_run).should == "Aug 05 15:15:00 2009".to_time
|
|
67
|
+
(n = Period.new(n).every(5).minutes.from(15).to(50).next_run).should == "Aug 05 15:20:00 2009".to_time
|
|
68
68
|
|
|
69
|
-
period.every(2).hours.from(8).to(12).next_run
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
69
|
+
(n = period.every(2).hours.from(8).to(12).next_run).should == "Aug 06 08:00:00 2009".to_time
|
|
70
|
+
(n = Period.new(n).every(2).hours.from(8).to(12).next_run).should == "Aug 06 10:00:00 2009".to_time
|
|
71
|
+
(n = Period.new(n).every(2).hours.from(8).to(12).next_run).should == "Aug 06 12:00:00 2009".to_time
|
|
72
|
+
(n = Period.new(n).every(2).hours.from(8).to(12).next_run).should == "Aug 07 08:00:00 2009".to_time
|
|
73
|
+
(n = Period.new(n).every(2).hours.from(8).to(12).next_run).should == "Aug 07 10:00:00 2009".to_time
|
|
74
74
|
|
|
75
|
-
period.every(3).days.from(5).to(20).next_run
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
75
|
+
(n = period.every(3).days.from(5).to(20).next_run).should == "Aug 08 00:00:00 2009".to_time
|
|
76
|
+
(n = Period.new(n).every(3).days.from(5).to(20).next_run).should == "Aug 11 00:00:00 2009".to_time
|
|
77
|
+
(n = Period.new(n).every(3).days.from(5).to(20).next_run).should == "Aug 14 00:00:00 2009".to_time
|
|
78
|
+
(n = Period.new(n).every(3).days.from(5).to(20).next_run).should == "Aug 17 00:00:00 2009".to_time
|
|
79
|
+
(n = Period.new(n).every(3).days.from(5).to(20).next_run).should == "Aug 20 00:00:00 2009".to_time
|
|
80
|
+
(n = Period.new(n).every(3).days.from(5).to(20).next_run).should == "Sep 05 00:00:00 2009".to_time
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it "should calculate overnight limits correctly" do
|
|
84
|
+
(n = period.every.hour.from(21).to(2).next_run).should == "Aug 05 21:00:00 2009".to_time
|
|
85
|
+
(n = Period.new(n).every.hour.from(21).to(2).next_run).should == "Aug 05 22:00:00 2009".to_time
|
|
86
|
+
(n = Period.new(n).every.hour.from(21).to(2).next_run).should == "Aug 05 23:00:00 2009".to_time
|
|
87
|
+
(n = Period.new(n).every.hour.from(21).to(2).next_run).should == "Aug 06 00:00:00 2009".to_time
|
|
88
|
+
(n = Period.new(n).every.hour.from(21).to(2).next_run).should == "Aug 06 01:00:00 2009".to_time
|
|
89
|
+
(n = Period.new(n).every.hour.from(21).to(2).next_run).should == "Aug 06 02:00:00 2009".to_time
|
|
90
|
+
(n = Period.new(n).every.hour.from(21).to(2).next_run).should == "Aug 06 21:00:00 2009".to_time
|
|
91
|
+
(n = Period.new(n).every.hour.from(21).to(2).next_run).should == "Aug 06 22:00:00 2009".to_time
|
|
92
|
+
(n = Period.new(n).every.hour.from(21).to(2).next_run).should == "Aug 06 23:00:00 2009".to_time
|
|
93
|
+
(n = Period.new(n).every.hour.from(21).to(2).next_run).should == "Aug 07 00:00:00 2009".to_time
|
|
94
|
+
(n = Period.new(n).every.hour.from(21).to(2).next_run).should == "Aug 07 01:00:00 2009".to_time
|
|
95
|
+
(n = Period.new(n).every.hour.from(21).to(2).next_run).should == "Aug 07 02:00:00 2009".to_time
|
|
96
|
+
(n = Period.new(n).every.hour.from(21).to(2).next_run).should == "Aug 07 21:00:00 2009".to_time
|
|
97
|
+
|
|
98
|
+
(n = period.every.day.from(29).to(2).next_run).should == "Aug 29 00:00:00 2009".to_time
|
|
99
|
+
(n = Period.new(n).every.day.from(29).to(2).next_run).should == "Aug 30 00:00:00 2009".to_time
|
|
100
|
+
(n = Period.new(n).every.day.from(29).to(2).next_run).should == "Aug 31 00:00:00 2009".to_time
|
|
101
|
+
(n = Period.new(n).every.day.from(29).to(2).next_run).should == "Sep 01 00:00:00 2009".to_time
|
|
102
|
+
|
|
103
|
+
(n = period.every(3).days.from(29).to(10).next_run).should == "Aug 08 00:00:00 2009".to_time
|
|
104
|
+
(n = Period.new(n).every(3).days.from(29).to(10).next_run).should == "Aug 29 00:00:00 2009".to_time
|
|
105
|
+
(n = Period.new(n).every(3).days.from(29).to(10).next_run).should == "Sep 01 00:00:00 2009".to_time
|
|
106
|
+
(n = Period.new(n).every(3).days.from(29).to(10).next_run).should == "Sep 04 00:00:00 2009".to_time
|
|
107
|
+
(n = Period.new(n).every(3).days.from(29).to(10).next_run).should == "Sep 07 00:00:00 2009".to_time
|
|
108
|
+
(n = Period.new(n).every(3).days.from(29).to(10).next_run).should == "Sep 10 00:00:00 2009".to_time
|
|
109
|
+
(n = Period.new(n).every(3).days.from(29).to(10).next_run).should == "Sep 29 00:00:00 2009".to_time
|
|
110
|
+
(n = Period.new(n).every(3).days.from(29).to(10).next_run).should == "Oct 02 00:00:00 2009".to_time
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it "should calculate overnight with precision limits correctly" do
|
|
114
|
+
(n = period.every.hour.at(15).from(21).to(2).next_run).should == "Aug 05 21:15:00 2009".to_time
|
|
115
|
+
(n = Period.new(n).every.hour.at(15).from(21).to(2).next_run).should == "Aug 05 22:15:00 2009".to_time
|
|
116
|
+
(n = Period.new(n).every.hour.at(15).from(21).to(2).next_run).should == "Aug 05 23:15:00 2009".to_time
|
|
117
|
+
(n = Period.new(n).every.hour.at(15).from(21).to(2).next_run).should == "Aug 06 00:15:00 2009".to_time
|
|
118
|
+
(n = Period.new(n).every.hour.at(15).from(21).to(2).next_run).should == "Aug 06 01:15:00 2009".to_time
|
|
119
|
+
(n = Period.new(n).every.hour.at(15).from(21).to(2).next_run).should == "Aug 06 21:15:00 2009".to_time
|
|
120
|
+
(n = Period.new(n).every.hour.at(15).from(21).to(2).next_run).should == "Aug 06 22:15:00 2009".to_time
|
|
121
|
+
(n = Period.new(n).every.hour.at(15).from(21).to(2).next_run).should == "Aug 06 23:15:00 2009".to_time
|
|
122
|
+
(n = Period.new(n).every.hour.at(15).from(21).to(2).next_run).should == "Aug 07 00:15:00 2009".to_time
|
|
123
|
+
|
|
124
|
+
(n = period.every(5).minutes.at(30).from(40).to(10).next_run).should == "Aug 05 14:45:30 2009".to_time
|
|
125
|
+
(n = Period.new(n).every.every(5).minutes.at(30).from(40).to(10).next_run).should == "Aug 05 14:50:30 2009".to_time
|
|
126
|
+
(n = Period.new(n).every.every(5).minutes.at(30).from(40).to(10).next_run).should == "Aug 05 14:55:30 2009".to_time
|
|
127
|
+
(n = Period.new(n).every.every(5).minutes.at(30).from(40).to(10).next_run).should == "Aug 05 15:00:30 2009".to_time
|
|
128
|
+
(n = Period.new(n).every.every(5).minutes.at(30).from(40).to(10).next_run).should == "Aug 05 15:05:30 2009".to_time
|
|
129
|
+
(n = Period.new(n).every.every(5).minutes.at(30).from(40).to(10).next_run).should == "Aug 05 15:40:30 2009".to_time
|
|
130
|
+
(n = Period.new(n).every.every(5).minutes.at(30).from(40).to(10).next_run).should == "Aug 05 15:45:30 2009".to_time
|
|
131
|
+
(n = Period.new(n).every.every(5).minutes.at(30).from(40).to(10).next_run).should == "Aug 05 15:50:30 2009".to_time
|
|
81
132
|
end
|
|
82
133
|
|
|
83
134
|
it "should avoid some pitfalls" do
|
|
84
135
|
Period.new("Aug 05 14:41:23 2009".to_time).every.hour.at(40).next_run.should == "Aug 05 15:40:00 2009".to_time
|
|
85
136
|
Period.new("Aug 05 14:39:23 2009".to_time).every.hour.at(40).next_run.should == "Aug 05 15:40:00 2009".to_time
|
|
86
137
|
|
|
87
|
-
lambda {period.from(5).to(1)}.should raise_error
|
|
88
|
-
lambda {period.to(1).from(5)}.should raise_error
|
|
138
|
+
# lambda {period.from(5).to(1)}.should raise_error
|
|
139
|
+
# lambda {period.to(1).from(5)}.should raise_error
|
|
89
140
|
end
|
|
90
141
|
|
|
91
142
|
def period
|