Sha256: ce8ce4c9507848c623aa4b3301b22fb428a895a19a5e8e220991c2899ac7b3c9

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

# Copyright:: Copyright 2014-2016, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module CookbookOmnifetch

  # This class is copied from the Chef codebase:
  # https://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.
  class ThreadedJobQueue
    def initialize
      @queue = Queue.new
      @lock = Mutex.new
    end

    def <<(job)
      @queue << job
    end

    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 { |worker| worker.join }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cookbook-omnifetch-0.8.1 lib/cookbook-omnifetch/threaded_job_queue.rb
cookbook-omnifetch-0.8.0 lib/cookbook-omnifetch/threaded_job_queue.rb
cookbook-omnifetch-0.7.0 lib/cookbook-omnifetch/threaded_job_queue.rb