Class: UV::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/uv-rays/scheduler.rb,
lib/uv-rays/scheduler/time.rb

Defined Under Namespace

Classes: TimeInZone

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reactor) ⇒ Scheduler

Returns a new instance of Scheduler.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/uv-rays/scheduler.rb', line 176

def initialize(reactor)
    @reactor = reactor
    @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

    # Every hour we should re-calibrate this (just in case)
    calibrate_time

    @calibrate = @reactor.timer do
        calibrate_time
        @calibrate.start(3600000)
    end
    @calibrate.start(3600000)
    @calibrate.unref
end

Instance Attribute Details

#next ⇒ Object (readonly)

Returns the value of attribute next.



173
174
175
# File 'lib/uv-rays/scheduler.rb', line 173

def next
  @next
end

#reactor ⇒ Object (readonly)

Returns the value of attribute reactor.



171
172
173
# File 'lib/uv-rays/scheduler.rb', line 171

def reactor
  @reactor
end

#time_diff ⇒ Object (readonly)

Returns the value of attribute time_diff.



172
173
174
# File 'lib/uv-rays/scheduler.rb', line 172

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



303
304
305
# File 'lib/uv-rays/scheduler/time.rb', line 303

def self.h_to_s(t=Time.now)
    "#{t.strftime('%H:%M:%S')}.#{sprintf('%06d', t.usec)}"
end

.parse_at(o, quiet = false) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/uv-rays/scheduler/time.rb', line 64

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, timezone: nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/uv-rays/scheduler/time.rb', line 89

def self.parse_cron(o, quiet = false, timezone: nil)
    if timezone
        tz = TimeInZone.new(timezone)
        CronParser.new(o, tz)
    else
        CronParser.new(o)
    end

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

Raises:

  • (ArgumentError)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/uv-rays/scheduler/time.rb', line 158

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



57
58
59
60
# File 'lib/uv-rays/scheduler/time.rb', line 57

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

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
109
110
111
112
# File 'lib/uv-rays/scheduler/time.rb', line 102

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


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/uv-rays/scheduler/time.rb', line 229

def self.to_duration(seconds, options = {})
    h = to_duration_hash(seconds, options)

    return (options[:drop_seconds] ? '0m' : '0s') if h.empty?

    s = DU_KEYS.inject(String.new) { |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


264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/uv-rays/scheduler/time.rb', line 264

def self.to_duration_hash(seconds, options = {})
    h = {}

    if (seconds % 1000) > 0
        h[:ms] = (seconds % 1000).to_i
        seconds = (seconds / 1000).to_i * 1000
    end

    if options[:drop_seconds]
        h.delete(:ms)
        seconds = (seconds - seconds % 60000)
    end

    durations = options[: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"



297
298
299
# File 'lib/uv-rays/scheduler/time.rb', line 297

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

Parameters:

  • time (String, Time) —

    a representation of a date and time that can be parsed

  • callback (Proc) (defaults to: nil) —

    a block or method to execute when the event triggers

Returns:



245
246
247
248
249
250
251
252
253
254
255
# File 'lib/uv-rays/scheduler.rb', line 245

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

#calibrate_time ⇒ Object

As the libuv time is taken from an arbitrary point in time we need to roughly synchronize between it and ruby's Time.now



201
202
203
204
# File 'lib/uv-rays/scheduler.rb', line 201

def calibrate_time
    @reactor.update_time
    @time_diff = (Time.now.to_f * 1000).to_i - @reactor.now
end

#cron(schedule, callback = nil, timezone: nil, &block) ⇒ ::UV::Repeat

Create a repeating event that uses a CRON line to determine the trigger time

Parameters:

  • schedule (String) —

    a standard CRON job line.

  • callback (Proc) (defaults to: nil) —

    a block or method to execute when the event triggers

Returns:



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/uv-rays/scheduler.rb', line 262

def cron(schedule, callback = nil, timezone: nil , &block)
    callback ||= block
    ms = Scheduler.parse_cron(schedule, timezone: timezone)
    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

Parameters:

  • time (String) —

    a human readable string representing the time period. 3w2d4h1m2s for example.

  • callback (Proc) (defaults to: nil) —

    a block or method to execute when the event triggers

Returns:



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/uv-rays/scheduler.rb', line 211

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

Parameters:

  • time (String) —

    a human readable string representing the time period. 3w2d4h1m2s for example.

  • callback (Proc) (defaults to: nil) —

    a block or method to execute when the event triggers

Returns:



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/uv-rays/scheduler.rb', line 228

def in(time, callback = nil, &block)
    callback ||= block
    ms = @reactor.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

Parameters:



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/uv-rays/scheduler.rb', line 277

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

Parameters:



300
301
302
303
304
305
306
307
308
309
# File 'lib/uv-rays/scheduler.rb', line 300

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