Routing: start once the offline build's extracts are in, not after Planetiler; merged file keeps the .osm.pbf extension osmium needs
This commit is contained in:
1 parent
ac7c7f3ba9
commit
58ca871662
4 files changed
+35
-15
No files matched your search
@@ -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
|
||||
map and terrain (`/api/routing-tiles`, same shape). Apply builds all
|
||||
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.
|
||||
* `POST /api/route` (`{"locations": [[lon, lat], ...]}`) proxies to
|
||||
Valhalla `/route` and returns the route as a GeoJSON line with
|
||||
|
||||
@@ -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,14 +242,29 @@ 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
|
||||
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.
|
||||
Routing.offline_build_finished(data_dir) if defined?(Routing)
|
||||
end
|
||||
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.
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -25,16 +25,18 @@ class RoutingTest < Minitest::Test
|
||||
assert_equal "waiting for the routing container", Maptool::Routing.build_status(@data)[:step]
|
||||
end
|
||||
|
||||
def test_a_request_during_an_offline_build_waits_for_it
|
||||
Maptool::PmtilesBuilder::OFFLINE_STATUS.replace(state: "running")
|
||||
def test_a_request_while_the_offline_build_downloads_waits_for_the_extracts_only
|
||||
Maptool::PmtilesBuilder.instance_variable_set(:@fetching_extracts, true)
|
||||
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"))
|
||||
|
||||
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")
|
||||
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"))
|
||||
refute Maptool::PmtilesBuilder.fetching_extracts?
|
||||
end
|
||||
|
||||
def test_update_check_compares_regions_and_extract_age
|
||||
|
||||
Reference in new issue
Block a user