Class: WorkQueue
- Inherits:
-
Object
- Object
- WorkQueue
- Defined in:
- lib/scout/work_queue.rb,
lib/scout/work_queue/socket.rb,
lib/scout/work_queue/worker.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#callback ⇒ Object
Returns the value of attribute callback.
-
#worker_proc ⇒ Object
Returns the value of attribute worker_proc.
-
#workers ⇒ Object
Returns the value of attribute workers.
Instance Method Summary collapse
- #abort ⇒ Object
- #add_worker(&block) ⇒ Object
- #clean ⇒ Object
- #close ⇒ Object
- #ignore_ouput ⇒ Object
-
#initialize(workers = 0, &block) ⇒ WorkQueue
constructor
A new instance of WorkQueue.
- #join(clean = true) ⇒ Object
- #new_worker ⇒ Object
- #process(&callback) ⇒ Object
- #queue_id ⇒ Object
- #remove_one_worker ⇒ Object
- #remove_worker(pid) ⇒ Object
- #write(obj) ⇒ Object
Constructor Details
#initialize(workers = 0, &block) ⇒ WorkQueue
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/scout/work_queue.rb', line 15 def initialize(workers = 0, &block) workers = workers.to_i if String === workers @input = WorkQueue::Socket.new @output = WorkQueue::Socket.new @workers = workers.times.collect{ new_worker } @worker_proc = block @worker_mutex = Mutex.new @removed_workers = [] Log.medium "Starting queue #{queue_id} with workers: #{Log.fingerprint @workers.collect{|w| w.worker_short_id }} and sockets #{@input.socket_id} and #{@output.socket_id}" end |
Instance Attribute Details
#callback ⇒ Object
Returns the value of attribute callback.
7 8 9 |
# File 'lib/scout/work_queue.rb', line 7 def callback @callback end |
#worker_proc ⇒ Object
Returns the value of attribute worker_proc.
7 8 9 |
# File 'lib/scout/work_queue.rb', line 7 def worker_proc @worker_proc end |
#workers ⇒ Object
Returns the value of attribute workers.
7 8 9 |
# File 'lib/scout/work_queue.rb', line 7 def workers @workers end |
Instance Method Details
#abort ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/scout/work_queue.rb', line 154 def abort @aborted = true Log.low "Aborting #{@workers.length} workers in queue #{queue_id}" @worker_mutex.synchronize do @workers.each do |w| ScoutSemaphore.post_semaphore(@output.write_sem) ScoutSemaphore.post_semaphore(@input.read_sem) w.abort end end end |
#add_worker(&block) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/scout/work_queue.rb', line 30 def add_worker(&block) worker = new_worker @worker_mutex.synchronize do @workers.push(worker) if block_given? worker.process @input, @output, &block else worker.process @input, @output, &@worker_proc end end worker end |
#clean ⇒ Object
177 178 179 180 181 |
# File 'lib/scout/work_queue.rb', line 177 def clean @waiter.join if @waiter && Thread.current != @waiter @input.clean @output.clean end |
#close ⇒ Object
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/scout/work_queue.rb', line 166 def close return if @closed || @aborted @closed = true @worker_mutex.synchronize{ @workers.length }.times do begin @input.write DoneProcessing.new() unless @input.closed_write? rescue IOError,Errno::ENOENT end end end |
#ignore_ouput ⇒ Object
43 44 45 |
# File 'lib/scout/work_queue.rb', line 43 def ignore_ouput @workers.each{|w| w.ignore_ouput = true } end |
#join(clean = true) ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'lib/scout/work_queue.rb', line 183 def join(clean = true) close begin @waiter.join if @waiter @reader.join if @reader ensure self.clean if clean end end |
#new_worker ⇒ Object
9 10 11 12 13 |
# File 'lib/scout/work_queue.rb', line 9 def new_worker worker = Worker.new worker.queue_id = queue_id worker end |
#process(&callback) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/scout/work_queue.rb', line 64 def process(&callback) @workers.each do |w| w.process @input, @output, &@worker_proc end @reader = Thread.new(Thread.current) do |parent| begin Thread.current.report_on_exception = false Thread.current["name"] = "Output reader #{queue_id}" @done_workers ||= [] while true obj = @output.read if DoneProcessing === obj done = @worker_mutex.synchronize do Log.low "Worker #{obj.pid} from #{queue_id} done" @done_workers << obj.pid #@closed && (@workers.empty? || @workers.length == @removed_workers.length + @done_workers.length) @closed && @done_workers.length == @removed_workers.length + @workers.length end break if done elsif Exception === obj raise obj else callback.call obj if callback end end @waiter.join if @workers.any? rescue DoneProcessing rescue Aborted rescue WorkerException Log.error "Exception in worker #{obj.pid} in queue #{queue_id}: #{obj.worker_exception.message}" self.abort @input.abort obj.worker_exception raise obj.worker_exception rescue Log.error "Exception processing output in queue #{queue_id}: #{$!.message}" self.abort raise $! end end Thread.pass until @reader["name"] Thread.pass until @worker_mutex.synchronize{ @workers.select{|w| w.pid.nil? }.empty? } @waiter = Thread.new do Thread.current.report_on_exception = false Thread.current["name"] = "Worker waiter #{queue_id}" while true break if @worker_mutex.synchronize{ @workers.empty? } threads = @workers.collect do |w| t = Thread.new do Thread.current.report_on_exception = false Thread.current["name"] = "Worker waiter #{queue_id} worker #{w.pid}" pid, status = Process.wait2 w.pid remove_worker(pid) if pid #@output.write WorkerException.new(Exception.new("Worker ended with status #{status.exitstatus}"), pid) unless status.success? raise Exception.new("Worker #{pid} ended with status #{status.exitstatus}") unless (status.success? || status.exitstatus == WorkQueue::Worker::EXIT_STATUS) end Thread.pass until t["name"] t end exceptions = [] threads.each do |t| begin t.join rescue Exception exceptions << $! end end raise exceptions.first if exceptions.any? end end Thread.pass until @waiter["name"] end |
#queue_id ⇒ Object
26 27 28 |
# File 'lib/scout/work_queue.rb', line 26 def queue_id [object_id, Process.pid] * "@" end |
#remove_one_worker ⇒ Object
47 48 49 |
# File 'lib/scout/work_queue.rb', line 47 def remove_one_worker @input.write DoneProcessing.new end |
#remove_worker(pid) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/scout/work_queue.rb', line 51 def remove_worker(pid) @worker_mutex.synchronize do worker = @workers.index{|w| w.pid == pid} if worker @workers.delete_at(worker) @removed_workers << pid Log.low "Removed worker #{pid} from #{queue_id}" else Log.medium "Worker #{pid} not from #{queue_id}" end end end |
#write(obj) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/scout/work_queue.rb', line 144 def write(obj) begin @input.write obj rescue Exception raise $! unless @input.exception ensure raise @input.exception if @input.exception end end |