Sub topic 9
Controlling chains with AsyncOptions
This chapter assumes you can write and enqueue a Queueable class and chain one job from another. It also assumes you know the chain limits from the previous chapter. AsyncOptions is an optional second argument to System.enqueueJob. It lets the caller set guardrails on a job without changing the job's own code.
What AsyncOptions controls
AsyncOptions has three properties.
| Property | Effect |
|---|---|
| `MaximumQueueableStackDepth` | Caps how many jobs deep a chain may go. |
| `MinimumQueueableDelayInMinutes` | Delays the start of the job. The value is 0 to 10 minutes. |
| `DuplicateSignature` | Rejects the enqueue if an identical job is already waiting. |
Setting options on an enqueue
AsyncOptions options = new AsyncOptions();
options.MaximumQueueableStackDepth = 3;
options.MinimumQueueableDelayInMinutes = 2;
options.DuplicateSignature = QueueableDuplicateSignature.Builder()
.addId(UserInfo.getUserId())
.addString('AccountRatingJob')
.build();
System.enqueueJob(new AccountRatingJob(accountIds), options);This job starts no sooner than two minutes after it is enqueued. It may sit at most three levels deep in a chain. A second enqueue with the same user ID and name is refused while the first is still queued.
Stack depth
The stack depth is the position of a job in its chain. A job enqueued from a trigger, a controller, or anonymous Apex is at depth 1. Its child is at depth 2, and so on.
When you set MaximumQueueableStackDepth, the platform checks it each time a job in the chain calls System.enqueueJob. If the new job would exceed the maximum, the call throws an AsyncException. A job can look at its position before it tries, using the AsyncInfo class.
public void execute(QueueableContext context) {
// ... process this slice ...
if (AsyncInfo.hasMaxStackDepth()
&& AsyncInfo.getCurrentQueueableStackDepth() >= AsyncInfo.getMaximumQueueableStackDepth()) {
System.debug('Chain limit reached; stopping here.');
return;
}
System.enqueueJob(new AccountChunkJob(rest));
}The check makes a runaway chain stop quietly, instead of failing with an exception.
Delay
MinimumQueueableDelayInMinutes is a minimum, not a schedule. The job can start later than the delay if the queue is busy. A delay is useful when a chained job calls a service that needs a moment to recover. It also helps when you want to spread out jobs that would otherwise start together.
Duplicate signatures
A signature identifies a job by the values you add to the builder. The builder accepts IDs, strings, and integers. If you enqueue a job whose signature matches one already waiting, the platform throws a DuplicateMessageException. Catch it when a repeat enqueue is harmless, for example when a trigger fires many times for the same user.
try {
System.enqueueJob(new AccountRatingJob(accountIds), options);
} catch (DuplicateMessageException e) {
System.debug('Already queued, skipping: ' + e.getMessage());
}Common errors
- Setting a delay above 10 minutes. The enqueue fails.
- Treating
MaximumQueueableStackDepthas a soft hint. Exceeding it throws, so guard withAsyncInfoor catch the exception. - Forgetting that the signature only compares against jobs still waiting. It does not stop you from enqueuing the same work again after the first job has started.
Exercise
A trigger enqueues a job every time a Contact changes, and the same user often edits many Contacts in one request. Which AsyncOptions property prevents a pile of identical jobs, and what exception do you catch?
Show answer
Set DuplicateSignature, built from values such as the user ID and a job name. The extra enqueues throw a DuplicateMessageException, which you catch and ignore.
Exercise
A chained job should stop after five levels. Name two ways to enforce this.
Show answer
Set MaximumQueueableStackDepth to 5 on the first enqueue, so deeper enqueues throw an AsyncException. You can also compare AsyncInfo.getCurrentQueueableStackDepth() with AsyncInfo.getMaximumQueueableStackDepth() inside execute and skip the enqueue.