I have a scheduled process that need to prepare a bunch of jobs together for notification on end. Looking on Laravel documentation i fund that you can Job Batching. But apparently not in lumen, or at least is what i thought because i keep recive error during the dispach of the batch.
Following the doc i write the following:
/*scheduled_proc*/
info('start job');
Bus::batch([
new TestJob(1),
new TestJob(2),
new TestJob(3),
new TestJob(4),
new TestJob(5),
])
->then(function(Batch $batch){
info('end job');
})
->catch(function (Batch $batch, Throwable $e) {
report($e);
})
->onQueue('my_queue')
->allowFailures()
->dispatch();
______________________
TestJob.php
namespace App\Jobs;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class TestJob implements ShouldQueue
{
use Batchable, InteractsWithQueue, Queueable, SerializesModels;
private $num;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($num)
{
$this->num = $num;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if ($this->batch()->cancelled()) {
Log::error('Job cancelled');
return;
}
info('num'.$num);
}
}
but i recive an error during the dispatch method
Serialization of 'PDO' is not allowed {"exception":"[object] (Exception(code: 0): Serialization of 'PDO' is not allowed at /var/www/html/vendor/opis/closure/src/SerializableClosure.php:155)
[stacktrace]
#0 /var/www/html/vendor/opis/closure/src/SerializableClosure.php(155): serialize(Array)
#1 [internal function]: Opis\\Closure\\SerializableClosure->serialize()
#2 /var/www/html/vendor/illuminate/bus/DatabaseBatchRepository.php(302): serialize(Array)
#3 /var/www/html/vendor/illuminate/bus/DatabaseBatchRepository.php(106): Illuminate\\Bus\\DatabaseBatchRepository->serialize(Array)
#4 /var/www/html/vendor/illuminate/bus/PendingBatch.php(239): Illuminate\\Bus\\DatabaseBatchRepository->store(Object(Illuminate\\Bus\\PendingBatch))
#5 the call of Bus::dispatch()
#6 ....
The queue driver is database and i've done the tables and migrate the db.
Now on the doc of Lumen i read 'Note: Closure jobs are not supported by Lumen.', is this the case?
In Lumen, how can i group together a bunch of jobs so i can do something when are all done?
My Answer:
Unfortunately, job batching is not supported in the Lumen framework. You may need to find an alternative solution for grouping and dispatching multiple jobs together for notification. One possible solution could be to create a custom job that internally dispatches multiple jobs and then dispatch this custom job in your scheduled process. This way, you can achieve the desired grouping of jobs for notification.
Rate this post
5 of 5 based on 3947 votesComments