Class: UV::Scheduler
- Inherits:
-
Object
- Object
- UV::Scheduler
- Defined in:
- lib/uv-rays/scheduler.rb,
lib/uv-rays/scheduler/cron.rb,
lib/uv-rays/scheduler/time.rb
Defined Under Namespace
Classes: CronLine
Constant Summary collapse
- TZ_REGEX =
/\b((?:[a-zA-Z][a-zA-z0-9\-+]+)(?:\/[a-zA-Z0-9\-+]+)?)\b/- DURATIONS2M =
[ [ 'y', 365 * 24 * 3600 * 1000 ], [ 'M', 30 * 24 * 3600 * 1000 ], [ 'w', 7 * 24 * 3600 * 1000 ], [ 'd', 24 * 3600 * 1000 ], [ 'h', 3600 * 1000 ], [ 'm', 60 * 1000 ], [ 's', 1000 ] ]
- DURATIONS2 =
DURATIONS2M.dup
- DURATIONS =
DURATIONS2M.inject({}) { |r, (k, v)| r[k] = v; r }
- DURATION_LETTERS =
DURATIONS.keys.join
- DU_KEYS =
DURATIONS2M.collect { |k, v| k.to_sym }
Instance Attribute Summary collapse
-
#loop ⇒ Object
readonly
Returns the value of attribute loop.
-
#next ⇒ Object
readonly
Returns the value of attribute next.
-
#time_diff ⇒ Object
readonly
Returns the value of attribute time_diff.
Class Method Summary collapse
-
.h_to_s(t = Time.now) ⇒ Object
Produces a hour/min/sec/milli string representation of Time instance.
- .parse_at(o, quiet = false) ⇒ Object
- .parse_cron(o, quiet = false) ⇒ Object
-
.parse_duration(string, quiet = false) ⇒ Object
Turns a string like ‘1m10s’ into a float like ‘70.0’, more formally, turns a time duration expressed as a string into a Float instance (millisecond count).
- .parse_in(o, quiet = false) ⇒ Object
- .parse_to_time(o) ⇒ Object
-
.to_duration(seconds, options = {}) ⇒ Object
Turns a number of seconds into a a time string.
-
.to_duration_hash(seconds, options = {}) ⇒ Object
Turns a number of seconds (integer or Float) into a hash like in :.
-
.utc_to_s(t = Time.now) ⇒ Object
Produces the UTC string representation of a Time instance.
Instance Method Summary collapse
-
#at(time, callback = nil, &block) ⇒ ::UV::OneShot
Create a one off event that occurs at a particular date and time.
-
#cron(schedule, callback = nil, &block) ⇒ ::UV::Repeat
Create a repeating event that uses a CRON line to determine the trigger time.
-
#every(time, callback = nil, &block) ⇒ ::UV::Repeat
Create a repeating event that occurs each time period.
-
#in(time, callback = nil, &block) ⇒ ::UV::OneShot
Create a one off event that occurs after the time period.
-
#initialize(loop) ⇒ Scheduler
constructor
A new instance of Scheduler.
-
#reschedule(event) ⇒ Object
Schedules an event for execution.
-
#unschedule(event) ⇒ Object
Removes an event from the schedule.
Constructor Details
#initialize(loop) ⇒ Scheduler
Returns a new instance of Scheduler.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/uv-rays/scheduler.rb', line 164 def initialize(loop) @loop = loop @schedules = Set.new @scheduled = [] @next = nil # Next schedule time @timer = nil # Reference to the timer @timer_callback = method(:on_timer) # Not really required when used correctly @critical = Mutex.new # as the libuv time is taken from an arbitrary point in time we # need to roughly synchronize between it and ruby's Time.now @loop.update_time @time_diff = (Time.now.to_f * 1000).to_i - @loop.now end |
Instance Attribute Details
#loop ⇒ Object (readonly)
Returns the value of attribute loop.
159 160 161 |
# File 'lib/uv-rays/scheduler.rb', line 159 def loop @loop end |
#next ⇒ Object (readonly)
Returns the value of attribute next.
161 162 163 |
# File 'lib/uv-rays/scheduler.rb', line 161 def next @next end |
#time_diff ⇒ Object (readonly)
Returns the value of attribute time_diff.
160 161 162 |
# File 'lib/uv-rays/scheduler.rb', line 160 def time_diff @time_diff end |
Class Method Details
.h_to_s(t = Time.now) ⇒ Object
Produces a hour/min/sec/milli string representation of Time instance
270 271 272 |
# File 'lib/uv-rays/scheduler/time.rb', line 270 def self.h_to_s(t=Time.now) "#{t.strftime('%H:%M:%S')}.#{sprintf('%06d', t.usec)}" end |
.parse_at(o, quiet = false) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/uv-rays/scheduler/time.rb', line 36 def self.parse_at(o, quiet = false) return (o.to_f * 1000).to_i if o.is_a?(Time) tz = nil s = o.to_s.gsub(TZ_REGEX) { |m| t = TZInfo::Timezone.get(m) rescue nil tz ||= t t ? '' : m } begin DateTime.parse(o) rescue raise ArgumentError, "no time information in #{o.inspect}" end if RUBY_VERSION < '1.9.0' t = Time.parse(s) t = tz.local_to_utc(t) if tz (t.to_f * 1000).to_i # Convert to milliseconds rescue StandardError => se return nil if quiet raise se end |
.parse_cron(o, quiet = false) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/uv-rays/scheduler/time.rb', line 61 def self.parse_cron(o, quiet = false) CronLine.new(o) rescue ArgumentError => ae return nil if quiet raise ae end |
.parse_duration(string, quiet = false) ⇒ Object
Turns a string like ‘1m10s’ into a float like ‘70.0’, more formally, turns a time duration expressed as a string into a Float instance (millisecond count).
w -> week d -> day h -> hour m -> minute s -> second M -> month y -> year ‘nada’ -> millisecond
Some examples:
Rufus::Scheduler.parse_duration "0.5" # => 0.5
Rufus::Scheduler.parse_duration "500" # => 0.5
Rufus::Scheduler.parse_duration "1000" # => 1.0
Rufus::Scheduler.parse_duration "1h" # => 3600.0
Rufus::Scheduler.parse_duration "1h10s" # => 3610.0
Rufus::Scheduler.parse_duration "1w2d" # => 777600.0
Negative time strings are OK (Thanks Danny Fullerton):
Rufus::Scheduler.parse_duration "-0.5" # => -0.5
Rufus::Scheduler.parse_duration "-1h" # => -3600.0
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 157 158 159 160 161 |
# File 'lib/uv-rays/scheduler/time.rb', line 125 def self.parse_duration(string, quiet = false) string = string.to_s return 0 if string == '' m = string.match(/^(-?)([\d\.#{DURATION_LETTERS}]+)$/) return nil if m.nil? && quiet raise ArgumentError.new("cannot parse '#{string}'") if m.nil? mod = m[1] == '-' ? -1.0 : 1.0 val = 0.0 s = m[2] while s.length > 0 m = nil if m = s.match(/^(\d+|\d+\.\d*|\d*\.\d+)([#{DURATION_LETTERS}])(.*)$/) val += m[1].to_f * DURATIONS[m[2]] elsif s.match(/^\d+$/) val += s.to_i elsif s.match(/^\d*\.\d*$/) val += s.to_f elsif quiet return nil else raise ArgumentError.new( "cannot parse '#{string}' (unexpected '#{s}')" ) end break unless m && m[3] s = m[3] end res = mod * val res.to_i end |
.parse_in(o, quiet = false) ⇒ Object
29 30 31 32 |
# File 'lib/uv-rays/scheduler/time.rb', line 29 def self.parse_in(o, quiet = false) # if o is an integer we are looking at ms o.is_a?(String) ? parse_duration(o, quiet) : o end |
.parse_to_time(o) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/uv-rays/scheduler/time.rb', line 69 def self.parse_to_time(o) t = o t = parse(t) if t.is_a?(String) t = Time.now + t if t.is_a?(Numeric) raise ArgumentError.new( "cannot turn #{o.inspect} to a point in time, doesn't make sense" ) unless t.is_a?(Time) t end |
.to_duration(seconds, options = {}) ⇒ Object
Turns a number of seconds into a a time string
Rufus.to_duration 0 # => '0s'
Rufus.to_duration 60 # => '1m'
Rufus.to_duration 3661 # => '1h1m1s'
Rufus.to_duration 7 * 24 * 3600 # => '1w'
Rufus.to_duration 30 * 24 * 3600 + 1 # => "4w2d1s"
It goes from seconds to the year. Months are not counted (as they are of variable length). Weeks are counted.
For 30 days months to be counted, the second parameter of this method can be set to true.
Rufus.to_duration 30 * 24 * 3600 + 1, true # => "1M1s"
If a Float value is passed, milliseconds will be displayed without ‘marker’
Rufus.to_duration 0.051 # => "51"
Rufus.to_duration 7.051 # => "7s51"
Rufus.to_duration 0.120 + 30 * 24 * 3600 + 1 # => "4w2d1s120"
(this behaviour mirrors the one found for parse_time_string()).
Options are :
-
:months, if set to true, months (M) of 30 days will be taken into account when building up the result
-
:drop_seconds, if set to true, seconds and milliseconds will be trimmed from the result
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/uv-rays/scheduler/time.rb', line 196 def self.to_duration(seconds, = {}) h = to_duration_hash(seconds, ) return ([:drop_seconds] ? '0m' : '0s') if h.empty? s = DU_KEYS.inject('') { |r, key| count = h[key] count = nil if count == 0 r << "#{count}#{key}" if count r } ms = h[:ms] s << ms.to_s if ms s end |
.to_duration_hash(seconds, options = {}) ⇒ Object
Turns a number of seconds (integer or Float) into a hash like in :
Rufus.to_duration_hash 0.051
# => { :ms => "51" }
Rufus.to_duration_hash 7.051
# => { :s => 7, :ms => "51" }
Rufus.to_duration_hash 0.120 + 30 * 24 * 3600 + 1
# => { :w => 4, :d => 2, :s => 1, :ms => "120" }
This method is used by to_duration behind the scenes.
Options are :
-
:months, if set to true, months (M) of 30 days will be taken into account when building up the result
-
:drop_seconds, if set to true, seconds and milliseconds will be trimmed from the result
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/uv-rays/scheduler/time.rb', line 231 def self.to_duration_hash(seconds, = {}) h = {} if (seconds % 1000) > 0 h[:ms] = (seconds % 1000).to_i seconds = (seconds / 1000).to_i * 1000 end if [:drop_seconds] h.delete(:ms) seconds = (seconds - seconds % 60000) end durations = [:months] ? DURATIONS2M : DURATIONS2 durations.each do |key, duration| count = seconds / duration seconds = seconds % duration h[key.to_sym] = count if count > 0 end h end |
.utc_to_s(t = Time.now) ⇒ Object
Produces the UTC string representation of a Time instance
like “2009/11/23 11:11:50.947109 UTC”
264 265 266 |
# File 'lib/uv-rays/scheduler/time.rb', line 264 def self.utc_to_s(t=Time.now) "#{t.utc.strftime('%Y-%m-%d %H:%M:%S')}.#{sprintf('%06d', t.usec)} UTC" end |
Instance Method Details
#at(time, callback = nil, &block) ⇒ ::UV::OneShot
Create a one off event that occurs at a particular date and time
221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/uv-rays/scheduler.rb', line 221 def at(time, callback = nil, &block) callback ||= block ms = Scheduler.parse_at(time) - @time_diff event = OneShot.new(self, ms) if callback.respond_to? :call event.progress callback end schedule(event) event end |
#cron(schedule, callback = nil, &block) ⇒ ::UV::Repeat
Create a repeating event that uses a CRON line to determine the trigger time
238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/uv-rays/scheduler.rb', line 238 def cron(schedule, callback = nil, &block) callback ||= block ms = Scheduler.parse_cron(schedule) event = Repeat.new(self, ms) if callback.respond_to? :call event.progress callback end schedule(event) event end |
#every(time, callback = nil, &block) ⇒ ::UV::Repeat
Create a repeating event that occurs each time period
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/uv-rays/scheduler.rb', line 187 def every(time, callback = nil, &block) callback ||= block ms = Scheduler.parse_in(time) event = Repeat.new(self, ms) if callback.respond_to? :call event.progress callback end schedule(event) event end |
#in(time, callback = nil, &block) ⇒ ::UV::OneShot
Create a one off event that occurs after the time period
204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/uv-rays/scheduler.rb', line 204 def in(time, callback = nil, &block) callback ||= block ms = @loop.now + Scheduler.parse_in(time) event = OneShot.new(self, ms) if callback.respond_to? :call event.progress callback end schedule(event) event end |
#reschedule(event) ⇒ Object
Schedules an event for execution
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/uv-rays/scheduler.rb', line 253 def reschedule(event) # Check promise is not resolved return if event.resolved? @critical.synchronize { # Remove the event from the scheduled list and ensure it is in the schedules set if @schedules.include?(event) remove(event) else @schedules << event end # optimal algorithm for inserting into an already sorted list Bisect.insort(@scheduled, event) # Update the timer check_timer } end |
#unschedule(event) ⇒ Object
Removes an event from the schedule
276 277 278 279 280 281 282 283 284 285 |
# File 'lib/uv-rays/scheduler.rb', line 276 def unschedule(event) @critical.synchronize { # Only call delete and update the timer when required if @schedules.include?(event) @schedules.delete(event) remove(event) check_timer end } end |