Offline routing phase 1: valhalla container building from the offline map's extracts, status in the Offline data panel
This commit is contained in:
1 parent
6f5f11cb4d
commit
bf912b96e5
13 files changed
+455
-36
No files matched your search
Executable
+103
@@ -0,0 +1,103 @@
|
||||
#!/bin/bash
|
||||
# Runs inside the valhalla container (docker-compose.yaml), which mounts the
|
||||
# whole data/ at /data. maptool copies this script to data/routing/ at boot
|
||||
# (Maptool::Routing.install_service_script), so the container always runs
|
||||
# the version that shipped with the app. See docs/routing-design.adoc.
|
||||
#
|
||||
# Serves data/routing/tiles.tar with valhalla_service, and polls for
|
||||
# data/routing/build-request.json ({"regions": ["SK", ...]}, written by
|
||||
# maptool). A build hard-links those countries' Geofabrik extracts, builds
|
||||
# into staging/ while the old tiles keep serving, then swaps tiles.tar in
|
||||
# and restarts the service. Progress goes to status.json, the build's
|
||||
# output to build.log.
|
||||
set -u
|
||||
|
||||
R=/data/routing
|
||||
S=$R/staging
|
||||
SERVICE_PID=
|
||||
|
||||
status() { # state step [message]
|
||||
local now
|
||||
now=$(date -u +%FT%TZ)
|
||||
python3 - "$R/status.json" "$1" "$2" "${3:-}" "$now" "${STARTED:-$now}" <<'PY'
|
||||
import json, os, sys
|
||||
path, state, step, message, now, started = sys.argv[1:]
|
||||
data = {"state": state, "started_at": started}
|
||||
if step: data["step"] = step
|
||||
if state != "running": data["finished_at"] = now
|
||||
if message: data["message"] = message
|
||||
with open(path + ".tmp", "w") as f: json.dump(data, f)
|
||||
os.replace(path + ".tmp", path)
|
||||
PY
|
||||
}
|
||||
|
||||
config() { # dir
|
||||
valhalla_build_config --mjolnir-tile-dir "$1/tiles" --mjolnir-tile-extract "$1/tiles.tar" \
|
||||
--mjolnir-admin "$1/admins.sqlite" --mjolnir-concurrency "${BUILD_THREADS:-$(( $(nproc) / 2 ))}" \
|
||||
> "$1/valhalla.json"
|
||||
}
|
||||
|
||||
stop_service() {
|
||||
[ -n "$SERVICE_PID" ] || return 0
|
||||
kill "$SERVICE_PID" 2>/dev/null
|
||||
# It can take a long time to exit on SIGTERM; it holds nothing to flush.
|
||||
for _ in 1 2 3 4 5 6 7 8 9 10; do kill -0 "$SERVICE_PID" 2>/dev/null || break; sleep 0.5; done
|
||||
kill -9 "$SERVICE_PID" 2>/dev/null
|
||||
wait "$SERVICE_PID" 2>/dev/null
|
||||
SERVICE_PID=
|
||||
}
|
||||
|
||||
serve() {
|
||||
stop_service
|
||||
[ -f "$R/tiles.tar" ] || return 0
|
||||
valhalla_service "$R/valhalla.json" "${SERVICE_THREADS:-4}" &
|
||||
SERVICE_PID=$!
|
||||
}
|
||||
|
||||
build() {
|
||||
STARTED=$(date -u +%FT%TZ)
|
||||
local regions code
|
||||
rm -rf "$S" "$R/pbf"
|
||||
mkdir -p "$S" "$R/pbf"
|
||||
mv "$R/build-request.json" "$S/request.json"
|
||||
regions=$(python3 -c 'import json,sys; print(" ".join(json.load(open(sys.argv[1]))["regions"]))' "$S/request.json")
|
||||
for code in $regions; do
|
||||
ln "/data/geofabrik_cache/$code.osm.pbf" "$R/pbf/" || { status error "" "no downloaded extract for $code"; return 1; }
|
||||
done
|
||||
: > "$R/build.log"
|
||||
run() { # step command...
|
||||
status running "$1"
|
||||
shift
|
||||
nice -n 10 "$@" >> "$R/build.log" 2>&1 || { status error "" "$(tail -c 2000 "$R/build.log")"; return 1; }
|
||||
}
|
||||
config "$S" &&
|
||||
run "admin areas" valhalla_build_admins -c "$S/valhalla.json" "$R"/pbf/*.osm.pbf &&
|
||||
run "routing graph ($regions)" valhalla_build_tiles -c "$S/valhalla.json" "$R"/pbf/*.osm.pbf &&
|
||||
run "packing tiles" valhalla_build_extract -c "$S/valhalla.json" -v || return 1
|
||||
|
||||
mv "$S/tiles.tar" "$R/tiles.tar"
|
||||
mv "$S/admins.sqlite" "$R/admins.sqlite"
|
||||
config "$R"
|
||||
mv "$S/request.json" "$R/built.json"
|
||||
rm -rf "$S" "$R/pbf"
|
||||
serve
|
||||
status done ""
|
||||
}
|
||||
|
||||
# bash as PID 1 ignores SIGTERM unless trapped: stop promptly on a redeploy.
|
||||
trap 'stop_service; exit 0' TERM INT
|
||||
|
||||
mkdir -p "$R"
|
||||
[ -f "$R/tiles.tar" ] && config "$R"
|
||||
# A build interrupted by a restart (every redeploy restarts the whole
|
||||
# compose project) starts over: its request is still in staging/.
|
||||
if [ -f "$S/request.json" ] && [ ! -f "$R/build-request.json" ]; then
|
||||
mv "$S/request.json" "$R/build-request.json"
|
||||
fi
|
||||
serve
|
||||
while true; do
|
||||
[ -f "$R/build-request.json" ] && build
|
||||
# valhalla_service died (or never started): start it again.
|
||||
if [ -n "$SERVICE_PID" ] && ! kill -0 "$SERVICE_PID" 2>/dev/null; then serve; fi
|
||||
sleep 10 & wait $! # interruptible, so the trap above runs right away
|
||||
done
|
||||
Reference in new issue
Block a user