Class: WorkQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/scout/work_queue.rb,
lib/scout/work_queue/socket.rb,
lib/scout/work_queue/worker.rb

Defined Under Namespace

Classes: Socket, Worker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workers = 0, &block) ⇒ WorkQueue

Returns a new instance of 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

#callbackObject

Returns the value of attribute callback.



7
8
9
# File 'lib/scout/work_queue.rb', line 7

def callback
  @callback
end

#worker_procObject

Returns the value of attribute worker_proc.



7
8
9
# File 'lib/scout/work_queue.rb', line 7

def worker_proc
  @worker_proc
end

#workersObject

Returns the value of attribute workers.



7
8
9
# File 'lib/scout/work_queue.rb', line 7

def workers
  @workers
end

Instance Method Details

#abortObject



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/scout/work_queue.rb', line 140

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

#cleanObject



163
164
165
166
167
# File 'lib/scout/work_queue.rb', line 163

def clean
  @waiter.join if @waiter 
  @input.clean
  @output.clean
end

#closeObject



152
153
154
155
156
157
158
159
160
161
# File 'lib/scout/work_queue.rb', line 152

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
    end
  end
end

#ignore_ouputObject



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



169
170
171
172
173
174
175
176
177
# File 'lib/scout/work_queue.rb', line 169

def join(clean = true)
  close
  begin
    @waiter.join if @waiter
    @reader.join if @reader
  ensure
    self.clean if clean
  end
end

#new_workerObject



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
# 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 && @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
    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["name"] = "Worker waiter #{queue_id} worker #{w.pid}"
          pid, status = Process.wait2 w.pid
          remove_worker(pid) if pid
        end
        Thread.pass until t["name"]
        t
      end
      threads.each do |t| t.join end
    end
  end

  Thread.pass until @waiter["name"]
end

#queue_idObject



26
27
28
# File 'lib/scout/work_queue.rb', line 26

def queue_id
  [object_id, Process.pid] * "@"
end

#remove_one_workerObject



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



130
131
132
133
134
135
136
137
138
# File 'lib/scout/work_queue.rb', line 130

def write(obj)
  begin
    @input.write obj
  rescue Exception
    raise $! unless @input.exception
  ensure
    raise @input.exception if @input.exception
  end
end