Class: WorkQueue::Worker
- Inherits:
-
Object
- Object
- WorkQueue::Worker
- Defined in:
- lib/scout/work_queue/worker.rb
Instance Attribute Summary collapse
-
#ignore_ouput ⇒ Object
Returns the value of attribute ignore_ouput.
-
#pid ⇒ Object
Returns the value of attribute pid.
-
#queue_id ⇒ Object
Returns the value of attribute queue_id.
Class Method Summary collapse
Instance Method Summary collapse
- #abort ⇒ Object
-
#initialize(ignore_ouput = false) ⇒ Worker
constructor
A new instance of Worker.
- #join ⇒ Object
- #process(input, output = nil, &block) ⇒ Object
- #run ⇒ Object
- #worker_id ⇒ Object
- #worker_short_id ⇒ Object
Constructor Details
#initialize(ignore_ouput = false) ⇒ Worker
Returns a new instance of Worker.
4 5 6 |
# File 'lib/scout/work_queue/worker.rb', line 4 def initialize(ignore_ouput = false) @ignore_output = ignore_ouput end |
Instance Attribute Details
#ignore_ouput ⇒ Object
Returns the value of attribute ignore_ouput.
3 4 5 |
# File 'lib/scout/work_queue/worker.rb', line 3 def ignore_ouput @ignore_ouput end |
#pid ⇒ Object
Returns the value of attribute pid.
3 4 5 |
# File 'lib/scout/work_queue/worker.rb', line 3 def pid @pid end |
#queue_id ⇒ Object
Returns the value of attribute queue_id.
3 4 5 |
# File 'lib/scout/work_queue/worker.rb', line 3 def queue_id @queue_id end |
Class Method Details
.join(workers) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/scout/work_queue/worker.rb', line 68 def self.join(workers) workers = [workers] unless Array === workers begin while pid = Process.wait status = $? worker = workers.select{|w| w.pid == pid }.first end rescue Errno::ECHILD end end |
Instance Method Details
#abort ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/scout/work_queue/worker.rb', line 54 def abort begin Log.medium "Aborting worker #{worker_id}" Process.kill "INT", @pid rescue Errno::ECHILD rescue Errno::ESRCH end end |
#join ⇒ Object
63 64 65 66 |
# File 'lib/scout/work_queue/worker.rb', line 63 def join Log.low "Joining worker #{worker_id}" Process.waitpid @pid end |
#process(input, output = nil, &block) ⇒ Object
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 |
# File 'lib/scout/work_queue/worker.rb', line 26 def process(input, output = nil, &block) run do begin if output Open.purge_pipes(output.swrite) else Open.purge_pipes end while obj = input.read if DoneProcessing === obj output.write DoneProcessing.new raise obj end res = block.call obj output.write res unless ignore_ouput || res == :ignore end rescue DoneProcessing rescue Interrupt rescue Exception output.write WorkerException.new($!, Process.pid) exit -1 ensure end exit 0 end end |
#run ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/scout/work_queue/worker.rb', line 16 def run @pid = Process.fork do Signal.trap("INT") do Kernel.exit! -1 end Log.low "Worker start #{worker_id}" yield end end |
#worker_id ⇒ Object
12 13 14 |
# File 'lib/scout/work_queue/worker.rb', line 12 def worker_id [worker_short_id, queue_id] * "->" end |
#worker_short_id ⇒ Object
8 9 10 |
# File 'lib/scout/work_queue/worker.rb', line 8 def worker_short_id [object_id, pid].compact * "@" end |