Skip to content

Calling useless database queries when using SerializesModels #645

@drSychev

Description

@drSychev
  • Laravel/Lumen version: 12.42.0
  • Package version: 14.4.0

If you use SerializesModels for a task, then calling dispatch will create a database query for the passed models. This is because when adding a task to the queue, the unserialized method is called to get the priority in here . This is especially bad if you send tasks in a loop.

Reproduce:

  1. Create job with SerializesModels
<?php

namespace App\Jobs;

use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\SerializesModels;

class UserJob implements ShouldQueue
{
    use Queueable, SerializesModels;

    /**
     * Create a new job instance.
     */
    public function __construct(private readonly User $user)
    {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        echo $this->user->email;
    }
}
  1. Send job to queue
Route::get('/test', function () {
    $users = \App\Models\User::query()->get();
    foreach ($users as $user) {
        UserJob::dispatch($user)->onConnection('rabbitmq');
    }
    return "test";
});

Result:

Image

Might it be necessary to make an exception to exclude functionality with priorities?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions