问题
I am looking for a way to bulk queue jobs in L5 that will all have different data.
I was hoping I would be able to do something like this:
$jobs = [];
foreach($items as $item)
{
$jobs[] = new Job($item->someValue);
}
Queue::bulk('desired_queue', $jobs);
When using the database queue driver, it is very slow to queue a large number of jobs in a loop as for each job a query is executed. Alternatively, I installed Redis on my windows dev box, and queueing large numbers of jobs using redis in a loop was very fast, however, the performance of the job running actually decreased, and I am not sure if our production server environment will support redis, so for the time being I am assuming we will be using the database driver.
Is there any way to queue large numbers of jobs and avoid performing a query for each one when using the database driver?
Thanks.
Edit:
By looking at how the framework implements this, I can do the following, however, this is not very elegant and will be blind to any changes made to the default implementation.
$now = Carbon::now()->getTimestamp();
$jobs = [];
foreach($items as $item)
{
$job = new Job($item->someValue);
$payload = json_encode([
'job' => 'Illuminate\Queue\CallQueuedHandler@call',
'data' => ['command' => serialize(clone $job)],
]);
$jobs[] = [
'queue' => 'desired_queue',
'payload' => $payload,
'attempts' => 0,
'reserved' => 0,
'reserved_at' => null,
'available_at' => $now,
'created_at' => $now
];
}
DB::table('jobs')->insert($jobs);
来源:https://stackoverflow.com/questions/30248634/laravel-5-bulk-queue-jobs-w-same-data