Skip to content

[12.x] Enhancement: the beforeCommit method will now work even if using the ShouldQueueAfterCommit contract #56445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

ahmad-cit22
Copy link
Contributor

@ahmad-cit22 ahmad-cit22 commented Jul 27, 2025

This PR addresses the issue (#56397). The issue is that when a job is using the ShouldQueueAfterCommit contract it always is dispatched after the commit even when the beforeCommit method is used when dispatching..

DB::transaction(function(){
    $someData = TestModel::create(...);

    JobWithShouldQueueAfterCommitContract::dispatch($someData)->beforeCommit(); // it still is dispatched after the db commit
});

But I guess the `beforeCommit` method should work even when the `ShouldQueueAfterCommit` contract is used, as the purpose of the `beforeCommit` method is to deliberately or especially specify that the **job must be dispatched before commit**.
So, that's why in the `shouldDispatchAfterCommit` method of `Queue.php`, I updated the logic a bit:
if ($job instanceof ShouldQueueAfterCommit) {
    return ! (isset($job->afterCommit) && $job->afterCommit === false); // now it checks the afterCommit flag value
}

Hope, this will enhance the job dispatch behavior and make it better for DX. And yes, I added unit tests in BeforeCommitContractTest.php that checks the relevant cases mostly.


Let me know if any more tests or explanations needed or if I need to modify anything. Thanks.

@taylorotwell taylorotwell merged commit b4cba0a into laravel:12.x Jul 27, 2025
62 checks passed
@ahmad-cit22 ahmad-cit22 deleted the enhancement/update-dispatcher-respect-beforeCommit branch July 28, 2025 03:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants