Laravel 中的异步队列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34181644/
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 12:48:06  来源:igfitidea点击:

Async Queues In Laravel

phplaravelasynchronouslaravel-5queue

提问by user2099451

I'm trying to implement Queuing, but the result is not async And I have Applied the following

我正在尝试实现排队,但结果不是异步的,我已经应用了以下内容

config/queue.php
'default' => env('QUEUE_DRIVER', 'database'),
    'connections' => [

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

    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'expire' => 60,
    ],
   ] 

and then applied the following commands php artisan queue:table php artisan migrate

然后应用以下命令 php artisan queue:table php artisan migrate

and then run

然后运行

php artisan queue:listen

and here is the functionality

这是功能

SomethingController.php
   $model1 = new \App\Model1;
public function store(){
     Log::debug('before dispatch');
     $this->dispatch(new SendGiftCommand($model1));
     Log::debug('before dispatch');
     return true;
}



SendGiftCommand.php
{
    Log::debug('handle');
    SendGift::SendGiftAgedBased($this->model1);
    sleep(4);
    Log::debug('SendGiftCommand end');
}
SendGift.php
public static function SendGiftAgedBased(Model1 $model1){
  Log::debug('inside SendGiftAgedBased');
} 

even the process has worked but its not async, and it waits for the command to finish to return the response in the controller

即使进程已经工作但它不是异步的,它等待命令完成以在控制器中返回响应

And I git the Logs in this order

我按照这个顺序git的日志

 [2015-12-09 16:28:42] local.DEBUG: before dispatch  
 [2015-12-09 16:28:42] local.DEBUG: handle  
 [2015-12-09 16:28:42] local.DEBUG: inside SendGiftAgedBased   
 [2015-12-09 16:28:46] local.DEBUG: SendGiftCommand end  
 [2015-12-09 16:28:46] local.DEBUG: after dispatch 

should it be working on Localhost ?

它应该在 Localhost 上工作吗?

回答by Jocelyn

I had the same problem with jobs not being asynchronous and this worked for me :

我遇到了与非异步作业相同的问题,这对我有用:

  1. Edit .envand change the QUEUE_DRIVERsetting from syncto database(editing config/queue.phpwasn't enough)
  2. Restart your processes
  1. 编辑.env并将QUEUE_DRIVER设置从同步更改为数据库(编辑config/queue.php还不够)
  2. 重新启动您的流程

回答by jedrzej.kurylo

In order for the jobto be queued, the job class needs to implement Illuminate\Contracts\Queue\ShouldQueueinterface - make sure it's true for your class.

为了让作业排队,作业类需要实现Illuminate\Contracts\Queue\ShouldQueue接口 - 确保它适用于您的类。

You can find more info on queuing jobs here: http://laravel.com/docs/5.1/queues#writing-job-classes

您可以在此处找到有关排队作业的更多信息:http: //laravel.com/docs/5.1/queues#writing-job-classes

回答by Anshu Shekhar

Use this command to create the job:

使用此命令创建作业:

   php artisan make:job SendGiftCommand --queued    

Refer this link for defining the job:

请参阅此链接以定义作业:

 https://laravel.com/docs/5.1/queues#writing-job-classes

Then pass declare the job and dispatch the job in this manner:

然后通过声明作业并以这种方式调度作业:

    $processGift = new sendGiftCommand($model1);
    $this->dispatch($processGift);

Also refer Supervisor Configuration on the above mentioned link further for continuously listening to the queue or automatically restarting the command queue:listen if they fail.

另请参阅上述链接上的主管配置,以进一步持续侦听队列或自动重新启动命令队列:如果失败,则侦听。