Stoppable async Apex: Batch finish method vs. Queueable cancellation checks
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?