fuzzyworld-tools / files / server / jobs / north-star-cleanup.sh
north-star-cleanup.sh
a16147a4 2026-10-27 Pavel Safronov | 1 | #!/bin/bash |
| 2 | # Weekly cleanup of stale north-star rooms in Firebase RTDB. Replaces the GitLab pipeline | |
| 3 | # schedule "drop month-old or older rooms" (cron 0 6 * * 2, UTC) and runs the same two steps. | |
| 4 | # | |
| 5 | # Cron starts this every hour (see server/jobs/crontab). DreamHost's clock is Pacific time, | |
| 6 | # so the script itself checks for Tuesday 06:00 UTC; daylight saving can't move the job. | |
| 7 | # | |
| 8 | # north-star-cleanup.sh scheduled use: act only on Tuesday 06 UTC | |
| 9 | # north-star-cleanup.sh --now run now | |
| 10 | # north-star-cleanup.sh --now --preview-only list what would be removed, remove nothing | |
| 11 | # | |
| 12 | # Needs ~/.config/north-star.env (mode 600) with FIREBASE_TOKEN=... | |
| 13 | set -euo pipefail | |
| 14 | ||
| 15 | NOW=false | |
| 16 | PREVIEW_ONLY=false | |
| 17 | for arg in "$@"; do | |
| 18 | case "$arg" in | |
| 19 | --now) NOW=true ;; | |
| 20 | --preview-only) PREVIEW_ONLY=true ;; | |
| 21 | *) echo "unknown option $arg" >&2; exit 2 ;; | |
| 22 | esac | |
| 23 | done | |
| 24 | ||
| 25 | if ! $NOW && [ "$(date -u +%u%H)" != "206" ]; then exit 0; fi # %u: 2 = Tuesday | |
| 26 | ||
| 27 | mkdir -p "$HOME/fw/state/locks" | |
| 28 | exec 9>"$HOME/fw/state/locks/job-north-star-cleanup.lock" | |
| 29 | flock -n 9 || { echo "already running"; exit 0; } | |
| 30 | ||
| 31 | echo "== $(date -u '+%F %T UTC') north-star cleanup$($PREVIEW_ONLY && echo ' (preview only)')" | |
| 32 | ||
| 33 | # Node 22 needs --jitless here: DreamHost's grsec kernel refuses executable memory for V8's JIT. | |
| 34 | export PATH="$HOME/fw/node/bin:$HOME/bin:/usr/bin:/bin" | |
| 35 | export NODE_OPTIONS=--jitless | |
| 36 | set -a; . "$HOME/.config/north-star.env"; set +a | |
| 37 | export FIREBASE_PROJECT=north-star-e5dee FIREBASE_RTDB_INSTANCE=north-star-e5dee-default-rtdb | |
| 38 | ||
| 39 | JOB="$HOME/fw/jobs/north-star" | |
| 40 | [ -d "$JOB" ] || git clone --quiet --branch main "$HOME/git/north-star.git" "$JOB" | |
| 41 | cd "$JOB" | |
| 42 | git fetch --quiet origin main | |
| 43 | git reset --quiet --hard origin/main | |
| 44 | echo "north-star at $(git rev-parse --short HEAD)" | |
| 45 | ||
| 46 | # Reinstall only when the lockfile changes. --ignore-scripts: native addons (re2) can't be | |
| 47 | # built on shared hosting in jitless mode, and the database commands don't need them. | |
| 48 | lock_hash=$(sha256sum package-lock.json | cut -d' ' -f1) | |
| 49 | if [ ! -x node_modules/.bin/firebase ] || [ "$(cat node_modules/.fw-lock-hash 2>/dev/null)" != "$lock_hash" ]; then | |
| 50 | npm ci --ignore-scripts --no-audit --no-fund --loglevel=error | |
| 51 | echo "$lock_hash" > node_modules/.fw-lock-hash | |
| 52 | fi | |
| 53 | ||
| 54 | bash scripts/clean-old-firebase.sh --days=30 --verbose | |
| 55 | $PREVIEW_ONLY || bash scripts/clean-old-firebase.sh --days=30 --apply --verbose | |
| 56 | echo "== done $(date -u '+%F %T UTC')" |