cli_submit_poll_result.sh
1 #!/bin/bash 2 # CLI Example: Submit, Poll, and Get Result for Async Jobs 3 # 4 # This example demonstrates how to use the PraisonAI CLI to: 5 # 1. Submit an async job 6 # 2. Check job status 7 # 3. Get the result 8 # 9 # Requirements: 10 # - OPENAI_API_KEY environment variable set 11 # - Jobs server running on http://127.0.0.1:8005 12 # 13 # Usage: 14 # bash cli_submit_poll_result.sh 15 16 set -e 17 18 API_URL="${PRAISONAI_BASE_URL:-http://127.0.0.1:8005}" 19 20 echo "============================================================" 21 echo "PraisonAI Async Jobs CLI Example" 22 echo "============================================================" 23 24 # Check if server is running 25 echo "" 26 echo "Checking server health..." 27 if ! curl -s "$API_URL/health" > /dev/null 2>&1; then 28 echo "✗ Server not available at $API_URL" 29 echo " Start the server with: python -m uvicorn praisonai.jobs.server:create_app --port 8005 --factory" 30 exit 1 31 fi 32 echo "✓ Server is healthy" 33 34 # 1. Submit a job 35 echo "" 36 echo "1. Submitting job..." 37 RESPONSE=$(curl -s -X POST "$API_URL/api/v1/runs" \ 38 -H "Content-Type: application/json" \ 39 -d '{"prompt": "What is 2+2? Answer with just the number."}') 40 41 JOB_ID=$(echo "$RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin)['job_id'])") 42 echo " Job ID: $JOB_ID" 43 44 # 2. Poll for status 45 echo "" 46 echo "2. Polling for status..." 47 while true; do 48 STATUS_RESPONSE=$(curl -s "$API_URL/api/v1/runs/$JOB_ID") 49 STATUS=$(echo "$STATUS_RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin)['status'])") 50 PROGRESS=$(echo "$STATUS_RESPONSE" | python3 -c "import sys,json; print(json.load(sys.stdin)['progress']['percentage'])") 51 52 echo " Status: $STATUS | Progress: ${PROGRESS}%" 53 54 if [ "$STATUS" = "succeeded" ] || [ "$STATUS" = "failed" ] || [ "$STATUS" = "cancelled" ]; then 55 break 56 fi 57 58 sleep 2 59 done 60 61 # 3. Get result 62 echo "" 63 echo "3. Getting result..." 64 if [ "$STATUS" = "succeeded" ]; then 65 RESULT=$(curl -s "$API_URL/api/v1/runs/$JOB_ID/result") 66 echo " Result: $(echo "$RESULT" | python3 -c "import sys,json; print(json.load(sys.stdin)['result'])")" 67 echo " Duration: $(echo "$RESULT" | python3 -c "import sys,json; print(f\"{json.load(sys.stdin)['duration_seconds']:.2f}s\")")" 68 else 69 echo " Job did not succeed: $STATUS" 70 fi 71 72 echo "" 73 echo "============================================================" 74 echo "Example completed!" 75 echo "============================================================"