Sub topic 11
Monitoring and Aborting Jobs
This chapter assumes you can write a Batch Apex class and start it with Database.executeBatch. Once a job is running over millions of records, you need to see how far it has got and be able to stop it.
Where job information lives
Every batch job gets a row in the AsyncApexJob object. Database.executeBatch returns the Id of that row. You can query it from Apex, from the Developer Console, or from Setup under Apex Jobs.
The fields you will use most:
| Field | Meaning |
|---|---|
| `Status` | `Holding`, `Queued`, `Preparing`, `Processing`, `Aborted`, `Completed` or `Failed` |
| `JobItemsProcessed` | Number of batches (execute calls) finished so far |
| `TotalJobItems` | Number of batches the job will run |
| `NumberOfErrors` | Number of batches that failed |
| `ExtendedStatus` | Text of the first error, if any |
| `CompletedDate` | When the job ended |
Note that JobItemsProcessed counts batches, not records. With 2,000,000 records and a batch size of 200, TotalJobItems is 10,000.
Checking progress
Run this in Anonymous Apex, using the Id returned when you started the job:
Id jobId = '707000000000001AAA'; // replace with your job Id
AsyncApexJob job = [
SELECT Id, Status, JobItemsProcessed, TotalJobItems,
NumberOfErrors, ExtendedStatus
FROM AsyncApexJob
WHERE Id = :jobId
];
System.debug(job.Status + ': ' + job.JobItemsProcessed + ' of '
+ job.TotalJobItems + ' batches, errors: ' + job.NumberOfErrors);To list every batch job that is still active:
List<AsyncApexJob> active = [
SELECT Id, ApexClass.Name, Status, JobItemsProcessed, TotalJobItems
FROM AsyncApexJob
WHERE JobType = 'BatchApex'
AND Status IN ('Holding', 'Queued', 'Preparing', 'Processing')
];
for (AsyncApexJob j : active) {
System.debug(j.ApexClass.Name + ' ' + j.Status);
}Aborting a job
Call System.abortJob with the job Id:
System.abortJob('707000000000001AAA');You can also click Abort next to the job in Setup > Apex Jobs. The job moves to Aborted.
Aborting has limits you should know about:
- A batch that is already running its
executecall finishes that call. Its changes are committed. - Batches that already completed are not rolled back.
- Remaining batches do not run.
- Aborting a job does not undo records it already changed. Design your job so you can run it again safely.
Common errors
- Reading
finishas proof of success.finishruns even when some batches failed. CheckNumberOfErrorsthere. - Treating
Completedas "no errors". A job can beCompletedwith a non-zeroNumberOfErrors. - Losing the job Id. If you did not store it, query
AsyncApexJobbyApexClass.NameandCreatedDate.
Exercise
A batch job has 5,000,000 records and a batch size of 200. Its status is Processing and JobItemsProcessed is 12,500. About what percent is complete, and what happens to the 12,500 finished batches if you call System.abortJob?
Show answer
TotalJobItems is 5,000,000 / 200 = 25,000, so the job is 50% complete. The 12,500 finished batches stay committed. Only batches that have not started are skipped, and a batch already running finishes its current execute call.