Class: WorkQueue::FairQueue
- Inherits:
-
Object
- Object
- WorkQueue::FairQueue
- Defined in:
- lib/work_queue.rb
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(limit, &blk) ⇒ FairQueue
constructor
A new instance of FairQueue.
- #push(task, force:) ⇒ Object
- #shift ⇒ Object
Constructor Details
#initialize(limit, &blk) ⇒ FairQueue
Returns a new instance of FairQueue.
54 55 56 57 58 |
# File 'lib/work_queue.rb', line 54 def initialize(limit, &blk) @limit = limit @size = 0 @elements = Hash.new { |h, k| h[k] = blk.call } end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
52 53 54 |
# File 'lib/work_queue.rb', line 52 def size @size end |
Instance Method Details
#empty? ⇒ Boolean
82 83 84 |
# File 'lib/work_queue.rb', line 82 def empty? @elements.empty? end |
#push(task, force:) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/work_queue.rb', line 60 def push(task, force:) raise WorkQueueFull if !force && @size >= @limit key, task = task.values_at(:key, :task) @elements[key].push(task, force: force) @size += 1 nil end |
#shift ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/work_queue.rb', line 68 def shift unless @elements.empty? key, queue = @elements.shift task = queue.shift @elements[key] = queue unless queue.empty? @size -= 1 { key: key, task: task } end end |