Class: Later::Schedule
Instance Method Summary collapse
-
#[](event) ⇒ Object
Returns the time of a scheduled unique event.
-
#count ⇒ Object
Returns the number of scheduled unique events.
-
#each(timeout = 1, &block) ⇒ Object
Processes each scheduled unique event.
-
#empty? ⇒ Boolean
Returns ‘true` if there are no scheduled unique events.
-
#exceptions ⇒ Object
Returns the Nest key of this schedule’s exception list.
-
#initialize(key) ⇒ Schedule
constructor
A new instance of Schedule.
-
#key ⇒ Object
Returns the Nest key of this schedule.
-
#set(event, time) ⇒ Object
Sets a unique event to this schedule.
-
#stop! ⇒ Object
When called inside an ‘each` block, `stop!` signals the block to halt processing of this schedule.
-
#unset(event) ⇒ Object
Unsets a unique event from this schedule.
Constructor Details
Instance Method Details
#[](event) ⇒ Object
42 43 44 |
# File 'lib/later.rb', line 42 def [](event) Time.at key[:schedule].zscore(event) rescue nil end |
#count ⇒ Object
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
63 64 65 |
# File 'lib/later.rb', line 63 def empty? count.zero? end |
#exceptions ⇒ Object
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 |
#key ⇒ Object
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
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 |