2

I used laravel 5.1 Queue for my background function with async. 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. 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
}
4

2 回答 2

5

You should use --tries parameter with 1 value, for example:

php artisan queue:work --tries=1

This will run each task only once and won't repeat it in case of failure.

于 2015-11-29T15:10:55.337 回答
2

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

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

I hope that help :)

于 2017-02-23T08:51:04.013 回答