Offline routing phase 1: valhalla container building from the offline map's extracts, status in the Offline data panel
CI / check (push) Successful in 55s
CI / deploy (push) Successful in 2m48s

This commit is contained in:
randy committed 2026-11-13 21:24:19 +01:00
1 parent 6f5f11cb4d
commit bf912b96e5
13 files changed
+455 -36

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
require "fileutils"
require "json"
require "net/http"
require "time"
require_relative "pmtiles_builder"
module Maptool
# maptool's side of offline routing (docs/routing-design.adoc). Valhalla
# runs in its own container (bin/routing-service.sh); the two talk
# through files in data/routing/: maptool writes build-request.json, the
# container writes status.json, built.json and tiles.tar.
module Routing
SERVICE_SCRIPT = File.expand_path("../../bin/routing-service.sh", __dir__)
MUTEX = Mutex.new
@pending = false
def self.dir(data_dir)
File.join(data_dir, "routing")
end
def self.tiles_path(data_dir)
File.join(dir(data_dir), "tiles.tar")
end
# At boot: the container runs whatever script is in data/routing/, so it
# always matches the deployed app.
def self.install_service_script(data_dir)
FileUtils.mkdir_p(dir(data_dir))
FileUtils.cp(SERVICE_SCRIPT, File.join(dir(data_dir), "service.sh"))
end
# 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.
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"
@pending = true
return
end
missing = codes.reject { |code| File.exist?(extract_path(data_dir, code)) }
raise PmtilesBuilder::BuildError, "no downloaded map data for #{missing.join(", ")}, build the offline map first" if missing.any?
write_json(File.join(dir(data_dir), "build-request.json"), regions: codes, requested_at: Time.now.utc.iso8601)
end
end
def self.offline_build_finished(data_dir)
pending = MUTEX.synchronize { @pending.tap { @pending = false } }
request_build(data_dir) if pending
rescue PmtilesBuilder::BuildError
nil # the offline build failed to download something; its own row shows that
end
# The DataFileRow build-status shape (offline_data.js): state, step,
# started_at, message.
def self.build_status(data_dir)
return { state: "running", step: "waiting for the offline map build" } if MUTEX.synchronize { @pending }
return { state: "running", step: "waiting for the routing container" } if File.exist?(File.join(dir(data_dir), "build-request.json"))
read_json(File.join(dir(data_dir), "status.json")) || { state: "idle" }
end
# The DataFileRow status shape, plus whether Valhalla answers.
def self.tiles_status(data_dir, valhalla_url)
path = tiles_path(data_dir)
return { available: false } unless File.exist?(path)
built = read_json(File.join(dir(data_dir), "built.json")) || {}
{ available: true, size: File.size(path), mtime: File.mtime(path).utc.iso8601,
meta: { sources: (built[:regions] || []).map { |code| { code: code } }, serving: serving?(valhalla_url) } }
end
# nil = never built, true = other countries selected or a newer
# extract downloaded since, false = up to date.
def self.update_available?(data_dir)
path = tiles_path(data_dir)
return nil unless File.exist?(path)
built = (read_json(File.join(dir(data_dir), "built.json")) || {})[:regions] || []
codes = PmtilesBuilder.selected_regions(data_dir)
return true unless built.sort == codes.sort
codes.any? { |code| File.exist?(extract_path(data_dir, code)) && File.mtime(extract_path(data_dir, code)) > File.mtime(path) }
end
def self.serving?(valhalla_url)
return false if valhalla_url.to_s.empty?
uri = URI("#{valhalla_url}/status")
Net::HTTP.start(uri.host, uri.port, open_timeout: 1, read_timeout: 2) { |http| http.get(uri.path).is_a?(Net::HTTPSuccess) }
rescue StandardError
false
end
def self.extract_path(data_dir, code)
File.join(data_dir, "geofabrik_cache", "#{code}.osm.pbf")
end
private_class_method :extract_path
def self.read_json(path)
JSON.parse(File.read(path), symbolize_names: true)
rescue Errno::ENOENT, JSON::ParserError
nil
end
private_class_method :read_json
def self.write_json(path, data)
File.write("#{path}.tmp", JSON.generate(data))
File.rename("#{path}.tmp", path)
end
private_class_method :write_json
def self.reset!
MUTEX.synchronize { @pending = false }
end
end
end