I'm building an app to generate catalogs. Data I need to load are often more than 50mb so to do not disrupt user experience I tried to use a queue in Laravel.
I have a job class:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use PDF;
use App\Jobs\ProcessCatalog;
class ProcessCatalog implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $id;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($id)
{
$this->id=$id;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//code which generate catalogs
}
}
I tried to run this with:
public function generateC() {
ProcessCatalog::dispatch(1);
return 'it works';
}
and everything works fine when the queue is sync but when I QUEUE_DRIVER=sync to QUEUE_DRIVER=database everything seems that work but catalog never generated...
I need to run queue async? What is the best way to do that?
To Laravel database queue driver, you must first migrate the queue table:
php artisan queue:table
php artisan migrate
And you must then run the queue worker:
php artisan queue:work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.