← back to Commercialrealestate

scripts/refresh-broker-snapshot.sh

34 lines

#!/bin/bash
# Nightly: regenerate the broker snapshot from the LOCAL cre DB (only place it lives) and push it
# to prod (crcp.agentabrams.com has no cre DB → serves brokers from this snapshot). No prod restart
# needed: serve.js readBrokerSnap() re-reads the file on mtime change. Keeps prod brokers fresh as
# enrichment/new brokers land. Logs to /tmp/crcp-broker-snapshot.log.
set -euo pipefail
# launchd runs with a minimal PATH that does NOT include Homebrew, so bare `node`
# fails with "command not found" (seen 2026-07-13: the 04:15 run died at line 13,
# leaving prod brokers frozen). Pin PATH so every node call resolves. Same fix as
# the sibling CRCP runners (run-email-alerts.sh, refresh-listings-rotating.sh).
export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin"
cd "$(dirname "$0")/.."
LOG=/tmp/crcp-broker-snapshot.log
ts() { date '+%Y-%m-%d %H:%M:%S'; }
echo "[$(ts)] refresh start" >> "$LOG"

# 1) regenerate locally (needs the cre DB on this host)
if ! node scripts/export-brokers-snapshot.js >> "$LOG" 2>&1; then
  echo "[$(ts)] EXPORT FAILED — leaving prod snapshot untouched" >> "$LOG"; exit 1
fi

# 2) sanity-gate: file exists, is JSON, has a plausible broker count (never push a truncated/empty snapshot)
CNT=$(node -e 'try{const s=require("./data/brokers-snapshot.json");process.stdout.write(String((s.brokers||[]).length))}catch(e){process.stdout.write("0")}')
if [ "${CNT:-0}" -lt 100 ]; then
  echo "[$(ts)] SANITY FAIL — snapshot has only ${CNT} brokers (<100); NOT deploying" >> "$LOG"; exit 1
fi

# 3) push to prod (snapshot only; server auto-reloads on mtime)
if rsync -az data/brokers-snapshot.json root@45.61.58.125:/root/public-projects/commercialrealestate/data/ >> "$LOG" 2>&1; then
  echo "[$(ts)] refresh OK — ${CNT} brokers pushed to prod" >> "$LOG"
else
  echo "[$(ts)] RSYNC FAILED — prod keeps prior snapshot" >> "$LOG"; exit 1
fi