Class: Gitingest::ContentFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/gitingest/content_fetcher.rb

Constant Summary collapse

BUFFER_SIZE =
250
LOCAL_BUFFER_THRESHOLD =
50
DEFAULT_THREAD_COUNT =
[Concurrent.processor_count, 8].min
DEFAULT_THREAD_TIMEOUT =

seconds

60

Instance Method Summary collapse

Constructor Details

#initialize(client, repository, files, logger, options = {}) ⇒ ContentFetcher

Returns a new instance of ContentFetcher.



13
14
15
16
17
18
19
20
# File 'lib/gitingest/content_fetcher.rb', line 13

def initialize(client, repository, files, logger, options = {})
  @client = client
  @repository = repository
  @files = files
  @logger = logger
  @threads = options[:threads] || DEFAULT_THREAD_COUNT
  @thread_timeout = options[:thread_timeout] || DEFAULT_THREAD_TIMEOUT
end

Instance Method Details

#fetch(output) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gitingest/content_fetcher.rb', line 22

def fetch(output)
  @logger.debug "Using thread pool with #{@threads} threads"
  buffer = []
  progress = ProgressIndicator.new(@files.size, @logger)
  thread_buffers = Concurrent::Map.new
  mutex = Mutex.new
  errors = Concurrent::Array.new
  pool = Concurrent::FixedThreadPool.new(@threads)
  prioritized_files = prioritize_files(@files)

  prioritized_files.each_with_index do |repo_file, index|
    pool.post do
      thread_id = Thread.current.object_id
      thread_buffers[thread_id] ||= []
      local_buffer = thread_buffers[thread_id]
      begin
        content = fetch_file_content_with_retry(repo_file.sha)
        local_buffer << format_file_content(repo_file.path, content)
        if local_buffer.size >= LOCAL_BUFFER_THRESHOLD
          mutex.synchronize do
            buffer.concat(local_buffer)
            write_buffer(output, buffer) if buffer.size >= BUFFER_SIZE
            local_buffer.clear
          end
        end
        progress.update(index + 1)
      rescue Octokit::Error => e
        mutex.synchronize { errors << "Error fetching #{repo_file.path}: #{e.message}" }
        @logger.error "Error fetching #{repo_file.path}: #{e.message}"
      rescue StandardError => e
        mutex.synchronize { errors << "Unexpected error processing #{repo_file.path}: #{e.message}" }
        @logger.error "Unexpected error processing #{repo_file.path}: #{e.message}"
      end
    end
  end

  pool.shutdown
  unless pool.wait_for_termination(@thread_timeout)
    @logger.warn "Thread pool did not shut down gracefully within #{@thread_timeout}s, forcing termination."
    pool.kill
  end

  mutex.synchronize do
    thread_buffers.each_value { |local_buffer| buffer.concat(local_buffer) unless local_buffer.empty? }
    write_buffer(output, buffer) unless buffer.empty?
  end

  return unless errors.any?

  @logger.warn "Completed with #{errors.size} errors"
  @logger.debug "First few errors: #{errors.first(3).join(", ")}" if @logger.debug?
end