Reputation: 105
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
}
Upvotes: 3
Views: 6663
Reputation: 459
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 :)
Upvotes: 3
Reputation: 111869
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.
Upvotes: 6