Class: CookbookOmnifetch::ThreadedJobQueue
- Inherits:
-
Object
- Object
- CookbookOmnifetch::ThreadedJobQueue
- Defined in:
- lib/cookbook-omnifetch/threaded_job_queue.rb
Overview
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.
Instance Method Summary collapse
- #<<(job) ⇒ Object
-
#initialize ⇒ ThreadedJobQueue
constructor
A new instance of ThreadedJobQueue.
- #process(concurrency = 10) ⇒ Object
Constructor Details
#initialize ⇒ ThreadedJobQueue
Returns a new instance of ThreadedJobQueue.
24 25 26 27 |
# File 'lib/cookbook-omnifetch/threaded_job_queue.rb', line 24 def initialize @queue = Queue.new @lock = Mutex.new end |
Instance Method Details
#<<(job) ⇒ Object
29 30 31 |
# File 'lib/cookbook-omnifetch/threaded_job_queue.rb', line 29 def <<(job) @queue << job end |
#process(concurrency = 10) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# 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 |