brew/Library/Homebrew/download_queue.rb

215 lines
6.2 KiB
Ruby
Raw Normal View History

# typed: strict
2024-07-14 11:42:22 -04:00
# frozen_string_literal: true
require "downloadable"
2024-07-16 10:18:26 -04:00
require "concurrent/promises"
2024-07-15 10:25:25 -04:00
require "concurrent/executors"
require "retryable_download"
2024-07-14 11:42:22 -04:00
module Homebrew
class DownloadQueue
sig { params(concurrency: Integer, retries: Integer, force: T::Boolean).void }
def initialize(concurrency:, retries:, force:)
@concurrency = concurrency
@quiet = T.let(concurrency > 1, T::Boolean)
@tries = T.let(retries + 1, Integer)
@force = force
@pool = T.let(Concurrent::FixedThreadPool.new(concurrency), Concurrent::FixedThreadPool)
2024-07-14 11:42:22 -04:00
end
2025-07-11 17:53:25 +01:00
sig { params(downloadable: T.any(Resource, Bottle, Cask::Download, Downloadable)).void }
def enqueue(downloadable)
downloads[downloadable] ||= Concurrent::Promises.future_on(
pool, RetryableDownload.new(downloadable, tries:), force, quiet
) do |download, force, quiet|
download.clear_cache if force
download.fetch(quiet:)
2024-07-14 11:42:22 -04:00
end
end
2024-09-07 14:45:30 +02:00
sig { void }
def start
if concurrency == 1
downloads.each do |downloadable, promise|
promise.wait!
rescue ChecksumMismatchError => e
opoo "#{downloadable.download_type.capitalize} reports different checksum: #{e.expected}"
Homebrew.failed = true if downloadable.is_a?(Resource::Patch)
end
else
spinner = Spinner.new
remaining_downloads = downloads.dup.to_a
previous_pending_line_count = 0
begin
$stdout.print Tty.hide_cursor
$stdout.flush
output_message = lambda do |downloadable, future, last|
status = case future.state
when :fulfilled
"#{Tty.green}✔︎#{Tty.reset}"
when :rejected
"#{Tty.red}#{Tty.reset}"
when :pending, :processing
"#{Tty.blue}#{spinner}#{Tty.reset}"
else
raise future.state.to_s
end
message = "#{downloadable.download_type.capitalize} #{downloadable.name}"
$stdout.print "#{status} #{message}#{"\n" unless last}"
$stdout.flush
if future.rejected?
if (e = future.reason).is_a?(ChecksumMismatchError)
opoo "#{downloadable.download_type.capitalize} reports different checksum: #{e.expected}"
Homebrew.failed = true if downloadable.is_a?(Resource::Patch)
next 2
else
message = future.reason.to_s
onoe message
Homebrew.failed = true
next message.count("\n")
end
end
1
end
until remaining_downloads.empty?
begin
finished_states = [:fulfilled, :rejected]
finished_downloads, remaining_downloads = remaining_downloads.partition do |_, future|
finished_states.include?(future.state)
end
finished_downloads.each do |downloadable, future|
previous_pending_line_count -= 1
$stdout.print Tty.clear_to_end
$stdout.flush
output_message.call(downloadable, future, false)
end
previous_pending_line_count = 0
max_lines = [concurrency, Tty.height].min
remaining_downloads.each_with_index do |(downloadable, future), i|
break if previous_pending_line_count >= max_lines
$stdout.print Tty.clear_to_end
$stdout.flush
last = i == max_lines - 1 || i == remaining_downloads.count - 1
previous_pending_line_count += output_message.call(downloadable, future, last)
end
if previous_pending_line_count.positive?
if (previous_pending_line_count - 1).zero?
$stdout.print Tty.move_cursor_beginning
else
$stdout.print Tty.move_cursor_up_beginning(previous_pending_line_count - 1)
end
$stdout.flush
end
sleep 0.05
rescue Interrupt
remaining_downloads.each do |_, future|
# FIXME: Implement cancellation of running downloads.
end
cancel
if previous_pending_line_count.positive?
$stdout.print Tty.move_cursor_down(previous_pending_line_count - 1)
$stdout.flush
end
raise
end
end
ensure
$stdout.print Tty.show_cursor
$stdout.flush
end
end
2024-09-07 14:45:30 +02:00
end
2025-07-11 17:53:25 +01:00
sig { void }
def wait
downloads.each do |downloadable, promise|
promise.wait!
end
end
2024-07-14 11:42:22 -04:00
sig { void }
def shutdown
pool.shutdown
2024-09-07 14:45:30 +02:00
pool.wait_for_termination
2024-07-14 11:42:22 -04:00
end
private
sig { void }
def cancel
# FIXME: Implement graceful cancellation of running downloads based on
# https://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Cancellation.html
# instead of killing the whole thread pool.
pool.kill
end
sig { returns(Concurrent::FixedThreadPool) }
attr_reader :pool
sig { returns(Integer) }
attr_reader :concurrency
sig { returns(Integer) }
attr_reader :tries
sig { returns(T::Boolean) }
attr_reader :force
sig { returns(T::Boolean) }
attr_reader :quiet
2025-07-11 17:53:25 +01:00
sig { returns(T::Hash[T.any(Resource, Bottle, Cask::Download, Downloadable), Concurrent::Promises::Future]) }
def downloads
2025-07-11 17:53:25 +01:00
@downloads ||= T.let({}, T.nilable(T::Hash[T.any(Resource, Bottle, Cask::Download, Downloadable),
Concurrent::Promises::Future]))
end
class Spinner
FRAMES = [
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
].freeze
sig { void }
def initialize
@start = T.let(Time.now, Time)
@i = T.let(0, Integer)
end
sig { returns(String) }
def to_s
now = Time.now
if @start + 0.1 < now
@start = now
@i = (@i + 1) % FRAMES.count
end
FRAMES.fetch(@i)
end
end
2024-07-14 11:42:22 -04:00
end
end