[12.x] Add DeferredBatch for deferred batch construction in job chains#58905
Open
heffaklump90 wants to merge 1 commit intolaravel:12.xfrom
Open
[12.x] Add DeferredBatch for deferred batch construction in job chains#58905heffaklump90 wants to merge 1 commit intolaravel:12.xfrom
heffaklump90 wants to merge 1 commit intolaravel:12.xfrom
Conversation
Add `DeferredBatch`, a sibling to `ChainedBatch` that defers batch construction to execution time. This allows batch contents to depend on data created by earlier jobs in the chain. `ChainedBatch` requires all batch jobs to be known at chain construction time. In many real-world scenarios, a batch's contents depend on data created by earlier jobs (e.g., an import job creates records, then a calculation batch processes those records). `DeferredBatch` solves this by accepting a callable that is invoked at execution time and must return a `PendingBatch` or `null` to skip. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
|
I'm not really sure if this necessitates an entirely new class. I think a more targeted improvement would be adding a Bus::chain([
new ImportBuildingsJob($projectId),
CallQueuedClosure::create(function ($job) use ($projectId) {
$buildings = Building::where('project_id', $projectId)->get();
if ($buildings->isEmpty()) {
return;
}
$job->dispatchBatchInChain(
Bus::batch(
$buildings->map(fn ($b) => new CalculateBuildingJob($b))
)->name('calculate-buildings')
);
}),
new FinalizeProjectJob($projectId),
])->dispatch();This would also work from any regular job class, and |
This was referenced Feb 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DeferredBatchclass — a sibling toChainedBatchthat defers batch construction to execution timeDispatcher::deferredBatch()convenience methodMotivation
ChainedBatchrequires all batch jobs to be known at chain construction time. In many real-world scenarios, a batch's contents depend on data created by earlier jobs in the chain. For example, an import job creates buildings, then a calculation batch needs to process those buildings. SinceChainedBatcheagerly captures jobs in its constructor, the batch is empty when built before the chain runs.DeferredBatchsolves this by accepting a callable (closure or invokable class) that is invoked at execution time and must return aPendingBatchornullto skip.Usage
Design
ChainedBatch's provenattachRemainderOfChainToEndOfBatch()pattern for chain continuation viafinallycallbackchainCatchCallbacksto the batch (matchingChainedBatchbehavior)ChainedBatchbehavior)nullfrom the builder skips the batch — chain continues normally viadispatchNextJobInChain()SerializableClosurefor queue serialization; invokable classes work as-isTest plan
nullcontinues chain without interruptionInvalidArgumentExceptionfinallycallbackDispatcher::deferredBatch()helperShouldQueueinterface implementation🤖 Generated with Claude Code