Process Scheduled Rebalances #35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Process Scheduled Rebalances | |
| on: | |
| schedule: | |
| # Run at :25 and :55 past every hour | |
| # This gives us a 5-minute buffer before :00 and :30 schedules | |
| - cron: "25,55 * * * *" | |
| # Allow manual trigger for testing | |
| workflow_dispatch: | |
| inputs: | |
| debug: | |
| description: "Enable debug logging" | |
| required: false | |
| default: "false" | |
| jobs: | |
| process-schedules: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check current time | |
| run: | | |
| echo "Current UTC time: $(date -u '+%Y-%m-%d %H:%M:%S')" | |
| echo "Running scheduled rebalance check..." | |
| - name: Call Supabase Edge Function with Retry | |
| env: | |
| SUPABASE_URL: ${{ secrets.VITE_SUPABASE_URL }} | |
| SUPABASE_SERVICE_KEY: ${{ secrets.SUPABASE_SERVICE_ROLE_KEY }} | |
| run: | | |
| # Function to make the API call with retry logic | |
| make_api_call() { | |
| local attempt=$1 | |
| echo "Attempt $attempt: Calling edge function..." | |
| response=$(curl -s -w "\n%{http_code}" -X POST \ | |
| "${SUPABASE_URL}/functions/v1/process-scheduled-rebalances" \ | |
| -H "Authorization: Bearer ${SUPABASE_SERVICE_KEY}" \ | |
| -H "Content-Type: application/json" \ | |
| -H "User-Agent: GitHub-Actions" \ | |
| --max-time 30 \ | |
| -d '{"source": "github-actions"}') | |
| # Extract HTTP status code (last line) | |
| http_code=$(echo "$response" | tail -n1) | |
| # Extract response body (everything except last line) | |
| body=$(echo "$response" | sed '$d') | |
| echo "HTTP Status: $http_code" | |
| # Check for specific error codes | |
| if [ "$http_code" -eq 520 ]; then | |
| echo "⚠️ HTTP 520: Cloudflare error - origin server issue" | |
| return 520 | |
| elif [ "$http_code" -eq 502 ] || [ "$http_code" -eq 503 ] || [ "$http_code" -eq 504 ]; then | |
| echo "⚠️ HTTP $http_code: Gateway/Service error" | |
| return $http_code | |
| elif [ "$http_code" -eq 200 ]; then | |
| echo "✅ Success!" | |
| echo "$body" | |
| return 0 | |
| else | |
| echo "❌ Unexpected status: $http_code" | |
| echo "Response: $body" | |
| return $http_code | |
| fi | |
| } | |
| # Retry logic with exponential backoff | |
| max_attempts=3 | |
| attempt=1 | |
| wait_time=5 | |
| while [ $attempt -le $max_attempts ]; do | |
| if make_api_call $attempt; then | |
| body_result="$body" | |
| break | |
| else | |
| exit_code=$? | |
| if [ $attempt -lt $max_attempts ]; then | |
| echo "Retrying in ${wait_time} seconds..." | |
| sleep $wait_time | |
| wait_time=$((wait_time * 2)) # Exponential backoff | |
| attempt=$((attempt + 1)) | |
| else | |
| echo "❌ All attempts failed. Last error code: $exit_code" | |
| # For 520 errors, provide specific guidance | |
| if [ $exit_code -eq 520 ]; then | |
| echo "" | |
| echo "📝 HTTP 520 indicates the edge function is not accessible." | |
| echo " This could mean:" | |
| echo " 1. The function needs to be redeployed" | |
| echo " 2. The function is still initializing" | |
| echo " 3. There's a configuration issue" | |
| echo "" | |
| echo " Manual intervention may be required." | |
| fi | |
| exit 1 | |
| fi | |
| fi | |
| done | |
| # Parse and display results if successful | |
| echo "" | |
| echo "Response body:" | |
| echo "$body_result" | jq '.' 2>/dev/null || echo "$body_result" | |
| # Parse and display results | |
| if command -v jq >/dev/null 2>&1; then | |
| processed=$(echo "$body" | jq -r '.processed // 0') | |
| failed=$(echo "$body" | jq -r '.failed // 0') | |
| total=$(echo "$body" | jq -r '.total // 0') | |
| echo "" | |
| echo "📊 Summary:" | |
| echo " Total schedules found: $total" | |
| echo " Successfully processed: $processed" | |
| echo " Failed: $failed" | |
| if [ "$processed" -gt 0 ]; then | |
| echo "✅ Successfully triggered $processed rebalance(s)" | |
| fi | |
| if [ "$failed" -gt 0 ]; then | |
| echo "⚠️ Warning: $failed schedule(s) failed to process" | |
| fi | |
| fi | |
| - name: Log completion | |
| if: always() | |
| run: | | |
| echo "Scheduled rebalance check completed at $(date -u '+%Y-%m-%d %H:%M:%S') UTC" |