简体   繁体   English

Laravel中的异步队列

[英]Async Queues In Laravel

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 the Logs

 [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上工作吗?

I had the same problem with jobs not being asynchronous and this worked for me : 我有同样的问题,工作不是异步,这对我有用:

  1. Edit .env and change the QUEUE_DRIVER setting from sync to database (editing config/queue.php wasn't enough) 编辑.env并将QUEUE_DRIVER设置从同步更改为数据库 (编辑config / queue.php是不够的)
  2. Restart your processes 重启你的进程

In order for the job to be queued, the job class needs to implement Illuminate\\Contracts\\Queue\\ShouldQueue interface - 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

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. 另外还要在上面提到的链接上引用Supervisor Configuration,以便持续监听队列或自动重启命令队列:监听它们是否失败。

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

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