Sha256: 9e3fd88168c0f22e387099570adafe6ee03de5725aa097069107edb8bbf3cfef

Contents?: true

Size: 651 Bytes

Versions: 4

Compression:

Stored size: 651 Bytes

Contents

=begin
  Most periodic tasks need to be run both now 
  and after a specificed time period.
  
  This Eventmachine helper does that. You can
  specify the timeframe in hours, minutes or 
  seconds.
=end

module EventMachine
  def self.now_and_every(period, &blk)
    seconds = case
      when period.respond_to?(:to_f)
        period.to_f
      when period.include?(:hours)
        period[:hours]*60*60
      when period.include?(:minutes)
        period[:minutes]*60
      else
        period[:seconds]
    end
    
    EM::Synchrony.next_tick do
      yield
    end
    
    EM::Synchrony.add_periodic_timer(seconds) do
      yield
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
radiodan-0.0.4 lib/radiodan/em_additions.rb
radiodan-0.0.3 lib/radiodan/em_additions.rb
radiodan-0.0.2 lib/radiodan/em_additions.rb
radiodan-0.0.1 lib/em_additions.rb