def maintain_worker_count
off = num_workers - WORKERS.size
if off.zero?
return
elsif off == num_workers
@spawn_attempts ||= 0
sleep 1 if @spawn_attempts > 1
if timeout > 0 && @spawn_attempts > timeout
message = "Unable to spawn workers after #{@spawn_attempts} attempts"
logger.error(message)
instrument 'error', :error => message
master_quit
return
end
@spawn_attempts += 1
else
@spawn_attempts = nil
end
return spawn_workers if off > 0
WORKERS.dup.each_pair do |wpid, worker|
worker.id >= num_workers && kill_worker(:QUIT, wpid) rescue nil
end
end