120 lines
4.7 KiB
Bash
Executable File
120 lines
4.7 KiB
Bash
Executable File
#!/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
|
|
}
|
|
|
|
# trace limits: maptool reads the per-country km of a whole route (up to
|
|
# 5000 km, like route's own auto limit) from /trace_attributes; the
|
|
# defaults (200 km, 16000 points) are meant for GPS traces.
|
|
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 ))}" \
|
|
--service-limits-trace-max-distance 5000000 --service-limits-trace-max-shape 500000 \
|
|
> "$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")
|
|
pbf=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("pbf", ""))' "$S/request.json")
|
|
if [ -z "$pbf" ] && [ "$(echo $regions | wc -w)" -gt 1 ]; then # a request from before the merge
|
|
status error "" "this request has several countries but no merged extract; click Apply again"
|
|
return 1
|
|
fi
|
|
if [ -n "$pbf" ]; then # several countries, merged by maptool (Valhalla breaks on overlapping extracts)
|
|
ln "/data/$pbf" "$R/pbf/" || { status error "" "missing $pbf"; return 1; }
|
|
else
|
|
for code in $regions; do
|
|
ln "/data/geofabrik_cache/$code.osm.pbf" "$R/pbf/" || { status error "" "no downloaded extract for $code"; return 1; }
|
|
done
|
|
fi
|
|
: > "$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"
|
|
# Not if a newer request (with a freshly merged file) is already waiting.
|
|
[ -n "$pbf" ] && [ ! -f "$R/build-request.json" ] && rm -f "/data/$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
|
|
# A failed build isn't retried by the next restart.
|
|
[ -f "$R/build-request.json" ] && { build || rm -f "$S/request.json"; }
|
|
# 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
|