Laravel 5.7 作业队列未异步运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54834813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:17:34  来源:igfitidea点击:

Laravel 5.7 jobs queue not running async

phplaravelqueuejobs

提问by Wally

I'm trying to use Laravel 5.7 jobs queueto 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:

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

.env

.env

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

queue.php

队列.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

工作名称.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 !

观察:我正在阅读文档,但我无法找到问题所在!

回答by Mihir Bhende

You are using QUEUE_CONNECTION=syncwhich basically has synchronous behavior.

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

Please follow below steps :

请按照以下步骤操作:

  • Run php artisan queue:tablewhich will create a migration for jobstable autimatically

  • Run php artisan migratewhich will create the table by running migration

  • Change QUEUE_CONNECTION=databaseand as per default, it will automatically take jobstable to manage the queues.

  • Run php artisan config:clearto clear application configuration cache

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

  • 运行php artisan migrate这将通过运行迁移来创建表

  • 更改QUEUE_CONNECTION=database,默认情况下,它将自动使用jobstable 来管理队列。

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

That should be good to go. Check documentationfor more help.

那应该不错。查看文档以获得更多帮助。

回答by Giorgi Lagidze

Try this : QUEUE_CONNECTION=databaseand 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. But database is a good start.

您还可以设置 rabbitmq 或其他驱动程序,因为它们的实现要先进得多,并且会更加适合生产。但是数据库是一个好的开始。