Spaces:
Sleeping
Sleeping
Download scripts/daily_refresh.sh from fti-opportunity-tracker/app: direct link, hf CLI and curl.
- Browser
- Download file 4.82 kB
-
https://huggingface.co/spaces/fti-opportunity-tracker/app/resolve/main/scripts/daily_refresh.sh
- Command line
-
hf download hf://spaces/fti-opportunity-tracker/app/scripts/daily_refresh.sh
-
curl -L -o daily_refresh.sh https://huggingface.co/spaces/fti-opportunity-tracker/app/resolve/main/scripts/daily_refresh.sh
4.82 kB
| # Daily refresh pipeline: crawl every source, fetch detail pages, run codex | |
| # enrichment, then push the refreshed SQLite snapshot to the HF Space. | |
| # | |
| # Loaded by launchd at 09:30 daily β see launchd/com.fti.tracker.refresh.plist. | |
| # Each stage is run sequentially; failures in early stages are logged but do | |
| # not abort later ones, so a transient crawl failure on one source doesn't | |
| # block the final SQLite snapshot + push. | |
| set -u # strict undefined vars; intentionally NOT -e so partial failures continue | |
| REPO="/Users/carlchaaya/Documents/code/FTI AI Factory/fti-opportunity-tracker" | |
| ENV_FILE="$HOME/.config/fti-tracker/env" | |
| LOG_DIR="$REPO/logs" | |
| LOCK="$LOG_DIR/daily-refresh.lock" | |
| mkdir -p "$LOG_DIR" | |
| # Lockfile: skip if a previous run is still in flight. | |
| if [ -f "$LOCK" ]; then | |
| pid=$(cat "$LOCK" 2>/dev/null || echo "") | |
| if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then | |
| echo "[$(date)] Previous refresh (pid $pid) still running; skipping" >> "$LOG_DIR/launchd-refresh.err.log" | |
| exit 0 | |
| fi | |
| fi | |
| echo $$ > "$LOCK" | |
| trap 'rm -f "$LOCK"' EXIT | |
| # launchd's PATH is minimal; explicitly add Homebrew + user-local + codex. | |
| export PATH="/Users/carlchaaya/.local/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/Users/carlchaaya/.nvm/versions/node/v24.11.1/bin:$PATH" | |
| # Load HF_TOKEN + DATABASE_URL secrets (kept outside the repo). | |
| if [ -f "$ENV_FILE" ]; then | |
| # shellcheck disable=SC1090 | |
| set -a; source "$ENV_FILE"; set +a | |
| fi | |
| cd "$REPO" || { echo "FATAL: cannot cd to $REPO"; exit 2; } | |
| # Dated log; capture all output (stdout+stderr) to it. | |
| LOG="$LOG_DIR/daily-refresh-$(date +%Y-%m-%d).log" | |
| exec >> "$LOG" 2>&1 | |
| echo "============================================================" | |
| echo "Daily refresh starting at $(date -u +'%Y-%m-%d %H:%M:%S UTC')" | |
| echo "uv = $(which uv)" | |
| echo "codex = $(which codex)" | |
| echo "============================================================" | |
| run_step () { | |
| local label="$1"; shift | |
| local started=$(date +%s) | |
| echo | |
| echo "[$(date '+%H:%M:%S')] STEP: $label" | |
| "$@" | |
| local rc=$? | |
| local elapsed=$(( $(date +%s) - started )) | |
| if [ "$rc" -eq 0 ]; then | |
| echo "[$(date '+%H:%M:%S')] STEP OK ($label) in ${elapsed}s" | |
| else | |
| echo "[$(date '+%H:%M:%S')] STEP FAIL ($label) rc=$rc in ${elapsed}s β continuing" | |
| fi | |
| return $rc | |
| } | |
| ensure_postgres () { | |
| # Quick check: if Postgres already answers on 55432, we're done. | |
| if PGPASSWORD=fti psql -h localhost -p 55432 -U fti -d fti_opportunities \ | |
| -c "SELECT 1" >/dev/null 2>&1; then | |
| echo "[$(date '+%H:%M:%S')] Postgres already up" | |
| return 0 | |
| fi | |
| echo "[$(date '+%H:%M:%S')] Postgres not reachable; bootstrapping infra" | |
| # Bring colima up if it isn't. | |
| if ! colima status >/dev/null 2>&1; then | |
| echo "[$(date '+%H:%M:%S')] Starting colima" | |
| colima start 2>&1 | tail -3 | |
| fi | |
| # Start the postgres container if it's stopped; create it if missing. | |
| if docker inspect fti-opportunity-postgres >/dev/null 2>&1; then | |
| docker start fti-opportunity-postgres >/dev/null 2>&1 | |
| else | |
| docker compose up postgres -d >/dev/null 2>&1 \ | |
| || (cd "$REPO" && docker compose up postgres -d >/dev/null 2>&1) \ | |
| || echo "[$(date '+%H:%M:%S')] docker compose failed to launch postgres" | |
| fi | |
| # Wait up to 60 s for Postgres to accept connections. | |
| for i in $(seq 1 30); do | |
| if PGPASSWORD=fti psql -h localhost -p 55432 -U fti -d fti_opportunities \ | |
| -c "SELECT 1" >/dev/null 2>&1; then | |
| echo "[$(date '+%H:%M:%S')] Postgres ready (after ${i} retries)" | |
| return 0 | |
| fi | |
| sleep 2 | |
| done | |
| echo "[$(date '+%H:%M:%S')] FATAL: Postgres still unreachable after 60s" | |
| return 1 | |
| } | |
| # 0. Ensure the local Postgres is up (auto-start colima + container if not). | |
| if ! ensure_postgres; then | |
| echo "[$(date '+%H:%M:%S')] Aborting refresh: Postgres unreachable" | |
| exit 3 | |
| fi | |
| # 1. Crawl every source (enrichment is disabled here; we run it explicitly | |
| # afterwards with our own batch/concurrency tuning). | |
| run_step "crawl" uv run python -m opportunity_tracker.cli crawl --no-enrich | |
| # 2. Backfill detail pages for open opportunities β picks up new rows. | |
| run_step "detail-fetch" uv run python -m opportunity_tracker.cli detail-fetch --only-open --concurrency 3 | |
| # 3. Run codex enrichment on anything pending. Hash-based selection means this | |
| # only re-runs rows whose source fields actually changed. | |
| run_step "enrich" uv run python -m opportunity_tracker.cli enrich --only-open --batch-size 20 --concurrency 6 --timeout 360 | |
| # 4. Export Postgres β SQLite β gzip β push to HF Space. This step is the | |
| # one we treat as success/fail for the overall job. | |
| run_step "export+push" uv run python "$REPO/scripts/daily_refresh.py" | |
| final_rc=$? | |
| echo | |
| echo "[$(date '+%H:%M:%S')] Daily refresh finished (final_rc=$final_rc)" | |
| exit $final_rc | |