Stoppable async Apex: Batch finish method vs. Queueable cancellation checks
0 reputation · 05 May 2021, 21:53 UTC
0 reputation · 05 May 2021, 21:53 UTC
A Salesforce team needs to run a long data-synchronization job that may need to stop before completion when an external system signals a timeout or cancellation. Batch Apex naturally partitions work into scopes and provides a finish method for finalization, while Queueable Apex supports chaining to avoid a single transaction timeout. The unresolved decision is that once an execute or executeBatch transaction has started on the server, neither pattern offers a documented way to cancel that specific in-flight transaction; cancellation is only possible before the transaction dequeues or between chained links. Because the Apex Developer Guide’s current governor limits and abort behavior differ by release, the team cannot assume that a queued job can always be stopped cleanly.
Does the Apex Developer Guide currently document any supported mechanism to interrupt a running execute() or executeBatch() transaction, or is cancellation only effective before the transaction begins? When using Queueable chaining for cancellable work, is checking a custom cancellation flag in each chained job the only documented pattern to prevent remaining links from executing? For Batch Apex, should the finish method be treated as the sole guaranteed cleanup hook when a job is aborted, given that concurrent batch limits may prevent rescheduling?
34775 reputation · 06 May 2021, 07:32 UTC
As of the current Apex Developer Guide, there is no documented way to interrupt an execute() or executeBatch() transaction once it has started on the server. Cancellation is only effective before the transaction dequeues or, for chained Queueable jobs, between links.
The only supported pattern to stop further work in a Queueable chain is to check a custom cancellation flag at the start of each execute method and, when the flag is set, return without enqueuing the next job.
execute, read the flag.null).This approach prevents remaining links from executing; there is no system‑level abort for an in‑flight Queueable transaction.
Batch jobs expose a finish method that runs after all scopes complete normally. If you call System.abortJob(jobId) while a batch is running:
Aborted.finish runs depends on the timing of the abort relative to the batch’s internal state. In many releases finish is skipped when abortion occurs after any scope has started.Therefore, treat finish as a guaranteed cleanup hook only for batches that complete without being aborted. For abort scenarios, place any essential cleanup logic inside the scope’s execute method (e.g., rollback‑style checks) or verify the job’s status after calling System.abortJob and act accordingly.
finish and logs the start of each scope.AsyncApexJob.Id and call System.abortJob(jobId).finish method’s log appears and review the final Status of the AsyncApexJob (should be Aborted or Completed depending on timing).If your org is near the concurrent batch limit, knowing the current limit and usage would determine whether you can safely reschedule a new batch after an abort. Please confirm your org’s API version and the value returned by Limits.getLimitBatchJobs() versus Limits.getBatchJobs() to refine the recommendation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.