class BitGirder::Core::WaitCondition

Public Class Methods

create_and_exec( waiter, opts, blk ) click to toggle source
# File lib/bitgirder/core.rb, line 1359
def self.create_and_exec( waiter, opts, blk )
    
    raise "No block given" unless blk

    self.send( :new, blk, waiter, opts ).execute
end
new( f, waiter, opts ) click to toggle source
# File lib/bitgirder/core.rb, line 1325
def initialize( f, waiter, opts )

    @f, @waiter = f, waiter
    
    raise "One of :max_tries or :max_wait must be given" unless 
        opts.key?( :max_tries ) || opts.key?( :max_wait )

    @max_tries = opts.key?( :max_tries ) ?
        positive( opts[ :max_tries ] ) : 1 << 63
    
    @max_wait = opts.key?( :max_wait ) ? 
        positive( opts[ :max_wait ] ) : Float::INFINITY
end
wait_backoff( opts, &blk ) click to toggle source
# File lib/bitgirder/core.rb, line 1374
def self.wait_backoff( opts, &blk )
    
    seed = positive( has_key( opts, :seed ), :seed )
    waiter = lambda { |rem| sleep( [ seed, rem ].min ); seed *= 2 }

    self.create_and_exec( waiter, opts, blk )
end
wait_poll( opts, &blk ) click to toggle source
# File lib/bitgirder/core.rb, line 1366
def self.wait_poll( opts, &blk )
    
    poll = positive( has_key( opts, :poll ), :poll )
    waiter = lambda { |rem| sleep( [ poll, rem ].min ) }

    self.create_and_exec( waiter, opts, blk )
end

Private Class Methods

new( *argv ) click to toggle source
# File lib/bitgirder/core.rb, line 825
def initialize( *argv )
    BitGirderClassDefinition.init_instance( self, argv )
end

Public Instance Methods

execute() click to toggle source
# File lib/bitgirder/core.rb, line 1340
def execute

    res = nil
    
    start = Time.now

    @max_tries.times do |i|

        break if res = @f.call

        remain = @max_wait - ( Time.now - start )
        break if remain <= 0

        @waiter.call( remain ) if i < @max_tries - 1
    end

    res
end