Class: UV::Scheduler

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reactor) ⇒ Scheduler

Returns a new instance of Scheduler.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/uv-rays/scheduler.rb', line 168

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

    # as the libuv time is taken from an arbitrary point in time we
    # need to roughly synchronize between it and ruby's Time.now
    @reactor.update_time
    @time_diff = (Time.now.to_f * 1000).to_i - @reactor.now
end

Instance Attribute Details

#nextObject (readonly)

Returns the value of attribute next.



165
166
167
# File 'lib/uv-rays/scheduler.rb', line 165

def next
  @next
end

#reactorObject (readonly)

Returns the value of attribute reactor.



163
164
165
# File 'lib/uv-rays/scheduler.rb', line 163

def reactor
  @reactor
end

#time_diffObject (readonly)

Returns the value of attribute time_diff.



164
165
166
# File 'lib/uv-rays/scheduler.rb', line 164

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



272
273
274
# File 'lib/uv-rays/scheduler/time.rb', line 272

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

.parse_at(o, quiet = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/uv-rays/scheduler/time.rb', line 38

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



63
64
65
66
67
68
69
# File 'lib/uv-rays/scheduler/time.rb', line 63

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

Raises:

  • (ArgumentError)


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
162
163
# File 'lib/uv-rays/scheduler/time.rb', line 127

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



31
32
33
34
# File 'lib/uv-rays/scheduler/time.rb', line 31

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)


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/uv-rays/scheduler/time.rb', line 71

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



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/uv-rays/scheduler/time.rb', line 198

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



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/uv-rays/scheduler/time.rb', line 233

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”



266
267
268
# File 'lib/uv-rays/scheduler/time.rb', line 266

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:



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/uv-rays/scheduler.rb', line 225

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

Parameters:

  • schedule (String)

    a standard CRON job line.

  • callback (Proc) (defaults to: nil)

    a block or method to execute when the event triggers

Returns:



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/uv-rays/scheduler.rb', line 242

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

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:



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/uv-rays/scheduler.rb', line 191

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:



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/uv-rays/scheduler.rb', line 208

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:



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/uv-rays/scheduler.rb', line 257

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:



280
281
282
283
284
285
286
287
288
289
# File 'lib/uv-rays/scheduler.rb', line 280

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