laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.6k stars 11.04k forks source link

Bulk dispatching jobs onto the DatabaseQueue does not trigger jobs #52689

Open lukasmu opened 2 months ago

lukasmu commented 2 months ago

Laravel Version

11.20.0

PHP Version

8.3.9

Database Driver & Version

No response

Description

When pushing an array of jobs onto the database queue using the bulk method the JobQueueing and JobQueued events are not triggered.

In contrast, when pushing an array of jobs onto the redis queue or the SQS queue using the bulk method, these event are triggered (as they should be imho).

I think that @RuslanMelnychenko also described symptoms of this bug in https://github.com/laravel/framework/issues/52380.

I see two potential solutions:

Steps To Reproduce

Add a listener for JobQueueing and dispatch jobs onto different queues:

Bus::batch($jobs)->onConnection('database')->dispatch();
Bus::batch($jobs)->onConnection('redis')->dispatch();

The listener will not be invoked when using the database connection, while it will be invoked for the redis connection.

github-actions[bot] commented 2 months ago

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

ismaildasci commented 2 weeks ago

Hey! 👋

You’re right—when pushing an array of jobs to the database queue with the bulk method, the JobQueueing and JobQueued events aren’t triggered. This is different from the behavior on Redis or SQS queues, which can definitely cause inconsistencies. Possible Workaround

One option is to manually trigger these events in your code before and after the bulk dispatch. Here’s a quick example:

use Illuminate\Support\Facades\Event; use Illuminate\Queue\Events\JobQueueing; use Illuminate\Queue\Events\JobQueued;

$jobs = [ / your array of jobs / ];

foreach ($jobs as $job) { Event::dispatch(new JobQueueing($job)); }

Bus::batch($jobs)->onConnection('database')->dispatch();

foreach ($jobs as $job) { Event::dispatch(new JobQueued($job)); }

This will manually raise the JobQueueing and JobQueued events, ensuring consistency across different queue drivers. Hope this helps! 😊