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

+1 -1
View File
@@ -130,7 +130,7 @@ serving swaps the tiles in, and a truck route with dimensions comes back.
* "Offline routing" row in the Offline data panel, next to the offline * "Offline routing" row in the Offline data panel, next to the offline
map and terrain (`/api/routing-tiles`, same shape). Apply builds all map and terrain (`/api/routing-tiles`, same shape). Apply builds all
three. A routing request made while the offline map is still three. A routing request made while the offline map is still
downloading extracts waits for it (`Routing.offline_build_finished`). downloading extracts waits for the downloads only, then runs alongside Planetiler (`Routing.extracts_ready`).
The row says whether the service answers. The row says whether the service answers.
* `POST /api/route` (`{"locations": [[lon, lat], ...]}`) proxies to * `POST /api/route` (`{"locations": [[lon, lat], ...]}`) proxies to
Valhalla `/route` and returns the route as a GeoJSON line with Valhalla `/route` and returns the route as a GeoJSON line with
+19 -2
View File
@@ -164,6 +164,7 @@ module Maptool
# for those. # for those.
def self.reset! def self.reset!
OFFLINE_MUTEX.synchronize { OFFLINE_STATUS.replace(state: "idle") } OFFLINE_MUTEX.synchronize { OFFLINE_STATUS.replace(state: "idle") }
@fetching_extracts = false
TERRAIN_MUTEX.synchronize { TERRAIN_STATUS.replace(state: "idle") } TERRAIN_MUTEX.synchronize { TERRAIN_STATUS.replace(state: "idle") }
end end
@@ -241,14 +242,29 @@ module Maptool
raise BuildError, "select at least one region first" if codes.empty? raise BuildError, "select at least one region first" if codes.empty?
begin_run!(OFFLINE_MUTEX, OFFLINE_STATUS) begin_run!(OFFLINE_MUTEX, OFFLINE_STATUS)
@fetching_extracts = true
Thread.new do Thread.new do
run_job(OFFLINE_MUTEX, OFFLINE_STATUS, kind: "offline", data_dir: data_dir, regions: codes) do |report_step| 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) extract_offline(codes, data_dir, download_extract, fetch_last_modified, report_step)
end end
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. # A routing build requested meanwhile waited for these extracts.
Routing.offline_build_finished(data_dir) if defined?(Routing) def self.extracts_ready(data_dir)
end return unless @fetching_extracts
@fetching_extracts = false
Routing.extracts_ready(data_dir) if defined?(Routing)
end end
private_class_method :extracts_ready
# fetch_manifest: same injectable-seam reasoning as download_extract # fetch_manifest: same injectable-seam reasoning as download_extract
# above, for the one Mapterhorn manifest lookup this needs. # above, for the one Mapterhorn manifest lookup this needs.
@@ -334,6 +350,7 @@ module Maptool
report_step.call(progress_step("fetching #{code}", codes, i)) report_step.call(progress_step("fetching #{code}", codes, i))
cached_extract(code, slug, data_dir, download_extract, fetch_last_modified) cached_extract(code, slug, data_dir, download_extract, fetch_last_modified)
end end
extracts_ready(data_dir)
osm_path = osm_path =
if sources.size == 1 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 # Routing covers the offline map's selected countries. While an offline
# build is running (it may be downloading those very extracts), the # 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) def self.request_build(data_dir)
codes = PmtilesBuilder.selected_regions(data_dir) codes = PmtilesBuilder.selected_regions(data_dir)
raise PmtilesBuilder::BuildError, "select at least one region first" if codes.empty? raise PmtilesBuilder::BuildError, "select at least one region first" if codes.empty?
MUTEX.synchronize do MUTEX.synchronize do
if PmtilesBuilder.offline_status[:state] == "running" if PmtilesBuilder.fetching_extracts?
@pending = true @pending = true
return return
end end
@@ -67,17 +67,18 @@ module Maptool
# one merged file. Its image has no osmium; this container does. # one merged file. Its image has no osmium; this container does.
def self.merge_and_request(data_dir, codes) def self.merge_and_request(data_dir, codes)
merged = File.join(dir(data_dir), "merged.osm.pbf") 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) } 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? 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 MUTEX.synchronize do
write_request(data_dir, codes, "routing/merged.osm.pbf") write_request(data_dir, codes, "routing/merged.osm.pbf")
@merging = nil @merging = nil
end end
rescue StandardError => e 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 } } MUTEX.synchronize { @merging = { state: "error", message: e.message, finished_at: Time.now.utc.iso8601 } }
end end
private_class_method :merge_and_request private_class_method :merge_and_request
@@ -88,7 +89,7 @@ module Maptool
end end
private_class_method :write_request 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 } } pending = MUTEX.synchronize { @pending.tap { @pending = false } }
request_build(data_dir) if pending request_build(data_dir) if pending
rescue PmtilesBuilder::BuildError rescue PmtilesBuilder::BuildError
@@ -99,7 +100,7 @@ module Maptool
# started_at, message. # started_at, message.
def self.build_status(data_dir) def self.build_status(data_dir)
pending, merging = MUTEX.synchronize { [@pending, @merging] } 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 return merging if merging
status = read_json(File.join(dir(data_dir), "status.json")) 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 return status if status&.dig(:state) == "running" # the container is building; a queued request runs next
+7 -5
View File
@@ -25,16 +25,18 @@ class RoutingTest < Minitest::Test
assert_equal "waiting for the routing container", Maptool::Routing.build_status(@data)[:step] assert_equal "waiting for the routing container", Maptool::Routing.build_status(@data)[:step]
end end
def test_a_request_during_an_offline_build_waits_for_it def test_a_request_while_the_offline_build_downloads_waits_for_the_extracts_only
Maptool::PmtilesBuilder::OFFLINE_STATUS.replace(state: "running") Maptool::PmtilesBuilder.instance_variable_set(:@fetching_extracts, true)
Maptool::Routing.request_build(@data) Maptool::Routing.request_build(@data)
assert_equal "waiting for the offline map build", Maptool::Routing.build_status(@data)[:step] assert_equal "waiting for the offline map downloads", Maptool::Routing.build_status(@data)[:step]
refute File.exist?(File.join(@routing, "build-request.json")) refute File.exist?(File.join(@routing, "build-request.json"))
Maptool::PmtilesBuilder::OFFLINE_STATUS.replace(state: "done") # Planetiler still running doesn't matter, the extracts are in.
File.write(File.join(@data, "geofabrik_cache", "SK.osm.pbf"), "pbf") File.write(File.join(@data, "geofabrik_cache", "SK.osm.pbf"), "pbf")
Maptool::Routing.offline_build_finished(@data) Maptool::PmtilesBuilder::OFFLINE_STATUS.replace(state: "running")
Maptool::PmtilesBuilder.send(:extracts_ready, @data)
assert File.exist?(File.join(@routing, "build-request.json")) assert File.exist?(File.join(@routing, "build-request.json"))
refute Maptool::PmtilesBuilder.fetching_extracts?
end end
def test_update_check_compares_regions_and_extract_age def test_update_check_compares_regions_and_extract_age