From b98a3ebcf176b79f120f51844b09a5078694b230 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Tue, 24 Nov 2026 14:27:29 +0100 Subject: [PATCH] Offline build: retry a stalled Geofabrik download (3 attempts, 120 s read timeout) and report network errors as a BuildError; a timed-out Last-Modified HEAD no longer fails the build --- lib/maptool/pmtiles_builder.rb | 22 +++++++++++++++++++--- test/test_pmtiles_builder.rb | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/maptool/pmtiles_builder.rb b/lib/maptool/pmtiles_builder.rb index 46be5a8..3db2082 100644 --- a/lib/maptool/pmtiles_builder.rb +++ b/lib/maptool/pmtiles_builder.rb @@ -522,13 +522,20 @@ module Maptool # meta / update-check below. Geofabrik 302s (e.g. .../europe/austria # redirecting to wherever actually serves it) — followed manually # since request_get's block form doesn't do this itself. - def self.download_extract(url, dest, limit: 5) + # Geofabrik's mirrors stall now and then (Net::ReadTimeout after 60 s + # without a byte); a stall shouldn't fail an hour-long build, so the + # download starts over a few times before giving up. + # ponytail: restarts from zero, add a Range resume if multi-GB extracts keep stalling. + NETWORK_ERRORS = [Net::OpenTimeout, Net::ReadTimeout, EOFError, IOError, SocketError, SystemCallError, OpenSSL::SSL::SSLError].freeze + DOWNLOAD_ATTEMPTS = 3 + + def self.download_extract(url, dest, limit: 5, attempt: 1) raise BuildError, "too many redirects downloading #{url}" if limit.zero? uri = URI(url) last_modified = nil redirect_to = nil - Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http| + Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", read_timeout: 120) do |http| # Net::HTTP's default (true) quietly accepts a body cut short of # its Content-Length; a dropped connection must fail the download. http.ignore_eof = false @@ -556,6 +563,11 @@ module Maptool return download_extract(redirect_to, dest, limit: limit - 1) if redirect_to last_modified + rescue *NETWORK_ERRORS => e + raise BuildError, "downloading #{url} failed #{attempt}× (#{e.class})" if attempt >= DOWNLOAD_ATTEMPTS + + sleep 10 * attempt + download_extract(url, dest, limit: limit, attempt: attempt + 1) end # Sidecar recording which Geofabrik sources (country code, slug, and @@ -597,7 +609,11 @@ module Maptool raise BuildError, "too many redirects reaching #{url}" if limit.zero? uri = URI(url) - response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") { |http| http.head(uri) } + response = begin + Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") { |http| http.head(uri) } + rescue *NETWORK_ERRORS => e + raise BuildError, "could not reach #{url} (#{e.class})" + end case response when Net::HTTPSuccess then response["last-modified"] when Net::HTTPRedirection then fetch_geofabrik_last_modified(URI.join(url, response["location"]).to_s, limit: limit - 1) diff --git a/test/test_pmtiles_builder.rb b/test/test_pmtiles_builder.rb index 56008ec..a1d3898 100644 --- a/test/test_pmtiles_builder.rb +++ b/test/test_pmtiles_builder.rb @@ -168,6 +168,23 @@ class PmtilesBuilderTest < Minitest::Test ] end + # A stalled Geofabrik connection is retried, then fails the build with a + # BuildError (shown in the UI) instead of a raw Net::ReadTimeout. + def test_download_extract_retries_stalls_then_gives_up + calls = 0 + stalled = ->(*, **) { calls += 1; raise Net::ReadTimeout } + builder = Maptool::PmtilesBuilder + builder.define_singleton_method(:sleep) { |_| nil } + Net::HTTP.singleton_class.alias_method(:real_start, :start) + Net::HTTP.define_singleton_method(:start, &stalled) + error = assert_raises(Maptool::PmtilesBuilder::BuildError) { builder.download_extract("https://example.invalid/x.osm.pbf", File.join(@data_dir, "x")) } + assert_match(/failed 3×/, error.message) + assert_equal 3, calls + ensure + builder.singleton_class.send(:remove_method, :sleep) + Net::HTTP.singleton_class.alias_method(:start, :real_start) + end + def test_offline_and_terrain_status_start_idle assert_equal({ state: "idle" }, Maptool::PmtilesBuilder.offline_status) assert_equal({ state: "idle" }, Maptool::PmtilesBuilder.terrain_status)