Class: Later::Schedule

Inherits:
Object
  • Object
show all
Extended by:
Predicates
Defined in:
lib/later.rb

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Schedule

Returns a new instance of Schedule.



13
14
15
16
17
18
19
# File 'lib/later.rb', line 13

def initialize(key)
  if key.is_a?(Nest)
    @key = key
  else
    @key = Later.key[key]
  end
end

Instance Method Details

#[](event) ⇒ Object

Returns the time of a scheduled unique event.

Later[:reservations].set 'event-1', Time.parse('2012-09-28 11:36:17 +0800')
Later[:reservations]['event-1'] #=> 2012-09-28 11:36:17 +0800


42
43
44
# File 'lib/later.rb', line 42

def [](event)
  Time.at key[:schedule].zscore(event) rescue nil
end

#countObject

Returns the number of scheduled unique events.

Later[:reservations].set 'event-1', Time.now + 60
Later[:reservations].set 'event-2', Time.now + 120
Later[:reservations].set 'event-3', Time.now + 180
Later[:reservations].count #=> 3


53
54
55
# File 'lib/later.rb', line 53

def count
  key[:schedule].zcard
end

#each(timeout = 1, &block) ⇒ Object

Processes each scheduled unique event. The block only gets called when an event is due to run based on the current time.

Accepts an optional ‘timeout` parameter with a default value of `1`. Passing an `Integer` will use Redis’ blocking mechanism to process the schedule and is therefore more efficient. Passing a ‘Float` will poll Redis using the given timeout, and should only be used for events which need to be triggered with millisecond precision.

Later[:reservations].each do |event|
  # Do something with the event
end

The schedule will be polled every 0.1 seconds:

Later[:reservations].each(0.1) do |event|
  # Do something with the event
end


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/later.rb', line 109

def each(timeout = 1, &block)
  @stop = false

  loop do
    break if stop?

    time = Time.now.to_f

    push_to_queue pop_from_schedules(time)
    next unless event = pop_from_queue(timeout)

    begin
      block.call event
    rescue Exception => e
      exceptions.rpush JSON(time: Time.now, event: event, message: e.inspect)
    ensure
      local.del
    end
  end
end

#empty?Boolean

Returns ‘true` if there are no scheduled unique events. Returns `false` otherwise.

Later[:reservations].set 'event-1', Time.now + 60
Later[:reservations].empty? #=> false
Later[:reservations].unset 'event-1'
Later[:reservations].empty? #=> true

Returns:

  • (Boolean)


63
64
65
# File 'lib/later.rb', line 63

def empty?
  count.zero?
end

#exceptionsObject

Returns the Nest key of this schedule’s exception list.

Later[:reservations].exceptions #=> Later:reservations:exceptions


33
34
35
# File 'lib/later.rb', line 33

def exceptions
  @exceptions ||= key[:exceptions]
end

#keyObject

Returns the Nest key of this schedule.

Later[:reservations].key #=> Later:reservations


25
26
27
# File 'lib/later.rb', line 25

def key
  @key
end

#set(event, time) ⇒ Object

Sets a unique event to this schedule.

Later[:reservations].set 'event-1', Time.now + 60


71
72
73
# File 'lib/later.rb', line 71

def set(event, time)
  key[:schedule].zadd time.to_f, event
end

#stop!Object

When called inside an ‘each` block, `stop!` signals the block to halt processing of this schedule.

Later[:reservations].each do |event|
  Later[:reservations].stop!
end


89
90
91
# File 'lib/later.rb', line 89

def stop!
  @stop = true
end

#unset(event) ⇒ Object

Unsets a unique event from this schedule.

Later[:reservations].unset 'event-1'


79
80
81
# File 'lib/later.rb', line 79

def unset(event)
  key[:schedule].zrem event
end