class CookbookOmnifetch::ThreadedJobQueue

This class is copied from the Chef codebase: github.com/chef/chef/blob/7f0b5150c32994b4ad593505172c5834a984b087/lib/chef/util/threaded_job_queue.rb

We do not re-use the code from Chef because we do not want to introduce a dependency on Chef in this library.

Public Class Methods

new() click to toggle source
# File lib/cookbook-omnifetch/threaded_job_queue.rb, line 24
def initialize
  @queue = Queue.new
  @lock = Mutex.new
end

Public Instance Methods

<<(job) click to toggle source
# File lib/cookbook-omnifetch/threaded_job_queue.rb, line 29
def <<(job)
  @queue << job
end
process(concurrency = 10) click to toggle source
# File lib/cookbook-omnifetch/threaded_job_queue.rb, line 33
def process(concurrency = 10)
  workers = (1..concurrency).map do
    Thread.new do
      loop do
        fn = @queue.pop
        fn.arity == 1 ? fn.call(@lock) : fn.call
      end
    end
  end
  workers.each { |worker| self << Thread.method(:exit) }
  workers.each(&:join)
end