Sub topic 8
Monitoring job progress
This chapter assumes you can create an ingest job, upload CSV data, and set the state to UploadComplete. You also need a valid access token and the job id from the create call. Here you learn how to watch the job until Salesforce finishes it.
Why you poll
Bulk API 2.0 is asynchronous. The UploadComplete call returns right away, and Salesforce processes your data in the background. The only way to learn the outcome is to ask the job for its state. This is called polling.
Reading the job state
Send a GET request to the job's URL. The response includes the state and the record counts so far.
curl "$INSTANCE_URL/services/data/v62.0/jobs/ingest/$JOB_ID" \
-H "Authorization: Bearer $ACCESS_TOKEN"These fields matter most:
| Field | Meaning |
|---|---|
| `state` | Where the job is in its lifecycle |
| `numberRecordsProcessed` | Records Salesforce has handled so far, whether they succeeded or failed |
| `numberRecordsFailed` | Records that failed so far |
| `errorMessage` | Present when the whole job failed |
States to watch for
A job moves through these states:
Open: the job exists and accepts data.UploadComplete: you closed the upload and the job is queued.InProgress: Salesforce is processing records.JobComplete: processing finished. Individual records may still have failed.Failed: the job could not process the data.Aborted: the job was stopped by a user or by you.
JobComplete, Failed and Aborted are terminal states. A polling loop should stop only when it sees one of them.
A polling loop
This loop checks the job every 10 seconds and stops at a terminal state. It reads the same environment variables as the earlier chapters.
import os
import time
import requests
instance = os.environ["SF_INSTANCE_URL"]
token = os.environ["SF_ACCESS_TOKEN"]
job_id = os.environ["SF_JOB_ID"]
url = f"{instance}/services/data/v62.0/jobs/ingest/{job_id}"
auth = {"Authorization": f"Bearer {token}"}
while True:
resp = requests.get(url, headers=auth)
resp.raise_for_status()
info = resp.json()
print(info["state"], info.get("numberRecordsProcessed"), info.get("numberRecordsFailed"))
if info["state"] in ("JobComplete", "Failed", "Aborted"):
break
time.sleep(10)Common errors
- Treating
JobCompleteas success. The state only means processing ended. ChecknumberRecordsFailed. If it is above zero, fetch the failed results, which the next chapter covers. - Stopping at
UploadComplete. That state means the job is queued, not finished. - Polling too fast. A tight loop wastes API requests and counts against your org's limits. An interval of several seconds is enough, and you can lengthen it for large jobs.
- Ignoring
Failed. When the whole job fails, readerrorMessagebefore you resubmit anything. - Expired tokens. A long job can outlast your access token. A 401 response during polling means you need to refresh the token and keep polling the same job id.
Exercise
Your loop exits with state JobComplete, but numberRecordsFailed is 120. Did the load succeed? What do you do next?
Show answer
The job finished, but 120 records failed, so the load is only partly successful. Retrieve the failed results (they include the sf__Error column), fix the causes, and resubmit those rows in a new job.
Exercise
Which three states should end a polling loop?
Show answer
JobComplete, Failed and Aborted.