rufus-scheduler 3.5.2 → 3.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,200 +1,216 @@
1
1
 
2
- module Rufus
2
+ class Rufus::Scheduler
3
3
 
4
- class Scheduler
4
+ class << self
5
5
 
6
- class << self
6
+ #--
7
+ # time and string methods
8
+ #++
7
9
 
8
- #--
9
- # time and string methods
10
- #++
10
+ def parse(o, opts={})
11
11
 
12
- def parse(o, opts={})
12
+ opts[:no_error] = true
13
13
 
14
- opts[:no_error] = true
14
+ parse_cron(o, opts) ||
15
+ parse_in(o, opts) || # covers 'every' schedule strings
16
+ parse_at(o, opts) ||
17
+ fail(ArgumentError.new("couldn't parse #{o.inspect} (#{o.class})"))
18
+ end
15
19
 
16
- parse_cron(o, opts) ||
17
- parse_in(o, opts) || # covers 'every' schedule strings
18
- parse_at(o, opts) ||
19
- fail(ArgumentError.new("couldn't parse #{o.inspect} (#{o.class})"))
20
- end
20
+ def parse_cron(o, opts={})
21
21
 
22
- def parse_cron(o, opts)
22
+ opts[:no_error] ?
23
+ Fugit.parse_cron(o) :
24
+ Fugit.do_parse_cron(o)
25
+ end
23
26
 
24
- Fugit.parse_cron(o)
25
- end
27
+ def parse_in(o, opts={})
26
28
 
27
- def parse_in(o, opts={})
29
+ #o.is_a?(String) ? parse_duration(o, opts) : o
28
30
 
29
- #o.is_a?(String) ? parse_duration(o, opts) : o
31
+ return parse_duration(o, opts) if o.is_a?(String)
32
+ return o if o.is_a?(Numeric)
30
33
 
31
- return parse_duration(o, opts) if o.is_a?(String)
32
- return o if o.is_a?(Numeric)
34
+ fail ArgumentError.new("couldn't parse time point in #{o.inspect}")
33
35
 
34
- fail ArgumentError.new("couldn't parse time point in #{o.inspect}")
36
+ rescue ArgumentError => ae
35
37
 
36
- rescue ArgumentError => ae
38
+ return nil if opts[:no_error]
39
+ fail ae
40
+ end
37
41
 
38
- return nil if opts[:no_error]
39
- fail ae
40
- end
42
+ def parse_at(o, opts={})
41
43
 
42
- def parse_at(o, opts={})
44
+ return o if o.is_a?(EoTime)
45
+ return EoTime.make(o) if o.is_a?(Time)
46
+ EoTime.parse(o, opts)
43
47
 
44
- return o if o.is_a?(EoTime)
45
- return EoTime.make(o) if o.is_a?(Time)
46
- EoTime.parse(o, opts)
48
+ rescue StandardError => se
47
49
 
48
- rescue StandardError => se
50
+ return nil if opts[:no_error]
51
+ fail se
52
+ end
49
53
 
50
- return nil if opts[:no_error]
51
- fail se
52
- end
54
+ # Turns a string like '1m10s' into a float like '70.0', more formally,
55
+ # turns a time duration expressed as a string into a Float instance
56
+ # (millisecond count).
57
+ #
58
+ # w -> week
59
+ # d -> day
60
+ # h -> hour
61
+ # m -> minute
62
+ # s -> second
63
+ # M -> month
64
+ # y -> year
65
+ # 'nada' -> millisecond
66
+ #
67
+ # Some examples:
68
+ #
69
+ # Rufus::Scheduler.parse_duration "0.5" # => 0.5
70
+ # Rufus::Scheduler.parse_duration "500" # => 0.5
71
+ # Rufus::Scheduler.parse_duration "1000" # => 1.0
72
+ # Rufus::Scheduler.parse_duration "1h" # => 3600.0
73
+ # Rufus::Scheduler.parse_duration "1h10s" # => 3610.0
74
+ # Rufus::Scheduler.parse_duration "1w2d" # => 777600.0
75
+ #
76
+ # Negative time strings are OK (Thanks Danny Fullerton):
77
+ #
78
+ # Rufus::Scheduler.parse_duration "-0.5" # => -0.5
79
+ # Rufus::Scheduler.parse_duration "-1h" # => -3600.0
80
+ #
81
+ def parse_duration(str, opts={})
82
+
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
90
+ end
53
91
 
54
- # Turns a string like '1m10s' into a float like '70.0', more formally,
55
- # turns a time duration expressed as a string into a Float instance
56
- # (millisecond count).
57
- #
58
- # w -> week
59
- # d -> day
60
- # h -> hour
61
- # m -> minute
62
- # s -> second
63
- # M -> month
64
- # y -> year
65
- # 'nada' -> millisecond
66
- #
67
- # Some examples:
68
- #
69
- # Rufus::Scheduler.parse_duration "0.5" # => 0.5
70
- # Rufus::Scheduler.parse_duration "500" # => 0.5
71
- # Rufus::Scheduler.parse_duration "1000" # => 1.0
72
- # Rufus::Scheduler.parse_duration "1h" # => 3600.0
73
- # Rufus::Scheduler.parse_duration "1h10s" # => 3610.0
74
- # Rufus::Scheduler.parse_duration "1w2d" # => 777600.0
75
- #
76
- # Negative time strings are OK (Thanks Danny Fullerton):
77
- #
78
- # Rufus::Scheduler.parse_duration "-0.5" # => -0.5
79
- # Rufus::Scheduler.parse_duration "-1h" # => -3600.0
80
- #
81
- def parse_duration(str, opts={})
82
-
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
90
- end
92
+ # Turns a number of seconds into a a time string
93
+ #
94
+ # Rufus.to_duration 0 # => '0s'
95
+ # Rufus.to_duration 60 # => '1m'
96
+ # Rufus.to_duration 3661 # => '1h1m1s'
97
+ # Rufus.to_duration 7 * 24 * 3600 # => '1w'
98
+ # Rufus.to_duration 30 * 24 * 3600 + 1 # => "4w2d1s"
99
+ #
100
+ # It goes from seconds to the year. Months are not counted (as they
101
+ # are of variable length). Weeks are counted.
102
+ #
103
+ # For 30 days months to be counted, the second parameter of this
104
+ # method can be set to true.
105
+ #
106
+ # Rufus.to_duration 30 * 24 * 3600 + 1, true # => "1M1s"
107
+ #
108
+ # If a Float value is passed, milliseconds will be displayed without
109
+ # 'marker'
110
+ #
111
+ # Rufus.to_duration 0.051 # => "51"
112
+ # Rufus.to_duration 7.051 # => "7s51"
113
+ # Rufus.to_duration 0.120 + 30 * 24 * 3600 + 1 # => "4w2d1s120"
114
+ #
115
+ # (this behaviour mirrors the one found for parse_time_string()).
116
+ #
117
+ # Options are :
118
+ #
119
+ # * :months, if set to true, months (M) of 30 days will be taken into
120
+ # account when building up the result
121
+ # * :drop_seconds, if set to true, seconds and milliseconds will be
122
+ # trimmed from the result
123
+ #
124
+ def to_duration(seconds, options={})
91
125
 
92
- # Turns a number of seconds into a a time string
93
- #
94
- # Rufus.to_duration 0 # => '0s'
95
- # Rufus.to_duration 60 # => '1m'
96
- # Rufus.to_duration 3661 # => '1h1m1s'
97
- # Rufus.to_duration 7 * 24 * 3600 # => '1w'
98
- # Rufus.to_duration 30 * 24 * 3600 + 1 # => "4w2d1s"
99
- #
100
- # It goes from seconds to the year. Months are not counted (as they
101
- # are of variable length). Weeks are counted.
102
- #
103
- # For 30 days months to be counted, the second parameter of this
104
- # method can be set to true.
105
- #
106
- # Rufus.to_duration 30 * 24 * 3600 + 1, true # => "1M1s"
107
- #
108
- # If a Float value is passed, milliseconds will be displayed without
109
- # 'marker'
110
- #
111
- # Rufus.to_duration 0.051 # => "51"
112
- # Rufus.to_duration 7.051 # => "7s51"
113
- # Rufus.to_duration 0.120 + 30 * 24 * 3600 + 1 # => "4w2d1s120"
114
- #
115
- # (this behaviour mirrors the one found for parse_time_string()).
116
- #
117
- # Options are :
118
- #
119
- # * :months, if set to true, months (M) of 30 days will be taken into
120
- # account when building up the result
121
- # * :drop_seconds, if set to true, seconds and milliseconds will be
122
- # trimmed from the result
123
- #
124
- def to_duration(seconds, options={})
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
125
130
 
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
131
+ to_fugit_duration(seconds, options).to_rufus_s
132
+ end
130
133
 
131
- to_fugit_duration(seconds, options).to_rufus_s
132
- end
134
+ # Turns a number of seconds (integer or Float) into a hash like in :
135
+ #
136
+ # Rufus.to_duration_hash 0.051
137
+ # # => { :s => 0.051 }
138
+ # Rufus.to_duration_hash 7.051
139
+ # # => { :s => 7.051 }
140
+ # Rufus.to_duration_hash 0.120 + 30 * 24 * 3600 + 1
141
+ # # => { :w => 4, :d => 2, :s => 1.120 }
142
+ #
143
+ # This method is used by to_duration behind the scenes.
144
+ #
145
+ # Options are :
146
+ #
147
+ # * :months, if set to true, months (M) of 30 days will be taken into
148
+ # account when building up the result
149
+ # * :drop_seconds, if set to true, seconds and milliseconds will be
150
+ # trimmed from the result
151
+ #
152
+ def to_duration_hash(seconds, options={})
133
153
 
134
- # Turns a number of seconds (integer or Float) into a hash like in :
135
- #
136
- # Rufus.to_duration_hash 0.051
137
- # # => { :s => 0.051 }
138
- # Rufus.to_duration_hash 7.051
139
- # # => { :s => 7.051 }
140
- # Rufus.to_duration_hash 0.120 + 30 * 24 * 3600 + 1
141
- # # => { :w => 4, :d => 2, :s => 1.120 }
142
- #
143
- # This method is used by to_duration behind the scenes.
144
- #
145
- # Options are :
146
- #
147
- # * :months, if set to true, months (M) of 30 days will be taken into
148
- # account when building up the result
149
- # * :drop_seconds, if set to true, seconds and milliseconds will be
150
- # trimmed from the result
151
- #
152
- def to_duration_hash(seconds, options={})
154
+ to_fugit_duration(seconds, options).to_rufus_h
155
+ end
153
156
 
154
- to_fugit_duration(seconds, options).to_rufus_h
155
- end
157
+ # Used by both .to_duration and .to_duration_hash
158
+ #
159
+ def to_fugit_duration(seconds, options={})
156
160
 
157
- # Used by both .to_duration and .to_duration_hash
158
- #
159
- def to_fugit_duration(seconds, options={})
161
+ d = Fugit::Duration
162
+ .parse(seconds, options)
163
+ .deflate
160
164
 
161
- d = Fugit::Duration
162
- .parse(seconds, options)
163
- .deflate
165
+ d = d.drop_seconds if options[:drop_seconds]
166
+ d = d.deflate(:month => options[:months]) if options[:months]
164
167
 
165
- d = d.drop_seconds if options[:drop_seconds]
166
- d = d.deflate(:month => options[:months]) if options[:months]
168
+ d
169
+ end
167
170
 
168
- d
169
- end
171
+ #--
172
+ # misc
173
+ #++
170
174
 
171
- #--
172
- # misc
173
- #++
175
+ if RUBY_VERSION > '1.9.9'
174
176
 
175
177
  # Produces the UTC string representation of a Time instance
176
178
  #
177
179
  # like "2009/11/23 11:11:50.947109 UTC"
178
180
  #
179
181
  def utc_to_s(t=Time.now)
180
-
181
- "#{t.utc.strftime('%Y-%m-%d %H:%M:%S')}.#{sprintf('%06d', t.usec)} UTC"
182
+ "#{t.dup.utc.strftime('%F %T.%6N')} UTC"
182
183
  end
183
184
 
184
185
  # Produces a hour/min/sec/milli string representation of Time instance
185
186
  #
186
187
  def h_to_s(t=Time.now)
188
+ t.strftime('%T.%6N')
189
+ end
190
+ else
187
191
 
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)
188
196
  "#{t.strftime('%H:%M:%S')}.#{sprintf('%06d', t.usec)}"
189
197
  end
190
198
  end
191
199
 
192
- # Debugging tools...
193
- #
194
- class D
195
-
196
- 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
197
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
198
214
  end
199
215
  end
200
216