Routing: start once the offline build's extracts are in, not after Planetiler; merged file keeps the .osm.pbf extension osmium needs
CI / check (push) Successful in 19s
CI / deploy (push) Successful in 12s

This commit is contained in:
randy committed 2026-11-14 14:50:39 +01:00
1 parent ac7c7f3ba9
commit 58ca871662
4 files changed
+35 -15

No files matched your search

+19 -2
View File
@@ -164,6 +164,7 @@ module Maptool
# for those.
def self.reset!
OFFLINE_MUTEX.synchronize { OFFLINE_STATUS.replace(state: "idle") }
@fetching_extracts = false
TERRAIN_MUTEX.synchronize { TERRAIN_STATUS.replace(state: "idle") }
end
@@ -241,15 +242,30 @@ module Maptool
raise BuildError, "select at least one region first" if codes.empty?
begin_run!(OFFLINE_MUTEX, OFFLINE_STATUS)
@fetching_extracts = true
Thread.new do
run_job(OFFLINE_MUTEX, OFFLINE_STATUS, kind: "offline", data_dir: data_dir, regions: codes) do |report_step|
extract_offline(codes, data_dir, download_extract, fetch_last_modified, report_step)
end
# A routing build requested meanwhile waited for these extracts.
Routing.offline_build_finished(data_dir) if defined?(Routing)
extracts_ready(data_dir) # no-op unless the build failed before its extracts were in
end
end
# True while an offline build may still be replacing the extracts that
# a routing build reads; Planetiler afterwards doesn't touch them.
def self.fetching_extracts?
@fetching_extracts
end
# A routing build requested meanwhile waited for these extracts.
def self.extracts_ready(data_dir)
return unless @fetching_extracts
@fetching_extracts = false
Routing.extracts_ready(data_dir) if defined?(Routing)
end
private_class_method :extracts_ready
# fetch_manifest: same injectable-seam reasoning as download_extract
# above, for the one Mapterhorn manifest lookup this needs.
def self.build_terrain(data_dir:, fetch_manifest: method(:fetch_mapterhorn_manifest))
@@ -334,6 +350,7 @@ module Maptool
report_step.call(progress_step("fetching #{code}", codes, i))
cached_extract(code, slug, data_dir, download_extract, fetch_last_modified)
end
extracts_ready(data_dir)
osm_path =
if sources.size == 1
+8 -7
View File
@@ -33,13 +33,13 @@ module Maptool
# Routing covers the offline map's selected countries. While an offline
# build is running (it may be downloading those very extracts), the
# request waits for it — PmtilesBuilder calls offline_build_finished.
# request waits for its downloads — PmtilesBuilder calls extracts_ready.
def self.request_build(data_dir)
codes = PmtilesBuilder.selected_regions(data_dir)
raise PmtilesBuilder::BuildError, "select at least one region first" if codes.empty?
MUTEX.synchronize do
if PmtilesBuilder.offline_status[:state] == "running"
if PmtilesBuilder.fetching_extracts?
@pending = true
return
end
@@ -67,17 +67,18 @@ module Maptool
# one merged file. Its image has no osmium; this container does.
def self.merge_and_request(data_dir, codes)
merged = File.join(dir(data_dir), "merged.osm.pbf")
part = File.join(dir(data_dir), "merged.part.osm.pbf") # osmium picks the format by extension
inputs = codes.map { |code| extract_path(data_dir, code) }
stdout, stderr, status = Open3.capture3(PmtilesBuilder.osmium_bin, "merge", "-O", *inputs, "-o", "#{merged}.part")
stdout, stderr, status = Open3.capture3(PmtilesBuilder.osmium_bin, "merge", "-O", *inputs, "-o", part)
raise PmtilesBuilder::BuildError, "osmium merge failed: #{(stderr + stdout)[-2000..] || stderr + stdout}" unless status.success?
File.rename("#{merged}.part", merged)
File.rename(part, merged)
MUTEX.synchronize do
write_request(data_dir, codes, "routing/merged.osm.pbf")
@merging = nil
end
rescue StandardError => e
FileUtils.rm_f("#{merged}.part")
FileUtils.rm_f(part)
MUTEX.synchronize { @merging = { state: "error", message: e.message, finished_at: Time.now.utc.iso8601 } }
end
private_class_method :merge_and_request
@@ -88,7 +89,7 @@ module Maptool
end
private_class_method :write_request
def self.offline_build_finished(data_dir)
def self.extracts_ready(data_dir)
pending = MUTEX.synchronize { @pending.tap { @pending = false } }
request_build(data_dir) if pending
rescue PmtilesBuilder::BuildError
@@ -99,7 +100,7 @@ module Maptool
# started_at, message.
def self.build_status(data_dir)
pending, merging = MUTEX.synchronize { [@pending, @merging] }
return { state: "running", step: "waiting for the offline map build" } if pending
return { state: "running", step: "waiting for the offline map downloads" } if pending
return merging if merging
status = read_json(File.join(dir(data_dir), "status.json"))
return status if status&.dig(:state) == "running" # the container is building; a queued request runs next