Sha256: 434dc5debddb4438798b048a6f20a0e3a031996344fa2c9cd899783ba90162d3
Contents?: true
Size: 1.17 KB
Versions: 3
Compression:
Stored size: 1.17 KB
Contents
require 'thread' require 'timers' module PromisePool class Timer Error = Class.new(RuntimeError) @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=Error.new('execution expired') self.timeout = timeout self.error = error end def on_timeout &block self.block = block start 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
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
promise_pool-0.9.2 | lib/promise_pool/timer.rb |
promise_pool-0.9.1 | lib/promise_pool/timer.rb |
promise_pool-0.9.0 | lib/promise_pool/timer.rb |