@@ -315,23 +315,7 @@ def run(argv)
315
315
316
316
# At the end, we're going to return whether or not this worker ever
317
317
# 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 )
335
319
action . failure
336
320
1
337
321
else
@@ -342,13 +326,11 @@ def run(argv)
342
326
343
327
private
344
328
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 )
350
332
workers =
351
- Etc . nprocessors . times . map do
333
+ [ Etc . nprocessors , queue . size ] . min . times . map do
352
334
Thread . new do
353
335
# Propagate errors in the worker threads up to the parent thread.
354
336
Thread . current . abort_on_exception = true
@@ -360,15 +342,33 @@ def with_workers(queue)
360
342
361
343
# While there is still work left to do, shift off the queue and
362
344
# 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
364
364
365
365
# At the end, we're going to return whether or not this worker
366
366
# ever encountered an error.
367
367
errored
368
368
end
369
369
end
370
370
371
- workers . inject ( false ) { | accum , thread | accum || thread . value }
371
+ workers . map ( & :value ) . inject ( :| )
372
372
end
373
373
374
374
# Highlights a snippet from a source and parse error.
0 commit comments