orderbooks/scripts/run_polymarket_collector_loop.sh
philipp 284e465588
Some checks failed
deploy / deploy (push) Has been cancelled
Prepare Kubernetes orderbooks deployment
2026-04-18 11:23:28 +02:00

90 lines
2.3 KiB
Bash
Executable file

#!/usr/bin/env bash
set -uo pipefail
APP_DIR="${ORDERBOOKS_APP_DIR:-/app}"
MANIFEST_DIR="${ORDERBOOKS_MANIFEST_DIR:-${ORDERBOOKS_DATA_DIR:-/var/lib/orderbooks}/manifests}"
LOOP_SLEEP_SECONDS="${ORDERBOOKS_LOOP_SLEEP_SECONDS:-15}"
STOP_REQUESTED=0
CHILD_PID=""
utc_compact() {
date -u +%Y%m%dT%H%M%SZ
}
utc_iso() {
date -u +%Y-%m-%dT%H:%M:%SZ
}
write_loop_event() {
local status="$1"
local exit_code="$2"
local message="$3"
local path="${MANIFEST_DIR%/}/collector_loop_$(utc_compact).json"
mkdir -p "${MANIFEST_DIR}"
PYTHONDONTWRITEBYTECODE=1 python3 - "$path" "$status" "$exit_code" "$message" <<'PY_LOOP_EVENT'
import json
import sys
import datetime as dt
from pathlib import Path
path = Path(sys.argv[1])
status = sys.argv[2]
exit_code = int(sys.argv[3])
message = sys.argv[4]
now = dt.datetime.now(dt.UTC).replace(microsecond=0).isoformat().replace('+00:00', 'Z')
path.write_text(json.dumps({
'schema_name': 'collector_loop_event',
'schema_version': 1,
'written_at_utc': now,
'status': status,
'exit_code': exit_code,
'message': message,
}, indent=2, sort_keys=True) + '\n', encoding='utf-8')
PY_LOOP_EVENT
}
request_stop() {
STOP_REQUESTED=1
if [[ -n "${CHILD_PID}" ]] && kill -0 "${CHILD_PID}" >/dev/null 2>&1; then
kill -TERM "${CHILD_PID}" >/dev/null 2>&1 || true
fi
}
trap request_stop INT TERM
mkdir -p "${MANIFEST_DIR}"
cd "${APP_DIR}" || exit 1
echo "collector loop started at $(utc_iso)"
while [[ "${STOP_REQUESTED}" -eq 0 ]]; do
cycle_started="$(utc_iso)"
echo "collector cycle starting at ${cycle_started}"
/bin/bash scripts/run_polymarket_collector_cycle.sh &
CHILD_PID="$!"
wait "${CHILD_PID}"
cycle_exit="$?"
CHILD_PID=""
if [[ "${STOP_REQUESTED}" -ne 0 ]]; then
write_loop_event "INTERRUPTED" "${cycle_exit}" "collector loop received stop request during or after cycle"
break
fi
if [[ "${cycle_exit}" -ne 0 ]]; then
write_loop_event "CYCLE_FAILED" "${cycle_exit}" "collector cycle exited nonzero; loop will continue after sleep"
echo "collector cycle failed with exit ${cycle_exit}; continuing after ${LOOP_SLEEP_SECONDS}s" >&2
else
echo "collector cycle completed at $(utc_iso)"
fi
for ((i = 0; i < LOOP_SLEEP_SECONDS; i++)); do
if [[ "${STOP_REQUESTED}" -ne 0 ]]; then
break
fi
sleep 1
done
done
echo "collector loop stopped at $(utc_iso)"