Class: BackgroundQueue::ServerLib::SortedWorkers
- Inherits:
-
Object
- Object
- BackgroundQueue::ServerLib::SortedWorkers
- Defined in:
- lib/background_queue/server_lib/sorted_workers.rb
Overview
we want a list of workers where the first in the list is the next worker to use. the next worker to use is the worker with the least number of running connections
Instance Attribute Summary collapse
-
#worker_list ⇒ Object
readonly
Returns the value of attribute worker_list.
Instance Method Summary collapse
-
#add_worker(worker) ⇒ Object
add the worker back in the correct position.
- #adjust_worker(worker) ⇒ Object
-
#initialize ⇒ SortedWorkers
constructor
A new instance of SortedWorkers.
- #remove_worker(worker) ⇒ Object
Constructor Details
#initialize ⇒ SortedWorkers
Returns a new instance of SortedWorkers.
8 9 10 |
# File 'lib/background_queue/server_lib/sorted_workers.rb', line 8 def initialize @worker_list = [] end |
Instance Attribute Details
#worker_list ⇒ Object (readonly)
Returns the value of attribute worker_list.
6 7 8 |
# File 'lib/background_queue/server_lib/sorted_workers.rb', line 6 def worker_list @worker_list end |
Instance Method Details
#add_worker(worker) ⇒ Object
add the worker back in the correct position
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/background_queue/server_lib/sorted_workers.rb', line 13 def add_worker(worker) idx = 0 while idx < @worker_list.length && @worker_list[idx].connections < worker.connections idx += 1 end if idx == 0 @worker_list.unshift(worker) else @worker_list.insert(idx, worker) end end |
#adjust_worker(worker) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/background_queue/server_lib/sorted_workers.rb', line 30 def adjust_worker(worker) idx = @worker_list.index(worker) raise "Worker not found (#{worker.inspect} not in #{@worker_list.inspect})" if idx.nil? swap_idx = idx - 1 while swap_idx >= 0 && @worker_list[swap_idx].connections > worker.connections swap_idx -= 1 end swap_idx += 1 if swap_idx == idx #we didnt move forward, try backwards swap_idx = idx + 1 while swap_idx < @worker_list.length && @worker_list[swap_idx].connections < worker.connections swap_idx += 1 end swap_idx -= 1 end if swap_idx != idx tmp = @worker_list[swap_idx] @worker_list[swap_idx] = @worker_list[idx] @worker_list[idx] = tmp end end |
#remove_worker(worker) ⇒ Object
25 26 27 |
# File 'lib/background_queue/server_lib/sorted_workers.rb', line 25 def remove_worker(worker) @worker_list.delete(worker) end |