name: Updraft rebalance # Fires every weekday at 15:00 UTC (mid-morning New York, always market hours). The Space guards it: it trades # only while the market is open, on one of the first 5 trading days of a month, once per month, on a ranking # built from data at most 5 days old — so in practice it trades on the first trading day of each month and # returns "executed: false" every other day. Also runnable by hand from the Actions tab ("force" overrides the # once-a-month / window guards, never the market-open or data-freshness guards). # # Free Spaces sleep after 48 h idle and a cold start can take minutes ("Internal server error" from HF's # gateway meanwhile), so the job wakes the Space first and retries the POST. The POST is idempotent. on: schedule: - cron: "0 15 * * 1-5" workflow_dispatch: inputs: force: description: "Rebalance now even if already done this month / outside the window" type: boolean default: false concurrency: updraft-rebalance jobs: rebalance: runs-on: ubuntu-24.04 steps: - name: Wake the Space env: URL: ${{ secrets.UPDRAFT_URL || 'https://s-ttp-updraft.hf.space' }} run: | for i in $(seq 1 40); do CODE=$(curl -s -o /dev/null -w '%{http_code}' --max-time 30 "$URL/api/health" || echo 000) echo "attempt $i: /api/health -> $CODE" [ "$CODE" = "200" ] && { echo "Space is awake."; exit 0; } sleep 15 done echo "::error::Space did not wake within ~10 minutes"; exit 1 - name: Trigger rebalance (retries on gateway errors) env: URL: ${{ secrets.UPDRAFT_URL || 'https://s-ttp-updraft.hf.space' }} REBALANCE_TOKEN: ${{ secrets.UPDRAFT_REBALANCE_TOKEN }} FORCE: ${{ inputs.force && '1' || '0' }} run: | for i in $(seq 1 5); do RESP=$(curl -s -w $'\n%{http_code}' --max-time 300 -X POST "$URL/api/rebalance?dry=0&force=$FORCE" \ -H "X-Rebalance-Token: $REBALANCE_TOKEN") CODE=$(printf '%s' "$RESP" | tail -1); BODY=$(printf '%s' "$RESP" | sed '$d') echo "attempt $i: HTTP $CODE"; echo "$BODY" | python3 -m json.tool 2>/dev/null || echo "$BODY" [ "$CODE" = "200" ] && exit 0 [ "$CODE" = "403" ] && { echo "::error::token rejected — check UPDRAFT_REBALANCE_TOKEN"; exit 1; } echo "transient HTTP $CODE — retrying in 30s"; sleep 30 done echo "::error::rebalance did not return 200 after retries"; exit 1