Class: Tap::App::Queue
- Inherits:
-
Monitor
- Object
- Monitor
- Tap::App::Queue
- Defined in:
- lib/tap/app/queue.rb
Overview
Queue allows thread-safe enqueing and dequeing of tasks and inputs for execution.
API
The following methods are required in alternative implementations of an applicaton queue, where a job is a [task, input] array:
enq(task, input) # pushes the job onto the queue
unshift(task, input) # 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
-
#clear ⇒ Object
Clears self and returns an array of the currently enqueued jobs.
-
#deq ⇒ Object
Dequeues the next job.
-
#enq(task, input) ⇒ Object
Enqueues the task and input.
-
#initialize ⇒ Queue
constructor
Creates a new Queue.
-
#size ⇒ Object
Returns the number of enqueued jobs.
-
#to_a ⇒ Object
Returns the enqueued jobs as an array.
-
#unshift(task, input) ⇒ Object
Enqueues the task and input, but to the top of the queue.
Constructor Details
#initialize ⇒ Queue
Creates a new Queue
27 28 29 30 |
# File 'lib/tap/app/queue.rb', line 27 def initialize super @queue = [] end |
Instance Method Details
#clear ⇒ Object
Clears self and returns an array of the currently enqueued jobs.
59 60 61 62 63 64 65 |
# File 'lib/tap/app/queue.rb', line 59 def clear synchronize do current = @queue @queue = [] current end end |
#deq ⇒ Object
Dequeues the next job. Returns nil if the queue is empty.
47 48 49 |
# File 'lib/tap/app/queue.rb', line 47 def deq synchronize { @queue.shift } end |
#enq(task, input) ⇒ Object
Enqueues the task and input.
33 34 35 36 37 |
# File 'lib/tap/app/queue.rb', line 33 def enq(task, input) synchronize do @queue.push [task, input] end end |
#size ⇒ Object
Returns the number of enqueued jobs.
52 53 54 55 56 |
# File 'lib/tap/app/queue.rb', line 52 def size synchronize do @queue.size end end |
#to_a ⇒ Object
Returns the enqueued jobs as an array.
68 69 70 71 72 |
# File 'lib/tap/app/queue.rb', line 68 def to_a synchronize do @queue.dup end end |
#unshift(task, input) ⇒ Object
Enqueues the task and input, but to the top of the queue.
40 41 42 43 44 |
# File 'lib/tap/app/queue.rb', line 40 def unshift(task, input) synchronize do @queue.unshift [task, input] end end |