2015-12-09 65 views
4

我试图实现排队,但结果不是异步 我已将下列异步队列在Laravel

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

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

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

,然后应用下面的命令 PHP工匠队列:表 PHP工匠迁移

,然后运行

php artisan queue:listen 

,这里是功能

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'); 
} 

即使过程已经工作,但它不是异步,并等待命令完成返回控制器

的反应和我的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 

它应该在Localhost上工作吗?

回答

2

为了使工作排队,作业类需要实现照亮\ \合同队列\ ShouldQueue界面 - 确保它是真的为你的类。

你可以找到更多的信息就在这里排队的作业:http://laravel.com/docs/5.1/queues#writing-job-classes

+0

它已经实现ShouldQueue,但它应该在本地主机上执行异步? – user2099451

+1

你怎么知道它是同步运行的?你有队列:侦听运行,所以它可能仍然是异步处理的,但是你按照正确的顺序获取日志,因为它可能会被处理为什么你的代码正在执行sleep()。停止队列:监听进程,在没有运行的情况下调度作业并查看作业是否被执行。 –

+0

感谢您的帮助,我的意思是我似乎同步,但我希望它是异步,如何实现? – user2099451

4

我有没有是异步的,工作同样的问题,这个工作对我来说:

  1. 编辑.ENV,改变QUEUE_DRIVER设置从同步数据库(编辑config/queue.php是不够的)
  2. 重新启动过程
+1

这帮助我非常 - 不知道为什么这不是在文档 –

+0

此外,请记住运行php artisan config:清除后 –

+1

这个数据库驱动程序是否真的异步?我试着运行它,但看起来不像是在运行异步操作。每项工作都是连续的而不是并行的。 –

0

使用此命令创建工作:

php artisan make:job SendGiftCommand --queued  

请参阅此链接定义的任务:

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

然后通过申报工作,并派遣以这种方式工作:

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

另请参考上述链接上的Supervisor Configuration,以继续监听队列或自动重新启动命令队列:如果失败,请监听。