Poll task status

Asynchronous tasks (Submit a task returns data.status=9) must be polled through the status API until they reach a final state.

Request

FieldValue
MethodGET
HostThe host of url in the AI policy
Pathstatus_query.path from the AI policy, such as ai/task_query
Querytask_id=<id>, where <id> is data.result.id from the /ai/task_push response
AuthSDK-HMAC-SHA256

Polling intervals

status_query.durations in the AI policy is a comma-separated list of milliseconds. Use the intervals in order. Tasks usually reach a final state before the list runs out.

"status_query": {
  "path": "ai/task_query",
  "durations": "500,1000,2000,2000,2000"
}

Set a polling deadline that matches how long your use case can wait.

Response

{
  "data": {
    "status": 10,
    "result": { "...": "..." }
  }
}
data.statusMeaningAction
1 / other intermediate statesIn progressQuery again after the next interval
10SucceededRead data.result
2FailedRead data.msg
20AbortedRead data.msg

If a task fails, the quota deducted for that task is returned to your account.

A failed query (non-200 HTTP status, timeout, and so on) does not mean the task failed. Keep querying at the durations intervals, and report an error only after several failures in a row.

cURL example

Continuing from the ai, base, and submitted variables in Submit a task. The signature covers the full URL including task_id, and the api function signs each call again.

task_id=$(jq -er '.data.result.id | tostring | @uri' <<< "$submitted")
query_path=$(jq -er '.status_query.path' <<< "$ai")
api GET "${base%/}/${query_path#/}?task_id=$task_id" | jq '.data'

While the task is in progress, repeat the last command at the millisecond intervals in status_query.durations. When the status is 10, read the result. When it is 2 or 20, check data.msg and stop querying.

Resume a submitted task

Set task_id to the original task ID and send the GET request above to the same status API. Querying an existing task does not consume quota again.

Next steps