lib/nanoc/extra/checking/checks/external_links.rb in nanoc-4.3.5 vs lib/nanoc/extra/checking/checks/external_links.rb in nanoc-4.3.6

- old
+ new

@@ -10,10 +10,12 @@ # @api private class ExternalLinks < ::Nanoc::Extra::Checking::Check identifiers :external_links, :elinks def run + require 'parallel' + # Find all broken external hrefs # TODO: de-duplicate this (duplicated in internal links check) filenames = output_filenames.select { |f| File.extname(f) == '.html' && !excluded_file?(f) } hrefs_with_filenames = ::Nanoc::Extra::LinkCollector.new(filenames, :external).filenames_per_href results = select_invalid(hrefs_with_filenames.keys) @@ -38,48 +40,11 @@ @href = href @explanation = explanation end end - class ArrayEnumerator - def initialize(array) - @array = array - @index = 0 - @mutex = Mutex.new - end - - def next - @mutex.synchronize do - @index += 1 - return @array[@index - 1] - end - end - end - def select_invalid(hrefs) - enum = ArrayEnumerator.new(hrefs.sort) - mutex = Mutex.new - invalid = Set.new - - threads = [] - 10.times do - threads << Thread.new do - loop do - href = enum.next - break if href.nil? - - res = validate(href) - next unless res - - mutex.synchronize do - invalid << res - end - end - end - end - threads.each(&:join) - - invalid + Parallel.map(hrefs, in_threads: 10) { |href| validate(href) }.compact end def validate(href) # Parse url = nil