简体   繁体   English

我如何只运行一次Laravel Queue?

[英]How can i run Laravel Queue only once?

I used laravel 5.1 Queue for my background function with async. 我将laravel 5.1 Queue与异步功能一起用于后台功能。 But i want to run that function for only 1 time. 但是我只想运行一次该功能。 If that function fail, i want to do other process. 如果该功能失败,我想做其他处理。 How can i detect my job is failed or not? 如何检测我的工作是否失败? Am i doing right or wrong ? 我是对还是错? What should i change? 我应该改变什么? ps: I'm beginner. ps:我是初学者。 I used like this in my controller. 我在控制器中使用过这样的程序。

$job = new myjob($var1, $var2);
$this->dispatch($job);
$job->release();
if ($job->attempts() >= 1)
{
    $job->delete();
    //will do other process
}

You should use --tries parameter with 1 value, for example: 您应将--tries参数与1值一起使用,例如:

php artisan queue:work --tries=1

This will run each task only once and won't repeat it in case of failure. 这只会将每个任务运行一次,并且在失败的情况下不会重复执行。

if you have trouble accessing the console, you can define tries in the controller: 如果您无法访问控制台,则可以在控制器中定义尝试:

<?php

namespace App\Jobs;

class ProcessPodcast implements ShouldQueue
{
    /**
     * The number of times the job may be attempted.
     *
     * @var int
     */
    public $tries = 5;
}

Reference: https://laravel.com/docs/5.4/queues#max-job-attempts-and-timeout 参考: https : //laravel.com/docs/5.4/queues#max-job-attempts-and-timeout

And based on what you want to achieved, I suggest you use the Queued Event Listener. 根据您要实现的目标,建议您使用排队事件监听器。

https://laravel.com/docs/5.4/events#queued-event-listeners https://laravel.com/docs/5.4/events#queued-event-listeners

I hope that help :) 我希望有帮助:)

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

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