Class: ConcurrentWorker::WorkerPool

Inherits:
Array
  • Object
show all
Defined in:
lib/concurrent_worker/workerpool.rb

Defined Under Namespace

Classes: ReadyWorkerQueue

Instance Method Summary collapse

Constructor Details

#initialize(*args, **options, &work_block) ⇒ WorkerPool

Returns a new instance of WorkerPool.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/concurrent_worker/workerpool.rb', line 65

def initialize(*args, **options, &work_block)
  @args = args
  
  @options = options
  @max_num = @options[:pool_max] || 8
  @set_blocks = []
  if work_block
    @set_blocks.push([:work_block, work_block])
  end

  @ready_queue = ReadyWorkerQueue.new
  
  @result_callbacks = []
  
  @snd_queue_max = @options[:snd_queue_max]||2
  @req_mutex   = Mutex.new
  @req_counter = RequestCounter.new

  @snd_queue = Queue.new
  @snd_thread = set_snd_thread
  
  @recv_queue = Queue.new
  @recv_thread = set_recv_thread
end

Instance Method Details

#add_callback(&callback) ⇒ Object



90
91
92
93
# File 'lib/concurrent_worker/workerpool.rb', line 90

def add_callback(&callback)
  raise "block is nil" unless callback
  @result_callbacks.push(callback)
end

#add_finished_callback(&callback) ⇒ Object



95
96
97
98
# File 'lib/concurrent_worker/workerpool.rb', line 95

def add_finished_callback(&callback)
  raise "block is nil" unless callback
  @finished_callbacks.push(callback)
end

#available_workerObject



34
35
36
37
38
39
40
41
42
# File 'lib/concurrent_worker/workerpool.rb', line 34

def available_worker
  delete_if{ |w| w.queue_closed? && w.join }
  deploy_worker if need_new_worker?
  if @snd_queue_max > 0
    @ready_queue.pop
  else
    sort_by{ |w| w.req_counter.size }.first
  end
end

#deploy_workerObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/concurrent_worker/workerpool.rb', line 121

def deploy_worker
  defined?(@work_block) || @work_block = nil
  worker_options = {
    type:                       @options[:type],
    snd_queue_max:              @snd_queue_max,
    result_callback_interrupt:  :never,
    retired_callback_interrupt: :never
  }
  w = Worker.new(*@args, worker_options, &@work_block)
  
  @set_blocks.each do |symbol, block|
    w.set_block(symbol, &block)
  end
  
  worker_pool_com_setting(w)
  w.run
  
  self.push(w)
  w
end

#joinObject



155
156
157
158
159
160
161
162
# File 'lib/concurrent_worker/workerpool.rb', line 155

def join
  @req_counter.wait_until_less_than(1)
  self.shift.join until self.empty?
  @recv_queue.push(nil)
  @recv_thread.join
  @snd_queue.push(nil)
  @snd_thread.join
end

#need_new_worker?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/concurrent_worker/workerpool.rb', line 30

def need_new_worker?
  self.size < @max_num && self.select{ |w| w.queue_empty? }.empty?
end

#queue_closed?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/concurrent_worker/workerpool.rb', line 26

def queue_closed?
  @req_counter.closed?
end

#req(*args, &work_block) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/concurrent_worker/workerpool.rb', line 146

def req(*args, &work_block)
  @req_mutex.synchronize do
    @req_counter.wait_until_less_than(@max_num * @snd_queue_max) if @snd_queue_max > 0
    @req_counter.push(true)
    @snd_queue.push([args, work_block])
    true
  end
end

#set_block(symbol, &block) ⇒ Object



142
143
144
# File 'lib/concurrent_worker/workerpool.rb', line 142

def set_block(symbol, &block)
  @set_blocks.push([symbol, block])
end

#set_recv_threadObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/concurrent_worker/workerpool.rb', line 54

def set_recv_thread
  Thread.new do
    while result = @recv_queue.pop
      @result_callbacks.each do |callback|
        callback.call(*result[0])
      end
      @req_counter.pop
    end
  end
end

#set_snd_threadObject



44
45
46
47
48
49
50
51
52
# File 'lib/concurrent_worker/workerpool.rb', line 44

def set_snd_thread
  Thread.new do
    while req = @snd_queue.pop
      (args, work_block) = req
      until available_worker.req(*args, &work_block)
      end
    end
  end
end

#worker_pool_com_setting(w) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/concurrent_worker/workerpool.rb', line 100

def worker_pool_com_setting(w)
  w.add_callback do |*args|
    @recv_queue.push([args])
    @ready_queue.push(w) if @snd_queue_max > 0
  end

  w.add_retired_callback do
    w.undone_requests.each do |req|
      @snd_queue.push(req)
    end
    @ready_queue.push(w) if @snd_queue_max > 0
  end

  if w.snd_queue_max > 0
    w.snd_queue_max.times do
      @ready_queue.push(w)
    end
  end
end