Laravel 5.7 作业队列未异步运行

2022-01-21 00:00:00 queue php laravel jobs

我正在尝试使用 Laravel 5.7 jobs queue 在我的数据库中进行一些插入/更新,我可能做错了什么,因为当调用该作业时,它似乎阻塞了我的应用程序,因此,不异步运行.我的代码结构如下:

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 IlluminateBusQueueable;
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;

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.:我正在阅读文档,但我找不到问题所在!

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清除应用配置缓存

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

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

相关文章