Class: Tap::App::Queue

Inherits:
Monitor
  • Object
show all
Defined in:
lib/tap/app/queue.rb

Overview

Queue allows thread-safe enqueing and dequeing of nodes and inputs for execution.

API

The following methods are required in alternative implementations of an applicaton queue, where a job is a [node, inputs] array:

enq(node, inputs)     # pushes the job onto the queue
unshift(node, inputs) # unshifts the job onto the queue
deq                   # shifts a job off the queue
size                  # returns the number of jobs in the queue
clear                 # clears the queue, returns current jobs
synchronize           # yields to the block
to_a                  # returns the queue as an array

Note that synchronize must be implemented even if it does nothing but yield to the block.

Instance Method Summary collapse

Constructor Details

#initializeQueue

Creates a new Queue



27
28
29
30
# File 'lib/tap/app/queue.rb', line 27

def initialize
  super
  @queue = []
end

Instance Method Details

#clearObject

Clears self and returns an array of the currently enqueued jobs.



60
61
62
63
64
65
66
# File 'lib/tap/app/queue.rb', line 60

def clear
  synchronize do
    current = @queue
    @queue = []
    current
  end
end

#deqObject

Dequeues the next job as an array like [node, inputs]. Returns nil if the queue is empty.



48
49
50
# File 'lib/tap/app/queue.rb', line 48

def deq
  synchronize { @queue.shift }
end

#enq(node, inputs) ⇒ Object

Enqueues the node and inputs as a job.



33
34
35
36
37
# File 'lib/tap/app/queue.rb', line 33

def enq(node, inputs)
  synchronize do
    @queue.push [node, inputs]
  end
end

#sizeObject

Returns the number of enqueued jobs.



53
54
55
56
57
# File 'lib/tap/app/queue.rb', line 53

def size
  synchronize do
    @queue.size
  end
end

#to_aObject

Returns the enqueued jobs as an array.



69
70
71
72
73
# File 'lib/tap/app/queue.rb', line 69

def to_a
  synchronize do
    @queue.dup
  end
end

#unshift(node, inputs) ⇒ Object

Enqueues the node and inputs, but to the top of the queue.



40
41
42
43
44
# File 'lib/tap/app/queue.rb', line 40

def unshift(node, inputs)
  synchronize do
    @queue.unshift [node, inputs]
  end
end