我使用laravel 5.1 Queue作为异步的后台函数。但我只想运行该函数1次。如果这个功能失败了,我想做其他的过程。我如何检测我的工作是否失败?我做的是对还是错?我应该改变什么?ps:我是初学者。我在我的控制器里用过这个。
$job = new myjob($var1, $var2);
$this->dispatch($job);
$job->release();
if ($job->attempts() >= 1)
{
$job->delete();
//will do other process
}
您应该将--tries
参数与1
值一起使用,例如:
php artisan queue:work --tries=1
这将只运行每个任务一次,在失败的情况下不会重复。
如果您在访问控制台时遇到问题,您可以在控制器中定义尝试:
<?php
namespace App'Jobs;
class ProcessPodcast implements ShouldQueue
{
/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 5;
}
参考:https://laravel.com/docs/5.4/queues#max-作业尝试和超时
根据您想要实现的目标,我建议您使用Queued Event Listener。
https://laravel.com/docs/5.4/events#queued-事件侦听器
我希望能有所帮助:)