← back to Sister Parish Onboarding
scripts/scheduled_push.sh
83 lines
#!/bin/zsh
# Wrapper called by com.steve.sister-parish-push launchd job.
#
# Idempotent:
# - If Sister Parish products already in DW Shopify (count > 0), exit and self-unload.
# - Otherwise run push_shopify.js, then if successful and all 73 landed, self-unload.
#
# Logs go to logs/scheduled_push.log alongside the project.
set -euo pipefail
ROOT="/Users/macstudio3/Projects/sister-parish-onboarding"
LOG="$ROOT/logs/scheduled_push.log"
PLIST="$HOME/Library/LaunchAgents/com.steve.sister-parish-push.plist"
PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin"
mkdir -p "$ROOT/logs"
exec >> "$LOG" 2>&1
echo
echo "===== $(date '+%F %T %Z') · scheduled_push.sh fired ====="
# 1) Load DW Shopify creds
if [[ ! -f "$HOME/Projects/dw-collections-viewer/.env" ]]; then
echo "FATAL: cannot find dw-collections-viewer/.env for SHOPIFY_* creds"
exit 2
fi
set -a; source "$HOME/Projects/dw-collections-viewer/.env"; set +a
if [[ -z "${SHOPIFY_ADMIN_TOKEN:-}" || -z "${SHOPIFY_STORE:-}" ]]; then
echo "FATAL: SHOPIFY_ADMIN_TOKEN or SHOPIFY_STORE empty"; exit 2
fi
# 2) Pre-flight: how many SP products already in Shopify
COUNT=$(curl -sf -H "X-Shopify-Access-Token: $SHOPIFY_ADMIN_TOKEN" \
"https://${SHOPIFY_STORE}/admin/api/2026-01/products/count.json?vendor=Sister+Parish&status=any" \
| /usr/bin/python3 -c "import json,sys; print(json.load(sys.stdin)['count'])" 2>/dev/null || echo "ERR")
echo "Sister Parish count on DW Shopify: $COUNT"
if [[ "$COUNT" == "ERR" ]]; then
echo "Shopify pre-flight failed — leaving plist installed for next fire"
exit 3
fi
if (( COUNT >= 73 )); then
echo "All 73 already in Shopify — running backfill, then self-unloading"
cd "$ROOT"
/opt/homebrew/bin/node scripts/backfill_shopify_ids.js || /opt/homebrew/bin/node scripts/backfill_shopify_ids.js || true
echo "Unloading $PLIST"
/bin/launchctl unload "$PLIST" 2>/dev/null || true
/bin/rm -f "$PLIST"
echo "===== self-unloaded ====="
exit 0
fi
if (( COUNT > 0 )); then
echo "Partial state — $COUNT < 73. Will re-attempt push (push script idempotent on handle conflict)."
fi
# 3) Run push
echo "--- running push_shopify.js ---"
cd "$ROOT"
NODE_BIN="/opt/homebrew/bin/node"
[[ -x "$NODE_BIN" ]] || NODE_BIN="/opt/homebrew/bin/node"
[[ -x "$NODE_BIN" ]] || NODE_BIN="$(/usr/bin/which node)"
"$NODE_BIN" scripts/push_shopify.js || PUSH_FAILED=1
# 4) Verify final count + decide whether to self-unload
COUNT2=$(curl -sf -H "X-Shopify-Access-Token: $SHOPIFY_ADMIN_TOKEN" \
"https://${SHOPIFY_STORE}/admin/api/2026-01/products/count.json?vendor=Sister+Parish&status=any" \
| /usr/bin/python3 -c "import json,sys; print(json.load(sys.stdin)['count'])")
echo "Post-push Sister Parish count: $COUNT2"
if (( COUNT2 >= 73 )); then
echo "All 73 landed — running backfill and self-unloading"
"$NODE_BIN" scripts/backfill_shopify_ids.js || true
/bin/launchctl unload "$PLIST" 2>/dev/null || true
/bin/rm -f "$PLIST"
echo "===== self-unloaded ====="
else
echo "Only $COUNT2/73 landed (probably another daily-variant cap). Leaving plist in place for next Wednesday 02:00 PT."
fi