Laravel 5.7作业队列未运行异步 [英] Laravel 5.7 jobs queue not running async

查看:107
本文介绍了Laravel 5.7作业队列未运行异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Laravel 5.7 作业队列在数据库中进行一些插入/更新,并且我可能出错了,因为调用该作业时似乎阻塞了我的应用程序,因此,而不是异步运行. 我的代码具有以下结构:

I'm trying to use Laravel 5.7 jobs queue to make some insertions/updates in my database and i problably made something wrong because when the job is called its seems to be blocking my application, therefore, not running asynchronously. My code is in the following structure:

.env

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

queue.php

'default' => env('QUEUE_CONNECTION', 'sync'),

'connections' => [

    'sync' => [
        'driver' => 'sync',
    ],

    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'retry_after' => 90,
    ],

job_caller.php

method_name(){ 
  InsereProspeccao::dispatch($path, $evento, $equipe)->onQueue('jobs');
  retur some_msg_to_user;
}

job_name.php

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class InsereProspeccao implements ShouldQueue{

use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

  private $path = '';
  private $evento = '';
  private $equipe = '';


 public function __construct($path, $evento, $equipe){
     $this->path = $path;
     $this->evento = $evento;
     $this->equipe = $equipe;        
 }

   public function handle(){
      //all program logic
      //access DB for insert/update
   }

}

不好意思:我正在阅读文档,但我想找出错误的地方!

Obs.: I'M READING THE DOCUMENTATION, BUT I CANT FIND WHAT'S GOING WRONG !

推荐答案

您正在使用的QUEUE_CONNECTION=sync基本上具有同步行为.

You are using QUEUE_CONNECTION=sync which basically has synchronous behavior.

请按照以下步骤操作:

  • 运行php artisan queue:table,它将自动为jobs表创建迁移

  • Run php artisan queue:table which will create a migration for jobs table autimatically

运行php artisan migrate,它将通过运行迁移来创建表

Run php artisan migrate which will create the table by running migration

更改QUEUE_CONNECTION=database,默认情况下,它将自动使用jobs表来管理队列.

Change QUEUE_CONNECTION=database and as per default, it will automatically take jobs table to manage the queues.

运行php artisan config:clear清除应用程序配置缓存

Run php artisan config:clear to clear application configuration cache

那应该很好.查看文档以获取更多帮助.

That should be good to go. Check documentation for more help.

这篇关于Laravel 5.7作业队列未运行异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆