fuzzyworld

fuzzyworld-tools / files / server / jobs / north-star-cleanup.sh

north-star-cleanup.sh

2.4 KB · history · blame

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=...
13set -euo pipefail
14
15NOW=false
16PREVIEW_ONLY=false
17for 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
23done
24
25if ! $NOW && [ "$(date -u +%u%H)" != "206" ]; then exit 0; fi # %u: 2 = Tuesday
26
27mkdir -p "$HOME/fw/state/locks"
28exec 9>"$HOME/fw/state/locks/job-north-star-cleanup.lock"
29flock -n 9 || { echo "already running"; exit 0; }
30
31echo "== $(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.
34export PATH="$HOME/fw/node/bin:$HOME/bin:/usr/bin:/bin"
35export NODE_OPTIONS=--jitless
36set -a; . "$HOME/.config/north-star.env"; set +a
37export FIREBASE_PROJECT=north-star-e5dee FIREBASE_RTDB_INSTANCE=north-star-e5dee-default-rtdb
38
39JOB="$HOME/fw/jobs/north-star"
40[ -d "$JOB" ] || git clone --quiet --branch main "$HOME/git/north-star.git" "$JOB"
41cd "$JOB"
42git fetch --quiet origin main
43git reset --quiet --hard origin/main
44echo "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.
48lock_hash=$(sha256sum package-lock.json | cut -d' ' -f1)
49if [ ! -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
52fi
53
54bash scripts/clean-old-firebase.sh --days=30 --verbose
55$PREVIEW_ONLY || bash scripts/clean-old-firebase.sh --days=30 --apply --verbose
56echo "== done $(date -u '+%F %T UTC')"