← back to Quadrille House Site
scripts/finalize-on-enrich.sh
30 lines
#!/bin/bash
# Auto-finalize: wait for QH enrichment to reach target, then rebuild the
# microsite snapshot (descriptions + color buckets) and restart the :9943 server.
cd "$(dirname "$0")/.." || exit 1
PSQL=/opt/homebrew/opt/postgresql@14/bin/psql
TARGET=1900
LOG=scripts/finalize.log
count() { $PSQL -d dw_unified -tA -c "select count(*) filter (where ai_tags is not null and ai_tags::text not in ('','[]','null')) from quadrille_house_catalog;" 2>/dev/null; }
echo "$(date) finalize waiting; at $(count)/1942" >> "$LOG"
while :; do
c=$(count)
[ "${c:-0}" -ge "$TARGET" ] && break
sleep 120
done
echo "$(date) enrichment reached $c — rebuilding snapshot" >> "$LOG"
export SHOPIFY_ADMIN_TOKEN=$(grep -m1 '^SHOPIFY_ADMIN_TOKEN=' "$HOME/Projects/secrets-manager/.env" | cut -d= -f2-)
export DATABASE_URL='postgresql:///dw_unified?host=/tmp'
node scripts/build-house.js >> "$LOG" 2>&1
node scripts/build-lookbook.js >> "$LOG" 2>&1 || true
# restart bare-node server on :9943
OLD=$(lsof -ti :9943 2>/dev/null)
[ -n "$OLD" ] && kill "$OLD" 2>/dev/null
sleep 2
nohup node server.js >> scripts/server.log 2>&1 &
echo "$!" > scripts/server.pid
sleep 3
code=$(curl -s -o /dev/null -w "%{http_code}" --max-time 8 http://127.0.0.1:9943/ 2>/dev/null)
desc=$($PSQL -d dw_unified -tA -c "select count(*) from quadrille_house_catalog where coalesce(ai_description,description,'')<>'';" 2>/dev/null)
echo "$(date) DONE — server HTTP $code, snapshot has $desc enriched descriptions, server pid $(cat scripts/server.pid)" >> "$LOG"