Class: BackgroundQueue::ServerLib::PriorityQueue
- Inherits:
-
Object
- Object
- BackgroundQueue::ServerLib::PriorityQueue
show all
- Defined in:
- lib/background_queue/server_lib/priority_queue.rb
Overview
internally implemented using a list of queues this does not do any locking, subclasses should look after any locking
Instance Method Summary
collapse
Constructor Details
Returns a new instance of PriorityQueue.
6
7
8
9
10
11
12
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 6
def initialize
@queues = []
@items = {}
@stalled_items = {}
@stalled = false
@running_items = 0
end
|
Instance Method Details
#each_item(&block) ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 77
def each_item(&block)
for queue in @queues
for item in queue
block.call(item)
end
end
end
|
#empty? ⇒ Boolean
53
54
55
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 53
def empty?
@queues.empty?
end
|
#has_running_items? ⇒ Boolean
57
58
59
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 57
def has_running_items?
@running_items > 0
end
|
#number_if_items_at_priority(priority) ⇒ Object
65
66
67
68
69
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 65
def number_if_items_at_priority(priority)
q = get_queue_for_priority(priority, false)
return 0 if q.nil?
q.length
end
|
#number_of_priorities ⇒ Object
61
62
63
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 61
def number_of_priorities
@queues.length
end
|
#peek ⇒ Object
71
72
73
74
75
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 71
def peek
q = get_next_queue
return nil if q.nil?
q.first
end
|
#pop ⇒ Object
14
15
16
17
18
19
20
21
22
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 14
def pop
q = get_next_queue
return nil if q.nil?
item = q.shift
if q.empty?
remove_queue(q)
end
item
end
|
#priority ⇒ Object
the highest priority queue
40
41
42
43
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 40
def priority
return nil if @queues.empty?
get_next_queue.priority
end
|
#push(item) ⇒ Object
24
25
26
27
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 24
def push(item)
q = get_queue_for_priority(item.priority, true)
q.push(item)
end
|
#remove(item, override_priority = nil) ⇒ Object
29
30
31
32
33
34
35
36
37
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 29
def remove(item, override_priority=nil)
override_priority = item.priority if override_priority.nil?
q = get_queue_for_priority(override_priority, false)
raise "unable to get queue at priority #{override_priority} when removing" if q.nil?
q.delete_if { |q_item| q_item.id == item.id }
if q.empty?
remove_queue(q)
end
end
|
#stalled=(stall) ⇒ Object
49
50
51
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 49
def stalled=(stall)
@stalled = stall
end
|
#stalled? ⇒ Boolean
45
46
47
|
# File 'lib/background_queue/server_lib/priority_queue.rb', line 45
def stalled?
@stalled
end
|