简体   繁体   English

Laravel 5.8 中的异步队列

[英]Async queue in Laravel 5.8

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.我需要加载的数据通常超过 50mb,所以为了不破坏用户体验,我尝试在 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...当队列同步时一切正常,但是当我 QUEUE_DRIVER=sync 到 QUEUE_DRIVER=database 时,一切似乎都有效,但从未生成目录...

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:到 Laravel 数据库队列驱动,必须先迁移队列表:

php artisan queue:table

php artisan migrate

And you must then run the queue worker:然后您必须运行队列工作器:

php artisan queue:work

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM