@@ -315,23 +315,7 @@ def run(argv)
315315
316316 # At the end, we're going to return whether or not this worker ever
317317 # encountered an error.
318- errored =
319- with_workers ( queue ) do |item |
320- action . run ( item )
321- false
322- rescue Parser ::ParseError => error
323- warn ( "Error: #{ error . message } " )
324- highlight_error ( error , item . source )
325- true
326- rescue Check ::UnformattedError , Debug ::NonIdempotentFormatError
327- true
328- rescue StandardError => error
329- warn ( error . message )
330- warn ( error . backtrace )
331- true
332- end
333-
334- if errored
318+ if process_queue ( queue , action )
335319 action . failure
336320 1
337321 else
@@ -342,13 +326,11 @@ def run(argv)
342326
343327 private
344328
345- def with_workers ( queue )
346- # If the queue is just 1 item, then we're not going to bother going
347- # through the whole ceremony of parallelizing the work.
348- return yield queue . shift if queue . size == 1
349-
329+ # Processes each item in the queue with the given action. Returns whether
330+ # or not any errors were encountered.
331+ def process_queue ( queue , action )
350332 workers =
351- Etc . nprocessors . times . map do
333+ [ Etc . nprocessors , queue . size ] . min . times . map do
352334 Thread . new do
353335 # Propagate errors in the worker threads up to the parent thread.
354336 Thread . current . abort_on_exception = true
@@ -360,15 +342,33 @@ def with_workers(queue)
360342
361343 # While there is still work left to do, shift off the queue and
362344 # process the item.
363- ( errored ||= yield queue . shift ) until queue . empty?
345+ until queue . empty?
346+ item = queue . shift
347+ errored |=
348+ begin
349+ action . run ( item )
350+ false
351+ rescue Parser ::ParseError => error
352+ warn ( "Error: #{ error . message } " )
353+ highlight_error ( error , item . source )
354+ true
355+ rescue Check ::UnformattedError ,
356+ Debug ::NonIdempotentFormatError
357+ true
358+ rescue StandardError => error
359+ warn ( error . message )
360+ warn ( error . backtrace )
361+ true
362+ end
363+ end
364364
365365 # At the end, we're going to return whether or not this worker
366366 # ever encountered an error.
367367 errored
368368 end
369369 end
370370
371- workers . inject ( false ) { | accum , thread | accum || thread . value }
371+ workers . map ( & :value ) . inject ( :| )
372372 end
373373
374374 # Highlights a snippet from a source and parse error.
0 commit comments