#!/bin/sh # Submit a request file once the 24 h slot frees, then poll its status until validated or rejected. # The token is read from the arena token file and only ever passed to curl on stdin. # Usage: sh scripts/submit_when_open.sh set -u BASE=https://openenvarena-arena.hf.space/api/openenv TOKEN_FILE="$HOME/.config/hf-arena/token" START=$1 REQ=$2 SUB=$(python3 -I -c "import json,sys; print(json.load(open(sys.argv[1]))['submission_id'])" "$REQ") || exit 1 OUT=reports/submission-$SUB mkdir -p "$OUT" api() { # api ; prints the HTTP status out=$1; shift TOKEN=$(cat "$TOKEN_FILE") printf 'Authorization: Bearer %s\n' "$TOKEN" | curl -sS -o "$out" -w '%{http_code}' -H @- "$@" unset TOKEN } until [ "$(date +%s)" -ge "$START" ]; do sleep 5; done for attempt in $(seq 1 40); do code=$(api "$OUT/submit-response.json" -H 'Content-Type: application/json' --data @"$REQ" "$BASE/submissions") echo "$(date '+%H:%M:%S') submit attempt $attempt: HTTP $code" case "$code" in 202|200) break ;; 429) wait_s=$(python3 -I -c "import json,sys; print(int(json.load(open(sys.argv[1])).get('retry_after_s') or 30) + 3)" "$OUT/submit-response.json" 2>/dev/null || echo 30) [ "$wait_s" -gt 900 ] && { echo "slot frees in $wait_s s; giving up"; cat "$OUT/submit-response.json"; exit 1; } sleep "$wait_s" ;; 502|503|504|000) sleep 30 ;; *) echo "refused:"; cat "$OUT/submit-response.json"; echo; exit 1 ;; esac done cat "$OUT/submit-response.json"; echo [ "$code" = 202 ] || [ "$code" = 200 ] || exit 1 for i in $(seq 1 120); do sleep 60 code=$(api "$OUT/status.json" "$BASE/submissions/$SUB") state=$(python3 -I -c "import json,sys; print(json.load(open(sys.argv[1])).get('state'))" "$OUT/status.json" 2>/dev/null) echo "$(date '+%H:%M:%S') status HTTP $code state=$state" case "$state" in validated|rejected) break ;; esac done cat "$OUT/status.json"; echo