Module: Capistrano::ASG::Rolling::Parallel

Defined in:
lib/capistrano/asg/rolling/parallel.rb

Overview

Simple helper for running code in parallel.

Class Method Summary collapse

Class Method Details

.run(work) ⇒ Object

Runs the given block once per element of ‘work`, in parallel.

All threads are joined before this method returns, so a failure in one block does not abandon the other in-flight threads. If any block raises, the first error is re-raised after all threads have completed; additional errors are surfaced via ‘Kernel.warn`.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/capistrano/asg/rolling/parallel.rb', line 18

def run(work)
  results = Concurrent::Array.new
  errors = Concurrent::Array.new

  threads = work.map do |w|
    Thread.new do
      results << yield(w)
    rescue StandardError => e
      errors << e
    end
  end

  threads.each(&:join)

  if errors.any?
    errors.drop(1).each do |e|
      Kernel.warn("WARNING: parallel task failed: #{e.class}: #{e.message}")
    end
    raise errors.first
  end

  results
end