#!/bin/sh # Credit cycle close — run by the Cloudron scheduler addon every 10 minutes. # # Calls the portal's own /admin/credit-cycle-close endpoint (localhost), which # walks each active member's completed allowance cycles and draws the credits # those cycles spent (issue #12: a monthly reset used to restore every credit # pack, because nothing ever recorded what usage consumed). # # Idempotent: each closed cycle is keyed by the ledger reference # `cycle-close::`, so an overlapping tick or a restart never # charges a cycle twice. Cheap: one SELECT when nobody has credits. # # The ADMIN_TOKEN env var is shared with the main app (scheduler runs in the # same launch environment), so we use it to authenticate. Admin auth is a # header, never a URL token. set -e if [ -z "$ADMIN_TOKEN" ]; then echo "credit-cycle-close: ADMIN_TOKEN is not set; aborting" >&2 exit 1 fi python3 - "$ADMIN_TOKEN" <<'PY' import sys, urllib.request, urllib.error token = sys.argv[1] url = "http://127.0.0.1:8000/admin/credit-cycle-close" req = urllib.request.Request(url, method="POST", data=b"", headers={ "Content-Type": "application/json", "X-Admin-Token": token, }) try: with urllib.request.urlopen(req, timeout=300) as resp: print("credit-cycle-close:", resp.read().decode()) except urllib.error.HTTPError as e: print(f"credit-cycle-close: HTTP {e.code}: {e.read().decode()}", file=sys.stderr) sys.exit(1) except Exception as e: print(f"credit-cycle-close: request failed: {e}", file=sys.stderr) sys.exit(1) PY