EM::ScheduledTimer
EventMachine timers are great, but they work by waiting for a specified
time interval before firing. Instead, EM::ScheduledTimer
lets you
specify a Time
, Date
or DateTime
object (or indeed anything that will
respond to to_time
).
Why is this useful?
Imagine you're polling an HTTP-based API for changes. Because the API is
nice, it will set Expires
response headers so you know when to make
the next request. With EM::ScheduledTimer
(and the
em-http-request gem),
this becomes very easy:
def poll_api
http = EM::HttpRequest.new("http://api.example.com/changes")
http.callback do
expires = http.response_header['EXPIRES']
time = Time.httpdate(expires)
EM::ScheduledTimer.new(time) { poll_api }
end
end
Usage
Generally speaking, the API for a ScheduledTimer
is modelled after
that of a regular EM::Timer
.
You can create EM::ScheduledTimer
instances and pass in a block:
EM::ScheduledTimer.new(some_future_time) do
puts "Fire!"
end
Alternatively, you can pass in any object that responds to #call
(including a Proc
):
callback = -> { puts "Fire!" }
EM::ScheduledTimer.new(some_future_time, callback)
A ScheduledTimer
can also be cancelled:
timer = EM::ScheduledTimer.new(some_future_time) do
puts "Fire!"
end
timer.cancel # The timer won't fire
As with regular timers, a convenience method is available on the
EventMachine
module:
EM.add_scheduled_timer(some_future_time) do
puts "Fire!"
end
Note that in the latter case, you won't be able to cancel a timer that you've scheduled.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request