Sha256: 2c2e093e93a7d6373705098a50e37a6cb640d9562ed808c4facf4aec07fc78f7
Contents?: true
Size: 1.18 KB
Versions: 1
Compression:
Stored size: 1.18 KB
Contents
require 'thread' require 'timers' module PromisePool class Timer @mutex = Mutex.new @interval = 1 singleton_class.module_eval do attr_accessor :interval def group @group ||= @mutex.synchronize{ @group ||= group_new } end private def group_new g = Timers::Group.new g.every(interval){} @thread = Thread.new do begin g.wait rescue => e warn "#{self.class}: ERROR: #{e}\n from #{e.backtrace.inspect}" end while g.count > 1 @group = nil end g end end attr_accessor :timeout, :error, :timer def initialize timeout, error, &block self.timeout = timeout self.error = error self.block = block start if block_given? end def on_timeout &block self.block = block start if block_given? end # should never raise! def cancel timer.cancel if timer self.block = nil end def start return if timeout.nil? || timeout.zero? self.timer = self.class.group.after(timeout){ block.call if block } end protected attr_accessor :block end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
promise_pool-0.1.0 | lib/promise_pool/timer.rb |