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
CI / check (push) Successful in 8m50s
CI / deploy (push) Successful in 18s

This commit is contained in:
randy committed 2026-11-24 14:27:29 +01:00
1 parent 637f739e18
commit b98a3ebcf1
2 files changed
+36 -3

No files matched your search

+19 -3
View File
@@ -522,13 +522,20 @@ module Maptool
# meta / update-check below. Geofabrik 302s (e.g. .../europe/austria # meta / update-check below. Geofabrik 302s (e.g. .../europe/austria
# redirecting to wherever actually serves it) — followed manually # redirecting to wherever actually serves it) — followed manually
# since request_get's block form doesn't do this itself. # 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? raise BuildError, "too many redirects downloading #{url}" if limit.zero?
uri = URI(url) uri = URI(url)
last_modified = nil last_modified = nil
redirect_to = 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 # Net::HTTP's default (true) quietly accepts a body cut short of
# its Content-Length; a dropped connection must fail the download. # its Content-Length; a dropped connection must fail the download.
http.ignore_eof = false http.ignore_eof = false
@@ -556,6 +563,11 @@ module Maptool
return download_extract(redirect_to, dest, limit: limit - 1) if redirect_to return download_extract(redirect_to, dest, limit: limit - 1) if redirect_to
last_modified 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 end
# Sidecar recording which Geofabrik sources (country code, slug, and # 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? raise BuildError, "too many redirects reaching #{url}" if limit.zero?
uri = URI(url) 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 case response
when Net::HTTPSuccess then response["last-modified"] 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) when Net::HTTPRedirection then fetch_geofabrik_last_modified(URI.join(url, response["location"]).to_s, limit: limit - 1)
+17
View File
@@ -168,6 +168,23 @@ class PmtilesBuilderTest < Minitest::Test
] ]
end 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 def test_offline_and_terrain_status_start_idle
assert_equal({ state: "idle" }, Maptool::PmtilesBuilder.offline_status) assert_equal({ state: "idle" }, Maptool::PmtilesBuilder.offline_status)
assert_equal({ state: "idle" }, Maptool::PmtilesBuilder.terrain_status) assert_equal({ state: "idle" }, Maptool::PmtilesBuilder.terrain_status)