Class: RbbtProcessQueue::RbbtProcessQueueWorker
- Inherits:
-
Object
- Object
- RbbtProcessQueue::RbbtProcessQueueWorker
- Defined in:
- lib/rbbt/util/concurrency/processes/worker.rb
Defined Under Namespace
Classes: Respawn
Constant Summary collapse
- WORKER_ABORT_SIGNAL =
15
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#callback_queue ⇒ Object
readonly
Returns the value of attribute callback_queue.
-
#cleanup ⇒ Object
readonly
Returns the value of attribute cleanup.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#stopping ⇒ Object
readonly
Returns the value of attribute stopping.
Instance Method Summary collapse
- #abort ⇒ Object
- #abort_and_join ⇒ Object
- #done? ⇒ Boolean
-
#initialize(queue, callback_queue = nil, cleanup = nil, respawn = false, offset = false, &block) ⇒ RbbtProcessQueueWorker
constructor
A new instance of RbbtProcessQueueWorker.
- #join ⇒ Object
- #run ⇒ Object
- #run_with_respawn(multiplier = nil) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(queue, callback_queue = nil, cleanup = nil, respawn = false, offset = false, &block) ⇒ RbbtProcessQueueWorker
Returns a new instance of RbbtProcessQueueWorker.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 191 def initialize(queue, callback_queue = nil, cleanup = nil, respawn = false, offset = false, &block) @queue, @callback_queue, @cleanup, @block, @offset = queue, callback_queue, cleanup, block, offset @pid = Process.fork do Misc.pre_fork Log::ProgressBar.add_offset @offset if @offset @cleanup.call if @cleanup @queue.close_write if @callback_queue Misc.purge_pipes(@callback_queue.swrite) @callback_queue.close_read else Misc.purge_pipes end if respawn run_with_respawn respawn else run end Log::ProgressBar.remove_offset @offset if @offset end end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
4 5 6 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 4 def block @block end |
#callback_queue ⇒ Object (readonly)
Returns the value of attribute callback_queue.
4 5 6 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 4 def callback_queue @callback_queue end |
#cleanup ⇒ Object (readonly)
Returns the value of attribute cleanup.
4 5 6 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 4 def cleanup @cleanup end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
4 5 6 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 4 def pid @pid end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
4 5 6 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 4 def queue @queue end |
#stopping ⇒ Object (readonly)
Returns the value of attribute stopping.
4 5 6 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 4 def stopping @stopping end |
Instance Method Details
#abort ⇒ Object
236 237 238 239 240 241 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 236 def abort begin Process.kill WORKER_ABORT_SIGNAL, @pid rescue Errno::ESRCH, Errno::ECHILD end end |
#abort_and_join ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 243 def abort_and_join self.abort begin Misc.insist([0,0.05,0.5,1,2]) do begin pid, status = Process.waitpid2 @pid, Process::WNOHANG Log.low "Abort and join of #{@pid}" return rescue Aborted abort raise rescue ProcessFailed Log.low "Abort and join of #{@pid} (ProcessFailed)" return rescue Errno::ESRCH, Errno::ECHILD Log.low "Already joined worker #{@pid}" return end end rescue Aborted retry end begin Log.low "Forcing abort of #{@pid}" Process.kill 9, @pid pid, status = Process.waitpid2 @pid rescue Errno::ESRCH, Errno::ECHILD Log.low "Force killed worker #{@pid}" end end |
#done? ⇒ Boolean
286 287 288 289 290 291 292 293 294 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 286 def done? begin Process.waitpid @pid, Process::WNOHANG rescue Errno::ECHILD true rescue false end end |
#join ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 217 def join return unless Misc.pid_exists? @pid begin pid, status = Process.waitpid2 @pid raise ProcessFailed.new @pid if not status.success? rescue Aborted self.abort raise Aborted rescue Errno::ESRCH, Errno::ECHILD Log.exception $! rescue ProcessFailed raise $! rescue Exception Log.exception $! raise $! end end |
#run ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 15 def run begin begin Signal.trap(:INT){ Kernel.exit! -1 } @respawn = false Signal.trap(:USR1){ @respawn = true } @stop = false Signal.trap(:USR2){ @stop = true } Signal.trap(WORKER_ABORT_SIGNAL){ Log.high "Worker #{Process.pid} signaled to abort" Kernel.exit! -1 } loop do p = @queue.pop next if p.nil? raise p if Exception === p raise p.first if Array === p and Exception === p.first begin res = @block.call *p @callback_queue.push res if @callback_queue rescue Respawn @callback_queue.push $!.payload if @callback_queue raise $! end raise Respawn if @respawn if @stop Log.high "Worker #{Process.pid} leaving" break end end rescue Respawn Kernel.exit! 28 rescue ClosedStream rescue Interrupt,Aborted Log.high "Worker #{Process.pid} aborted" rescue SemaphoreInterrupted retry unless @stop Log.high "Worker #{Process.pid} leaving" rescue Exception Log.high "Worker #{Process.pid} had exception: #{$!.}" @callback_queue.push($!) if @callback_queue Kernel.exit! -1 ensure @callback_queue.close_write if @callback_queue end rescue Aborted Log.high "Worker #{Process.pid} aborted" end Log.high "Worker #{Process.pid} completed" Kernel.exit! 0 end |
#run_with_respawn(multiplier = nil) ⇒ Object
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 81 def run_with_respawn(multiplier = nil) multiplier = case multiplier when String multiplier.to_s when Numeric multiplier.to_i else 3 end status = nil begin initial = Misc.memory_use(Process.pid) memory_cap = multiplier * initial @asked = false @monitored = false @monitor_thread = Thread.new do begin while true @monitored = true current_mem = @current ? Misc.memory_use(@current) : 0 if current_mem > memory_cap and not @asked Log.medium "Worker #{@current} for #{Process.pid} asked to respawn -- initial: #{initial} - multiplier: #{multiplier} - cap: #{memory_cap} - current: #{current_mem}" RbbtSemaphore.synchronize(@callback_queue.write_sem) do Process.kill "USR1", @current if @current end @asked = true end sleep 2 end rescue Log.exception $! end end while ! @monitored sleep 0.1 end @current = nil Signal.trap(:INT){ begin Process.kill :INT, @current if @current rescue Errno::ESRCH, Errno::ECHILD end } Signal.trap(:USR1){ begin Process.kill :USR1, @current if @current rescue Errno::ESRCH, Errno::ECHILD end } Signal.trap(:USR2){ begin Process.kill :USR2, @current if @current rescue Errno::ESRCH, Errno::ECHILD end } Signal.trap(WORKER_ABORT_SIGNAL){ Log.high "Killing respawned process #{@current}" begin Process.kill WORKER_ABORT_SIGNAL, @current if @current rescue Errno::ESRCH, Errno::ECHILD end } @current = Process.fork do run end Log.debug "Worker for #{Process.pid} started with pid #{@current} -- initial: #{initial} - multiplier: #{multiplier} - cap: #{memory_cap}" while true @prev = @current pid, status = Process.waitpid2 @current code = status.to_i >> 8 break unless code == 28 @current = Process.fork do run end @asked = false Log.high "Worker #{Process.pid} respawning from #{@prev} to #{@current}" end rescue Aborted, Interrupt Log.high "Worker #{Process.pid} aborted. Current #{@current} #{Misc.pid_exists?(@current) ? "exists" : "does not exist"}" Process.kill "INT", @current if Misc.pid_exists? @current @callback_queue.close_write if @callback_queue Kernel.exit! 0 rescue Exception raise $! ensure @monitor_thread.kill if @monitor_thread Process.kill "INT", @current if Misc.pid_exists? @current @callback_queue.close_write if @callback_queue end if status Log.high "Worker #{@current} (respawner #{Process.pid} ) completed with status #{status}" Kernel.exit! status.to_i >> 8 else Kernel.exit! -1 end end |