No commit activity in last 3 years
No release in over 3 years
EM::ScheduledTimer provides EventMachine timers that let you specify a time, rather than an interval, at which to fire.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0
>= 0
>= 0

Runtime

 Project Readme

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

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request