class FastlaneCore::QueueWorker

This dispatches jobs to worker threads and make it work in parallel. It's suitable for I/O bounds works and not for CPU bounds works. Use this when you have all the items that you'll process in advance. Simply enqueue them to this and call `QueueWorker#start`.

Constants

NUMBER_OF_THREADS

Public Class Methods

new(concurrency = NUMBER_OF_THREADS, &block) click to toggle source

@param concurrency (Numeric) - A number of threads to be created @param block (Proc) - A task you want to execute with enqueued items

# File fastlane_core/lib/fastlane_core/queue_worker.rb, line 13
def initialize(concurrency = NUMBER_OF_THREADS, &block)
  @concurrency = concurrency
  @block = block
  @queue = Queue.new
end

Public Instance Methods

batch_enqueue(jobs) click to toggle source

@param jobs (Array<Object>) - An array of arbitrary object that keeps parameters

# File fastlane_core/lib/fastlane_core/queue_worker.rb, line 25
def batch_enqueue(jobs)
  raise(ArgumentError, "Enqueue Array instead of #{jobs.class}") unless jobs.kind_of?(Array)
  jobs.each { |job| enqueue(job) }
end
enqueue(job) click to toggle source

@param job (Object) - An arbitrary object that keeps parameters

# File fastlane_core/lib/fastlane_core/queue_worker.rb, line 20
def enqueue(job)
  @queue.push(job)
end
start() click to toggle source

Call this after you enqueuned all the jobs you want to process This method blocks current thread until all the enqueued jobs are processed

# File fastlane_core/lib/fastlane_core/queue_worker.rb, line 32
def start
  @queue.close

  threads = []
  @concurrency.times do
    threads << Thread.new do
      job = @queue.pop
      while job
        @block.call(job)
        job = @queue.pop
      end
    end
  end

  threads.each(&:join)
end