Class: Raktr::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/raktr/queue.rb

Overview

Note:

Pretty much an ‘EventMachine::Queue` rip-off.

A cross thread, Raktr scheduled, linear queue.

This class provides a simple queue abstraction on top of the scheduler.

It services two primary purposes:

  • API sugar for stateful protocols.

  • Pushing processing onto the reactor thread.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reactor) ⇒ Queue

Returns a new instance of Queue.

Parameters:

  • reactor (Reactor)


31
32
33
34
35
# File 'lib/raktr/queue.rb', line 31

def initialize( reactor )
    @raktr = reactor
    @items   = []
    @waiting = []
end

Instance Attribute Details

#raktrRaktr (readonly)

Returns:



28
29
30
# File 'lib/raktr/queue.rb', line 28

def raktr
  @raktr
end

Instance Method Details

#empty?Boolean

Note:

This is a peek, it’s not thread safe, and may only tend toward accuracy.

Returns:

  • (Boolean)


67
68
69
# File 'lib/raktr/queue.rb', line 67

def empty?
    @items.empty?
end

#num_waitingInteger

Note:

Accuracy cannot be guaranteed.

Returns Number of jobs that are currently waiting on the Queue for items to appear.

Returns:

  • (Integer)

    Number of jobs that are currently waiting on the Queue for items to appear.



83
84
85
# File 'lib/raktr/queue.rb', line 83

def num_waiting
    @waiting.size
end

#pop(&block) ⇒ Object

Parameters:

  • block (Block)

    Block to be scheduled by the Reactor and passed an item from the queue as soon as one becomes available.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/raktr/queue.rb', line 40

def pop( &block )
    @raktr.schedule do
        if @items.empty?
            @waiting << block
        else
            block.call @items.shift
        end
    end

    nil
end

#push(item) ⇒ Object Also known as: <<

Parameters:

  • item (Object)

    Schedules an item for addition to the queue.



54
55
56
57
58
59
60
61
# File 'lib/raktr/queue.rb', line 54

def push( item )
    @raktr.schedule do
        @items.push( item )
        @waiting.shift.call @items.shift until @items.empty? || @waiting.empty?
    end

    nil
end

#sizeInteger

Note:

This is a peek, it’s not thread safe, and may only tend toward accuracy.

Returns Queue size.

Returns:

  • (Integer)

    Queue size.



75
76
77
# File 'lib/raktr/queue.rb', line 75

def size
    @items.size
end