rufus-scheduler 3.1.4 → 3.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +410 -0
  3. data/CREDITS.md +142 -0
  4. data/LICENSE.txt +1 -1
  5. data/Makefile +27 -0
  6. data/README.md +407 -143
  7. data/lib/rufus/scheduler/job_array.rb +36 -66
  8. data/lib/rufus/scheduler/jobs_core.rb +369 -0
  9. data/lib/rufus/scheduler/jobs_one_time.rb +53 -0
  10. data/lib/rufus/scheduler/jobs_repeat.rb +335 -0
  11. data/lib/rufus/scheduler/locks.rb +41 -67
  12. data/lib/rufus/scheduler/util.rb +89 -179
  13. data/lib/rufus/scheduler.rb +545 -453
  14. data/rufus-scheduler.gemspec +22 -11
  15. metadata +44 -85
  16. data/CHANGELOG.txt +0 -243
  17. data/CREDITS.txt +0 -88
  18. data/Rakefile +0 -83
  19. data/TODO.txt +0 -151
  20. data/lib/rufus/scheduler/cronline.rb +0 -470
  21. data/lib/rufus/scheduler/jobs.rb +0 -633
  22. data/lib/rufus/scheduler/zones.rb +0 -174
  23. data/lib/rufus/scheduler/zotime.rb +0 -155
  24. data/spec/basics_spec.rb +0 -54
  25. data/spec/cronline_spec.rb +0 -915
  26. data/spec/error_spec.rb +0 -139
  27. data/spec/job_array_spec.rb +0 -39
  28. data/spec/job_at_spec.rb +0 -58
  29. data/spec/job_cron_spec.rb +0 -128
  30. data/spec/job_every_spec.rb +0 -104
  31. data/spec/job_in_spec.rb +0 -20
  32. data/spec/job_interval_spec.rb +0 -68
  33. data/spec/job_repeat_spec.rb +0 -357
  34. data/spec/job_spec.rb +0 -631
  35. data/spec/lock_custom_spec.rb +0 -47
  36. data/spec/lock_flock_spec.rb +0 -47
  37. data/spec/lock_lockfile_spec.rb +0 -61
  38. data/spec/lock_spec.rb +0 -59
  39. data/spec/parse_spec.rb +0 -263
  40. data/spec/schedule_at_spec.rb +0 -158
  41. data/spec/schedule_cron_spec.rb +0 -66
  42. data/spec/schedule_every_spec.rb +0 -109
  43. data/spec/schedule_in_spec.rb +0 -80
  44. data/spec/schedule_interval_spec.rb +0 -128
  45. data/spec/scheduler_spec.rb +0 -1067
  46. data/spec/spec_helper.rb +0 -126
  47. data/spec/threads_spec.rb +0 -96
  48. data/spec/zotime_spec.rb +0 -396
@@ -1,101 +1,56 @@
1
- #--
2
- # Copyright (c) 2006-2015, 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
-
25
-
26
- module Rufus
27
-
28
- class Scheduler
1
+
2
+ class Rufus::Scheduler
3
+
4
+ class << self
29
5
 
30
6
  #--
31
7
  # time and string methods
32
8
  #++
33
9
 
34
- def self.parse(o, opts={})
10
+ def parse(o, opts={})
35
11
 
36
12
  opts[:no_error] = true
37
13
 
38
14
  parse_cron(o, opts) ||
39
15
  parse_in(o, opts) || # covers 'every' schedule strings
40
16
  parse_at(o, opts) ||
41
- raise(ArgumentError.new("couldn't parse \"#{o}\""))
17
+ fail(ArgumentError.new("couldn't parse #{o.inspect} (#{o.class})"))
42
18
  end
43
19
 
44
- def self.parse_in(o, opts={})
20
+ def parse_cron(o, opts={})
45
21
 
46
- o.is_a?(String) ? parse_duration(o, opts) : o
22
+ opts[:no_error] ?
23
+ Fugit.parse_cron(o) :
24
+ Fugit.do_parse_cron(o)
47
25
  end
48
26
 
49
- def self.parse_at(o, opts={})
50
-
51
- Rufus::Scheduler::ZoTime.parse(o, opts).time
27
+ def parse_in(o, opts={})
52
28
 
53
- rescue StandardError => se
29
+ #o.is_a?(String) ? parse_duration(o, opts) : o
54
30
 
55
- return nil if opts[:no_error]
56
- raise se
57
- end
31
+ return parse_duration(o, opts) if o.is_a?(String)
32
+ return o if o.is_a?(Numeric)
58
33
 
59
- def self.parse_cron(o, opts)
60
-
61
- CronLine.new(o)
34
+ fail ArgumentError.new("couldn't parse time point in #{o.inspect}")
62
35
 
63
36
  rescue ArgumentError => ae
64
37
 
65
38
  return nil if opts[:no_error]
66
- raise ae
39
+ fail ae
67
40
  end
68
41
 
69
- def self.parse_to_time(o)
42
+ def parse_at(o, opts={})
70
43
 
71
- t = o
72
- t = parse(t) if t.is_a?(String)
73
- t = Time.now + t if t.is_a?(Numeric)
44
+ return o if o.is_a?(EoTime)
45
+ return EoTime.make(o) if o.is_a?(Time)
46
+ EoTime.parse(o, opts)
74
47
 
75
- raise ArgumentError.new(
76
- "cannot turn #{o.inspect} to a point in time, doesn't make sense"
77
- ) unless t.is_a?(Time)
48
+ rescue StandardError => se
78
49
 
79
- t
50
+ return nil if opts[:no_error]
51
+ fail se
80
52
  end
81
53
 
82
- DURATIONS2M = [
83
- [ 'y', 365 * 24 * 3600 ],
84
- [ 'M', 30 * 24 * 3600 ],
85
- [ 'w', 7 * 24 * 3600 ],
86
- [ 'd', 24 * 3600 ],
87
- [ 'h', 3600 ],
88
- [ 'm', 60 ],
89
- [ 's', 1 ]
90
- ]
91
- DURATIONS2 = DURATIONS2M.dup
92
- DURATIONS2.delete_at(1)
93
-
94
- DURATIONS = DURATIONS2M.inject({}) { |r, (k, v)| r[k] = v; r }
95
- DURATION_LETTERS = DURATIONS.keys.join
96
-
97
- DU_KEYS = DURATIONS2M.collect { |k, v| k.to_sym }
98
-
99
54
  # Turns a string like '1m10s' into a float like '70.0', more formally,
100
55
  # turns a time duration expressed as a string into a Float instance
101
56
  # (millisecond count).
@@ -123,53 +78,17 @@ module Rufus
123
78
  # Rufus::Scheduler.parse_duration "-0.5" # => -0.5
124
79
  # Rufus::Scheduler.parse_duration "-1h" # => -3600.0
125
80
  #
126
- def self.parse_duration(string, opts={})
127
-
128
- string = string.to_s
129
-
130
- return 0.0 if string == ''
131
-
132
- m = string.match(/^(-?)([\d\.#{DURATION_LETTERS}]+)$/)
133
-
134
- return nil if m.nil? && opts[:no_error]
135
- raise ArgumentError.new("cannot parse '#{string}'") if m.nil?
136
-
137
- mod = m[1] == '-' ? -1.0 : 1.0
138
- val = 0.0
139
-
140
- s = m[2]
141
-
142
- while s.length > 0
143
- m = nil
144
- if m = s.match(/^(\d+|\d+\.\d*|\d*\.\d+)([#{DURATION_LETTERS}])(.*)$/)
145
- val += m[1].to_f * DURATIONS[m[2]]
146
- elsif s.match(/^\d+$/)
147
- val += s.to_i
148
- elsif s.match(/^\d*\.\d*$/)
149
- val += s.to_f
150
- elsif opts[:no_error]
151
- return nil
152
- else
153
- raise ArgumentError.new(
154
- "cannot parse '#{string}' (especially '#{s}')"
155
- )
156
- end
157
- break unless m && m[3]
158
- s = m[3]
159
- end
160
-
161
- mod * val
162
- end
81
+ def parse_duration(str, opts={})
163
82
 
164
- class << self
165
- #-
166
- # for compatibility with rufus-scheduler 2.x
167
- #+
168
- alias parse_duration_string parse_duration
169
- alias parse_time_string parse_duration
83
+ d =
84
+ opts[:no_error] ?
85
+ Fugit::Duration.parse(str, opts) :
86
+ Fugit::Duration.do_parse(str, opts)
87
+ d ?
88
+ d.to_sec :
89
+ nil
170
90
  end
171
91
 
172
-
173
92
  # Turns a number of seconds into a a time string
174
93
  #
175
94
  # Rufus.to_duration 0 # => '0s'
@@ -199,45 +118,27 @@ module Rufus
199
118
  #
200
119
  # * :months, if set to true, months (M) of 30 days will be taken into
201
120
  # account when building up the result
202
- # * :drop_seconds, if set to true, seconds and milliseconds will be trimmed
203
- # from the result
121
+ # * :drop_seconds, if set to true, seconds and milliseconds will be
122
+ # trimmed from the result
204
123
  #
205
- def self.to_duration(seconds, options={})
206
-
207
- h = to_duration_hash(seconds, options)
124
+ def to_duration(seconds, options={})
208
125
 
209
- return (options[:drop_seconds] ? '0m' : '0s') if h.empty?
126
+ #d = Fugit::Duration.parse(seconds, options).deflate
127
+ #d = d.drop_seconds if options[:drop_seconds]
128
+ #d = d.deflate(:month => options[:months]) if options[:months]
129
+ #d.to_rufus_s
210
130
 
211
- s =
212
- DU_KEYS.inject('') { |r, key|
213
- count = h[key]
214
- count = nil if count == 0
215
- r << "#{count}#{key}" if count
216
- r
217
- }
218
-
219
- ms = h[:ms]
220
- s << ms.to_s if ms
221
-
222
- s
223
- end
224
-
225
- class << self
226
- #-
227
- # for compatibility with rufus-scheduler 2.x
228
- #+
229
- alias to_duration_string to_duration
230
- alias to_time_string to_duration
131
+ to_fugit_duration(seconds, options).to_rufus_s
231
132
  end
232
133
 
233
134
  # Turns a number of seconds (integer or Float) into a hash like in :
234
135
  #
235
136
  # Rufus.to_duration_hash 0.051
236
- # # => { :ms => "51" }
137
+ # # => { :s => 0.051 }
237
138
  # Rufus.to_duration_hash 7.051
238
- # # => { :s => 7, :ms => "51" }
139
+ # # => { :s => 7.051 }
239
140
  # Rufus.to_duration_hash 0.120 + 30 * 24 * 3600 + 1
240
- # # => { :w => 4, :d => 2, :s => 1, :ms => "120" }
141
+ # # => { :w => 4, :d => 2, :s => 1.120 }
241
142
  #
242
143
  # This method is used by to_duration behind the scenes.
243
144
  #
@@ -245,62 +146,71 @@ module Rufus
245
146
  #
246
147
  # * :months, if set to true, months (M) of 30 days will be taken into
247
148
  # account when building up the result
248
- # * :drop_seconds, if set to true, seconds and milliseconds will be trimmed
249
- # from the result
149
+ # * :drop_seconds, if set to true, seconds and milliseconds will be
150
+ # trimmed from the result
250
151
  #
251
- def self.to_duration_hash(seconds, options={})
252
-
253
- h = {}
152
+ def to_duration_hash(seconds, options={})
254
153
 
255
- if seconds.is_a?(Float)
256
- h[:ms] = (seconds % 1 * 1000).to_i
257
- seconds = seconds.to_i
258
- end
259
-
260
- if options[:drop_seconds]
261
- h.delete(:ms)
262
- seconds = (seconds - seconds % 60)
263
- end
154
+ to_fugit_duration(seconds, options).to_rufus_h
155
+ end
264
156
 
265
- durations = options[:months] ? DURATIONS2M : DURATIONS2
157
+ # Used by both .to_duration and .to_duration_hash
158
+ #
159
+ def to_fugit_duration(seconds, options={})
266
160
 
267
- durations.each do |key, duration|
161
+ d = Fugit::Duration
162
+ .parse(seconds, options)
163
+ .deflate
268
164
 
269
- count = seconds / duration
270
- seconds = seconds % duration
165
+ d = d.drop_seconds if options[:drop_seconds]
166
+ d = d.deflate(:month => options[:months]) if options[:months]
271
167
 
272
- h[key.to_sym] = count if count > 0
273
- end
274
-
275
- h
168
+ d
276
169
  end
277
170
 
278
171
  #--
279
172
  # misc
280
173
  #++
281
174
 
282
- # Produces the UTC string representation of a Time instance
283
- #
284
- # like "2009/11/23 11:11:50.947109 UTC"
285
- #
286
- def self.utc_to_s(t=Time.now)
175
+ if RUBY_VERSION > '1.9.9'
287
176
 
288
- "#{t.utc.strftime('%Y-%m-%d %H:%M:%S')}.#{sprintf('%06d', t.usec)} UTC"
289
- end
177
+ # Produces the UTC string representation of a Time instance
178
+ #
179
+ # like "2009/11/23 11:11:50.947109 UTC"
180
+ #
181
+ def utc_to_s(t=Time.now)
182
+ "#{t.dup.utc.strftime('%F %T.%6N')} UTC"
183
+ end
290
184
 
291
- # Produces a hour/min/sec/milli string representation of Time instance
292
- #
293
- def self.h_to_s(t=Time.now)
185
+ # Produces a hour/min/sec/milli string representation of Time instance
186
+ #
187
+ def h_to_s(t=Time.now)
188
+ t.strftime('%T.%6N')
189
+ end
190
+ else
294
191
 
295
- "#{t.strftime('%H:%M:%S')}.#{sprintf('%06d', t.usec)}"
192
+ def utc_to_s(t=Time.now)
193
+ "#{t.utc.strftime('%Y-%m-%d %H:%M:%S')}.#{sprintf('%06d', t.usec)} UTC"
194
+ end
195
+ def h_to_s(t=Time.now)
196
+ "#{t.strftime('%H:%M:%S')}.#{sprintf('%06d', t.usec)}"
197
+ end
296
198
  end
297
199
 
298
- # Debugging tools...
299
- #
300
- class D
301
-
302
- def self.h_to_s(t=Time.now); Rufus::Scheduler.h_to_s(t); end
200
+ if defined?(Process::CLOCK_MONOTONIC)
201
+ def monow; Process.clock_gettime(Process::CLOCK_MONOTONIC); end
202
+ else
203
+ def monow; Time.now.to_f; end
303
204
  end
205
+
206
+ def ltstamp; Time.now.strftime('%FT%T.%3N'); end
207
+ end
208
+
209
+ # Debugging tools...
210
+ #
211
+ class D
212
+
213
+ def self.h_to_s(t=Time.now); Rufus::Scheduler.h_to_s(t); end
304
214
  end
305
215
  end
306
216