Class: Raktr::Queue
- Inherits:
-
Object
- Object
- Raktr::Queue
- 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.
Instance Attribute Summary collapse
- #raktr ⇒ Raktr readonly
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(reactor) ⇒ Queue
constructor
A new instance of Queue.
-
#num_waiting ⇒ Integer
Number of jobs that are currently waiting on the Queue for items to appear.
- #pop(&block) ⇒ Object
- #push(item) ⇒ Object (also: #<<)
-
#size ⇒ Integer
Queue size.
Constructor Details
#initialize(reactor) ⇒ Queue
Returns a new instance of Queue.
31 32 33 34 35 |
# File 'lib/raktr/queue.rb', line 31 def initialize( reactor ) @raktr = reactor @items = [] @waiting = [] end |
Instance Attribute Details
Instance Method Details
#empty? ⇒ Boolean
Note:
This is a peek, it’s not thread safe, and may only tend toward accuracy.
67 68 69 |
# File 'lib/raktr/queue.rb', line 67 def empty? @items.empty? end |
#num_waiting ⇒ Integer
Note:
Accuracy cannot be guaranteed.
Returns 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
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: <<
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 |
#size ⇒ Integer
Note:
This is a peek, it’s not thread safe, and may only tend toward accuracy.
Returns Queue size.
75 76 77 |
# File 'lib/raktr/queue.rb', line 75 def size @items.size end |