← back to Hollywood Import

hollywood-create-resume.sh.pre-acoustic.bak

81 lines

#!/bin/zsh
# hollywood-create-resume.sh — daily drip for creating the genuinely-absent Hollywood
# products under Shopify's daily variant-creation cap. Idempotent (skips already-CREATED via
# audit) and cap-aware (the node runner exits 3 when the daily cap is hit, resuming next run).
# Install is Steve-gated. Mirrors the sample-split-resume / schu-build-resume pattern.
# launchd minimal-PATH fix (node/budget.cjs need /usr/local/bin) — mirrors run-cadence-hourly.sh.
export PATH="/usr/local/bin:/opt/homebrew/bin:/opt/homebrew/opt/postgresql@14/bin:$PATH"
cd "$HOME/Projects/hollywood-import" || exit 1
ts="$(date '+%Y-%m-%d %H:%M %Z')"
echo "=== $ts drip start ===" >> hollywood-create.log

# Shared kill-switch (2026-06-19): this job previously ignored it entirely.
[ -f "$HOME/.dw-fixer-stop" ] && { echo "=== $ts kill-switch present — skip ===" >> hollywood-create.log; exit 0; }

# Shared variant-budget gate (2026-06-19): draw from the live 'upload' pool (== global ~864/day
# Shopify ceiling minus osborne) so Hollywood's create can't collectively blow the daily variant
# cap. take returns min(req, upload-remaining, global-remaining); 0 grant = no budget → exit clean.
# Grant (variants) → product limit (2 variants/product: Roll + Sample); --limit caps phase-1 creates.
BUDGET="$HOME/Projects/designerwallcoverings/scripts/variant-budget/budget.cjs"
HW_VARS=$(node "$BUDGET" take upload 40 2>/dev/null || echo 40)
HW_PRODUCTS=$(( HW_VARS / 2 ))
if [ "${HW_PRODUCTS:-0}" -lt 1 ]; then
  echo "=== $ts no variant budget this run (grant=$HW_VARS) — exit clean ===" >> hollywood-create.log
  exit 0
fi
echo "=== $ts budget grant=$HW_VARS variants → create up to $HW_PRODUCTS products ===" >> hollywood-create.log
# Phase 1: create the absent products (cap-aborts exit 3 → resume tomorrow), capped at the grant.
HW_P1_OUT=$(/usr/bin/env node hollywood-create.mjs --apply --limit="$HW_PRODUCTS" 2>&1)
p1=$?
echo "$HW_P1_OUT" >> hollywood-create.log
echo "=== $ts phase1 (create) exit $p1 ===" >> hollywood-create.log

# ── refund the unused phase-1 upload grant (budget-integrity, 2026-06-20; DTD verdict A) ──
# We took HW_VARS variants UP FRONT to size --limit, but hollywood-create.mjs consumes 2 variants
# (Roll + Sample) per product actually CREATED. It prints 'DONE: created=N' on clean exit and
# 'CAP-ABORT: created=N this run' on the exit-3 cap, so N is reliable either way; a 429/cap slot
# creates 0 (refund whole grant), a partial slot fewer. GUARD: refund ONLY when one of those count
# lines was found (HW_P1_CREATED non-empty) — a crash with no summary keeps its debit (conservative).
HW_P1_CREATED=$(printf '%s' "$HW_P1_OUT" | grep -oE '(created=[0-9]+|CAP-ABORT: created=[0-9]+)' | grep -oE '[0-9]+' | tail -1)
if [ -n "$HW_P1_CREATED" ]; then
  HW_P1_USED=$(( HW_P1_CREATED * 2 ))
  HW_P1_REFUND=$(( HW_VARS - HW_P1_USED ))
  if [ "$HW_P1_REFUND" -gt 0 ]; then
    HW_P1_GOT=$(node "$BUDGET" refund upload "$HW_P1_REFUND" 2>/dev/null || echo 0)
    echo "=== $ts phase1 budget refund: grant=$HW_VARS used=$HW_P1_USED (created $HW_P1_CREATED×2) → refunded $HW_P1_GOT upload variants ===" >> hollywood-create.log
  fi
fi
# Phase 2 ONLY after Phase 1 fully done (exit 0) — both share the daily variant cap.
# 2026-06-19 (budget fix): phase-2 (yard add = 1 variant/product) was previously UNBUDGETED —
# no --limit, no budget take — so once the create backlog cleared it could add variants ABOVE the
# recorded grant (bounded only by Shopify's own daily-cap abort, not the ledger). FIX: take a
# remaining-aware grant from the 'upload' pool and pass it as --limit so phase-2 can never exceed
# the shared ledger. take returns min(req, upload-remaining, global-remaining); 0 grant → skip.
if [ "$p1" -eq 0 ]; then
  P2_VARS=$(node "$BUDGET" take upload 40 2>/dev/null || echo 0)
  if [ "${P2_VARS:-0}" -lt 1 ]; then
    echo "=== $ts phase2 (add-yard) skipped — no variant budget (grant=$P2_VARS) ===" >> hollywood-create.log
  else
    echo "=== $ts phase2 (add-yard) budget grant=$P2_VARS variants → --limit=$P2_VARS ===" >> hollywood-create.log
    HW_P2_OUT=$(/usr/bin/env node hollywood-add-yard.mjs --apply --limit="$P2_VARS" 2>&1)
    echo "$HW_P2_OUT" >> hollywood-create.log
    echo "=== $ts phase2 (add-yard) exit $? ===" >> hollywood-create.log

    # ── refund the unused phase-2 upload grant (budget-integrity, 2026-06-20; DTD verdict A) ──
    # We took P2_VARS variants UP FRONT, but hollywood-add-yard.mjs adds only 1 yard variant per
    # product. It prints 'DONE: added=N' on clean exit and 'CAP-ABORT: added=N this run' on exit 3,
    # so N is reliable; an empty/cap slot adds 0 (refund whole grant), a partial slot fewer. GUARD:
    # refund ONLY when one of those count lines was found (HW_P2_ADDED non-empty) — no summary →
    # keep the debit (conservative; never over-refunds → never over-grants).
    HW_P2_ADDED=$(printf '%s' "$HW_P2_OUT" | grep -oE '(added=[0-9]+|CAP-ABORT: added=[0-9]+)' | grep -oE '[0-9]+' | tail -1)
    if [ -n "$HW_P2_ADDED" ]; then
      HW_P2_REFUND=$(( P2_VARS - HW_P2_ADDED ))   # 1 yard variant per product added
      if [ "$HW_P2_REFUND" -gt 0 ]; then
        HW_P2_GOT=$(node "$BUDGET" refund upload "$HW_P2_REFUND" 2>/dev/null || echo 0)
        echo "=== $ts phase2 budget refund: grant=$P2_VARS used=$HW_P2_ADDED (added×1) → refunded $HW_P2_GOT upload variants ===" >> hollywood-create.log
      fi
    fi
  fi
fi
exit 0