fix(graph): resync

This commit is contained in:
vl.arkhangelskii
2026-09-21 14:08:00 +03:00
parent 2bbd4c4ca1
commit dd4b2924ee
7 changed files with 195 additions and 16 deletions
+144
View File
@@ -0,0 +1,144 @@
#!/usr/bin/env bash
# Kick off OSM extract sync for every enabled source, wait until the worker is idle,
# then print per-source status.
#
# Usage:
# ./scripts/sync-all-sources.sh
# ./scripts/sync-all-sources.sh --rebuild-graph
# API=http://127.0.0.1:51300 ./scripts/sync-all-sources.sh --rebuild-graph
#
# --rebuild-graph rebuilds graphs/{id}.sqlite even if tiles are already Ready
# (needed after geom/polyline changes). Does not rebuild MBTiles
# unless the extract would rebuild them anyway.
set -euo pipefail
API="${API:-${TILE_SERVER_API:-https://tile-server.ru}}"
API="${API%/}"
POLL="${POLL:-20}"
REBUILD_GRAPH=0
usage() {
sed -n '2,14p' "$0" | sed 's/^# \{0,1\}//'
}
while [[ $# -gt 0 ]]; do
case "$1" in
--rebuild-graph|-g) REBUILD_GRAPH=1 ;;
--api) API="${2%/}"; shift ;;
--poll) POLL="$2"; shift ;;
-h|--help) usage; exit 0 ;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 2
;;
esac
shift
done
py() {
if command -v python3 >/dev/null 2>&1; then
python3 "$@"
elif command -v python >/dev/null 2>&1; then
python "$@"
elif command -v py >/dev/null 2>&1; then
py -3 "$@"
else
echo "Need python3 to parse JSON." >&2
exit 1
fi
}
json_field() {
py -c 'import json,sys; d=json.load(sys.stdin); print(d.get(sys.argv[1], "") if d is not None else "")' "$1"
}
is_running() {
local body
body="$(curl -fsS "$API/api/v1/sync/status")"
printf '%s' "$body" | json_field isRunning | grep -qi '^true$'
}
print_sources() {
curl -fsS "$API/api/v1/sources" | py -c '
import json, sys
rows = json.load(sys.stdin)
print("{:<32} {:<16} {:<6} {}".format("id", "status", "graph", "error"))
print("-" * 88)
for item in rows:
if not item.get("enabled", True):
continue
err = (item.get("lastError") or "").replace("\n", " ")[:60]
graph = "yes" if item.get("graphReady") else "no"
print("{:<32} {:<16} {:<6} {}".format(item.get("id", ""), item.get("status", ""), graph, err))
'
}
wait_idle() {
local reason="$1"
if ! is_running; then
return 0
fi
echo "Worker is busy ($reason). Polling every ${POLL}s…"
while is_running; do
print_sources || true
echo
sleep "$POLL"
done
}
echo "API $API"
echo "Mode $([[ "$REBUILD_GRAPH" -eq 1 ]] && echo rebuild-graph || echo normal)"
echo
echo "Sources before:"
print_sources
echo
wait_idle "previous job"
query=""
if [[ "$REBUILD_GRAPH" -eq 1 ]]; then
query="?rebuildGraph=true"
fi
echo "POST $API/api/v1/sync$query"
http="$(curl -sS -o /tmp/tile-sync-body.json -w '%{http_code}' -X POST "$API/api/v1/sync$query")"
body="$(cat /tmp/tile-sync-body.json 2>/dev/null || true)"
echo "HTTP $http $body"
if [[ "$http" == "409" ]]; then
echo "Conflict — waiting for the running job, then retrying once."
wait_idle "409"
http="$(curl -sS -o /tmp/tile-sync-body.json -w '%{http_code}' -X POST "$API/api/v1/sync$query")"
body="$(cat /tmp/tile-sync-body.json 2>/dev/null || true)"
echo "HTTP $http $body"
fi
if [[ "$http" != "202" ]]; then
echo "Failed to start sync." >&2
exit 1
fi
# Give Kestrel a moment to flip isRunning.
sleep 2
wait_idle "this job"
echo
echo "Sources after:"
print_sources
failed="$(curl -fsS "$API/api/v1/sources" | py -c '
import json, sys
rows = json.load(sys.stdin)
ids = [r["id"] for r in rows if r.get("enabled", True) and r.get("status") == "Failed"]
print(" ".join(ids))
')"
if [[ -n "${failed// }" ]]; then
echo >&2
echo "Failed extracts: $failed" >&2
exit 1
fi
echo
echo "All enabled extracts are not Failed."