0

I'm working with an API that uses OAuth-based authentication, accepts dynamic JSON payloads, and processes requests asynchronously. After submitting a request, I poll for job status updates. However, I'm encountering an issue where the job status never updates from "pending" even though the server logs show the job completes successfully.

Expected behavior: After submitting the job, I should be able to poll the job status endpoint and receive an updated status such as "completed" or "failed".

Observed behavior: The job status remains "pending" indefinitely, despite the backend processing the job correctly.

Minimal reproducible code:

import requests
# Step 1: Authenticate
token = requests.post("https://api.example.com/oauth/token", data={
    "client_id": "my_client_id",
    "client_secret": "my_client_secret",
    "grant_type": "client_credentials"
}).json()["access_token"]

headers = {"Authorization": f"Bearer {token}"}

# Step 2: Submit job
job_response = requests.post("https://api.example.com/jobs", headers=headers, json={
    "task": "process_data",
    "parameters": {"input": "some_input_data"}
}).json()

job_id = job_response["job_id"]

# Step 3: Poll job status
status_response = requests.get(f"https://api.example.com/jobs/{job_id}/status", headers=headers).json()
print("Job status:", status_response["status"])

What could be causing the job status to remain "pending"? Are there common pitfalls with asynchronous APIs or OAuth that I should check?

3
  • Seems like you are polling (and printing) your status_response only once directly after the job was submitted. So it's quite likely that it's not finished yet. But then you poll never again. How do you expect the status_response to change, if you don't do another request? Maybe you could add a loop that requests the status once in a while until it changes? Commented Oct 6 at 14:07
  • You're correct, I'm polling the job status only once immediately after submitting the job. Since the job is processed asynchronously, the status is still "pending" at that moment. Commented Oct 7 at 6:08
  • another consideration is to check for a retry-after header on the 202 response, assuming they are using 202. Some api designers are quite friendly and provide sufficient information to make follow up calls, particularly for async calls. check for this header and if it exists, retry the status polling after the indicated time. Commented Oct 7 at 22:20

1 Answer 1

0

To handle this properly, implementing a loop that periodically polls the job status until it changes to "completed" or "failed". updated version of the polling logic:

import time

# Poll job status until it's no longer "pending"
while True:
    status_response = requests.get(f"https://api.example.com/jobs/{job_id}/status", headers=headers).json()
    status = status_response["status"]
    print("Job status:", status)

    if status in ["completed", "failed"]:
        break

    time.sleep(5)  # Wait for 5 seconds before polling again

This way, the script keeps checking the job status at regular intervals and exits the loop once the job is done.

Sign up to request clarification or add additional context in comments.

1 Comment

A small remark: I don't know which values for status are possible in your case. But making the while True loop break only on very specific values bears the risk of an endless loop, when someone introduces a new final state like for instance "cancelled" ... Ie when a task enters this new state it won't change its state anymore, but you won't exit the loop either. So I'd introduce either some sort of emergency exit (ie break the loop after a specific number of iterations, regardless of the status) or negate the breaking option (ie something like if status != "pending": break)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.