简体   繁体   English

Laravel 5.7作业队列未运行异步

[英]Laravel 5.7 jobs queue not running async

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. 我正在尝试使用Laravel 5.7 作业队列在数据库中进行一些插入/更新,并且可能出错了,因为调用该作业时似乎阻塞了我的应用程序,因此,它不是异步运行。 My code is in the following structure: 我的代码具有以下结构:

.env .env

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

queue.php queue.php

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

'connections' => [

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

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

job_caller.php job_caller.php

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

job_name.php 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 ! Obs .:我正在阅读文档,但是我无法找到会发生什么的错误!

You are using QUEUE_CONNECTION=sync which basically has synchronous behavior. 您正在使用QUEUE_CONNECTION=sync ,它基本上具有同步行为。

Please follow below steps : 请按照以下步骤操作:

  • Run php artisan queue:table which will create a migration for jobs table autimatically 运行php artisan queue:table ,它将自动创建jobs表的迁移

  • Run php artisan migrate which will create the table by running migration 运行php artisan migrate migration,它将通过运行迁移创建表

  • Change QUEUE_CONNECTION=database and as per default, it will automatically take jobs table to manage the queues. 更改QUEUE_CONNECTION=database ,并按照默认情况下,它会自动采取jobs表来管理队列。

  • Run php artisan config:clear to clear application configuration cache 运行php artisan config:clear清除应用程序配置缓存

That should be good to go. 那应该很好。 Check documentation for more help. 查看文档以获取更多帮助。

Try this : QUEUE_CONNECTION=database and it should be good to go. 试试这个: QUEUE_CONNECTION=database ,应该很好。

You can also set up rabbitmq or other drivers, because their implementation is much more advanced and will be more production - ready. 您还可以设置rabbitmq或其他驱动程序,因为它们的实现要先进得多,并且可以进行更多生产。 But database is a good start. 但是数据库是一个好的开始。

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

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