Sub topic 14
Monitoring async jobs
This chapter assumes you can write the AccountSyncJob Queueable from the earlier chapters and read a basic SOQL query. Once the callout runs asynchronously, the trigger no longer shows you whether it worked. You need to check the job yourself.
Where to see jobs
In Setup, search for Apex Jobs. The page lists future methods, Queueable jobs, Batch Apex and scheduled jobs. Each row shows the status, the class, the submit time and the number of errors. Use it for a quick look.
The same data is available in code through the AsyncApexJob object. You can query it in anonymous Apex, in a test, or in a monitoring class.
List<AsyncApexJob> jobs = [
SELECT Id, JobType, Status, NumberOfErrors, ExtendedStatus, CreatedDate, CompletedDate
FROM AsyncApexJob
WHERE ApexClass.Name = 'AccountSyncJob'
ORDER BY CreatedDate DESC
LIMIT 10
];
for (AsyncApexJob j : jobs) {
System.debug(j.Id + ' ' + j.Status + ' errors=' + j.NumberOfErrors + ' ' + j.ExtendedStatus);
}System.enqueueJob returns the job Id. If you store it, for example in a log record, you can query that one job later.
Id jobId = System.enqueueJob(new AccountSyncJob(accountIds));Job statuses
A Queueable job moves through a small set of states. ExtendedStatus holds the error text when a job fails.
Batch Apex adds two more states. Holding means the job is waiting in the Apex Flex Queue. Preparing means its start method is running.
| Status | Meaning |
|---|---|
| Queued | Waiting for a free worker |
| Processing | Running now |
| Completed | Finished without an unhandled exception |
| Failed | Ended with an unhandled exception |
| Aborted | Stopped by a user or by `System.abortJob` |
Completed does not mean it worked
In AccountSyncJob, a 500 response from the billing service only calls System.debug. The job still finishes as Completed with NumberOfErrors = 0. AsyncApexJob only reports unhandled exceptions. It does not report HTTP failures that your code handled.
To see failed callouts, record them yourself:
- Write a log record, or publish a platform event, when the status code is not 2xx.
- Throw an exception for a failure you want the platform to count. The Finalizer then sees
UNHANDLED_EXCEPTION. - Read the debug log for the user or class. Set a trace flag in Setup, then filter the log for
Sync failed.
Stopping a job
If a job loops or floods the queue, abort it by Id:
System.abortJob('707000000000001AAA');Replace the sample Id with a real one from AsyncApexJob. This works for Queued jobs. Aborting a job that is already running is not guaranteed to stop it at once.
Common mistakes
- Querying
AsyncApexJobfor a future method by class name only. Future jobs also haveMethodName, so filter on it when several methods share a class. - Expecting
Status = 'Failed'for callout failures that you caught. - Forgetting that the Apex Jobs page keeps only recent history, so long-term reporting needs your own log records.
- Polling
AsyncApexJobinside a trigger. Each query counts against the SOQL limit, and the job you want may not have started.
Exercise
A Queueable job that calls an external API shows Status = Completed and NumberOfErrors = 0, but the external system never received the accounts. The code catches no exceptions and only calls System.debug when the status code is 401. Why does the job look healthy, and what are two ways to make the failure visible?
Show answer
The job reports Completed because no unhandled exception occurred. A 401 response is a normal HttpResponse, not an exception, so AsyncApexJob records no error. To make it visible, either insert a log record (or publish a platform event) when the status code is not 2xx, or throw an exception for that case so the job ends as Failed and the Finalizer reports UNHANDLED_EXCEPTION.