Skip to content
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

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

Open
lukasmu opened this issue Sep 7, 2024 · 4 comments
Open

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

lukasmu opened this issue Sep 7, 2024 · 4 comments

Comments

@lukasmu
Copy link

lukasmu commented Sep 7, 2024

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 #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.

Copy link

github-actions bot commented Sep 9, 2024

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
Copy link

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! 😊

@AbronStudio
Copy link

I have encountered the same issue with the bulk method on the database queue connection not firing the JobQueueing and JobQueued events.

Issue Analysis

It seems that the database queue does not trigger these events when enqueuing jobs in bulk. This behavior differs from other queue connections like Redis and SQS, where these events are properly dispatched.

Proposed Solution

To ensure consistency across queue connections, I suggest modifying the bulk method in the DatabaseQueue class to explicitly trigger the JobQueueing and JobQueued events for each job.

This can be done by:

  1. Calling raiseJobQueueingEvent before inserting each job into the database.
  2. Proceeding with the normal insertion logic.
  3. Calling raiseJobQueuedEvent after the insertion.

Code Suggestion

Here’s an example modification in DatabaseQueue.php:

foreach ($jobs as $job) {
    $this->raiseJobQueueingEvent($job);
    $this->insertIntoDatabase($job);
    $this->raiseJobQueuedEvent($job);
}

@alanondra
Copy link

I'm in a project with v11.44.0. I'm getting this error when using Bus::batch()->dispatch():

Illuminate\Bus\UpdatedBatchJobCounts::__construct(): Argument #1 ($pendingJobs) must be of type int, array given, called in ./vendor/laravel/framework/src/Illuminate/Bus/DatabaseBatchRepository.php on line 171

When attempting to use any of the additional chained methods, it comes up with one of two possible errors:

  1. If you attempt to use any resources from outside the closures (i.e. use ($logger)):
    Cannot assign Laravel\SerializableClosure\Serializers\Native to property Symfony\Component\Console\Input\InputArgument::$suggestedValues of type Closure|array
    
  2. If you pass either a plain anonymous function or an array-form callable when chaining the methods:
    Serialization of 'Closure' is not allowed
    

Which leads me to this documentation below it:

Since batch callbacks are serialized and executed at a later time by the Laravel queue, you should not use the $this variable within the callbacks. In addition, since batched jobs are wrapped within database transactions, database statements that trigger implicit commits should not be executed within the jobs.

I haven't tested this yet, but the implication seems to be that it is better to pass in some kind of serializable, invokable object that can be reconstructed from the batch job storage mechanism rather than a callback. IMO that also warrants a documentation update at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants