← back to Wallco Ai

scripts/cron-build-tifs.sh

44 lines

#!/usr/bin/env bash
# Nightly TIF backfill for wallco.ai (runs on prod / Kamatera via crontab).
#
# Builds a full-size TIF for every PUBLISHED design that doesn't have one yet,
# so the /designs/hires/:id mural preview + the full-res colorway recolor stay
# populated as new designs publish. pilot-build-tifs.py --all skips existing
# TIFs, so this is cheap on a steady state (only NEW designs get built).
#
# - flock-guarded so two runs never overlap.
# - --min-free-gb 15 disk guard (aborts before a build if disk runs low).
# - logs to logs/tif-cron.log (rotated when > 5 MB; logs/ is rsync-excluded so
#   the deploy never clobbers it).
#
# Install (idempotent) on prod:
#   ( crontab -l 2>/dev/null | grep -v 'cron-build-tifs.sh'; \
#     echo '30 3 * * * /root/public-projects/wallco-ai/scripts/cron-build-tifs.sh' ) | crontab -
set -euo pipefail
cd "$(dirname "$0")/.."
mkdir -p logs
LOG="logs/tif-cron.log"

# rotate if big
if [ -f "$LOG" ] && [ "$(wc -c < "$LOG")" -gt 5000000 ]; then
  tail -c 1000000 "$LOG" > "$LOG.tmp" && mv "$LOG.tmp" "$LOG"
fi

exec 9> /tmp/wallco-tif-cron.lock
if ! flock -n 9; then
  echo "$(date -Is) another tif-cron run in progress — skipping" >> "$LOG"
  exit 0
fi

echo "=== $(date -Is) wallco-tif nightly backfill start ===" >> "$LOG"
rc=0
python3 scripts/pilot-build-tifs.py --all --min-free-gb 15 --progress-every 500 >> "$LOG" 2>&1 || rc=$?
# exit codes: 0=all built · 1=some unbuildable (missing-source 404s — EXPECTED,
# a chronic ~408 designs have no source PNG on disk) · 2=disk-guard abort (real).
if [ "$rc" = "2" ]; then
  echo "$(date -Is) ⚠️  DISK-GUARD ABORT (rc=2) — free disk too low, backfill incomplete" >> "$LOG"
elif [ "$rc" != "0" ]; then
  echo "$(date -Is) ok (rc=$rc — some designs have no source image to TIF; expected)" >> "$LOG"
fi
echo "=== $(date -Is) wallco-tif nightly backfill done ===" >> "$LOG"