Module: Pod::PodGenerate::Parallel::BatchProcessor

Defined in:
lib/cocoapods-podgenerate/parallel/batch_processor.rb

Class Method Summary collapse

Class Method Details

.process(items, pool:, batch_size: nil) {|item| ... } ⇒ Array

Process items in batches using a thread pool.

Parameters:

  • items (Array)

    list of items to process

  • batch_size (Integer) (defaults to: nil)

    max items per batch (nil = auto-size)

  • pool (Concurrent::FixedThreadPool)

    the thread pool

Yields:

  • (item)

    block to process each item

Returns:

  • (Array)

    results in same order as input items



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cocoapods-podgenerate/parallel/batch_processor.rb', line 19

def self.process(items, pool:, batch_size: nil, &block)
  return [] if items.empty?

  results = Array.new(items.size)
  mutex = Mutex.new
  count = items.size
  completed = 0

  items.each_with_index do |item, idx|
    pool.post do
      begin
        result = block.call(item)
        mutex.synchronize { results[idx] = result }
      rescue StandardError => e
        mutex.synchronize do
          Pod::UI.warn "[cocoapods-podgenerate] BatchProcessor error on item #{idx}: #{e.message}"
        end
      ensure
        mutex.synchronize do
          completed += 1
        end
      end
    end
  end

  # Wait for all tasks to complete
  pool.wait_for_termination

  results
end