Files
lingniu-vehicle-ingest/vehicle-data-platform/scripts/replay-all-hydrogen-history.sh
T

65 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
OPEN_STAT_BIN=${OPEN_STAT_BIN:-/opt/lingniu-vehicle-platform/current/open-platform-stat}
BASE_ENV_FILE=${BASE_ENV_FILE:-/opt/lingniu-go-native/env/base.env}
PLATFORM_ENV_FILE=${PLATFORM_ENV_FILE:-/opt/lingniu-vehicle-platform/env/platform.env}
LOCK_FILE=${LOCK_FILE:-/run/lock/lingniu-hydrogen-history-replay.lock}
test -x "$OPEN_STAT_BIN" || { printf 'open-platform-stat is not executable: %s\n' "$OPEN_STAT_BIN" >&2; exit 1; }
test -r "$BASE_ENV_FILE" || { printf 'base environment file is unavailable: %s\n' "$BASE_ENV_FILE" >&2; exit 1; }
test -r "$PLATFORM_ENV_FILE" || { printf 'platform environment file is unavailable: %s\n' "$PLATFORM_ENV_FILE" >&2; exit 1; }
exec 9>"$LOCK_FILE"
flock -n 9 || { printf 'another hydrogen history replay is already running\n' >&2; exit 1; }
run_stat() {
python3 -c '
import os
import sys
binary, base_env, platform_env, *args = sys.argv[1:]
for path in (base_env, platform_env):
with open(path, encoding="utf-8") as handle:
for raw_line in handle:
line = raw_line.rstrip("\n")
if not line.strip() or line.lstrip().startswith("#") or "=" not in line:
continue
name, value = line.split("=", 1)
os.environ[name] = value
os.execv(binary, [binary, *args])
' "$OPEN_STAT_BIN" "$BASE_ENV_FILE" "$PLATFORM_ENV_FILE" "$@"
}
if test "${1:-}" = "--from"; then
test "$#" -eq 4 && test "${3:-}" = "--to" || {
printf 'usage: %s --from yyyy-mm-dd --to yyyy-mm-dd\n' "$0" >&2
exit 2
}
start_date=$2
end_date=$4
while IFS= read -r replay_date; do
printf 'replay_date=%s status=starting\n' "$replay_date"
run_stat -date "$replay_date" -lookback-days 1
done < <(python3 -c '
import datetime
import sys
start = datetime.datetime.strptime(sys.argv[1], "%Y-%m-%d").date()
end = datetime.datetime.strptime(sys.argv[2], "%Y-%m-%d").date()
if end < start:
raise SystemExit("end date must not precede start date")
current = start
while current <= end:
print(current.isoformat())
current += datetime.timedelta(days=1)
' "$start_date" "$end_date")
exit 0
fi
if test "$#" -eq 0; then
run_stat -all-dates
else
run_stat "$@"
fi