Reputation: 101
I am doing multiple calls to different api's on my cron job, like:
foreach ($transactions as $transaction) {
$job = (new SendApplicationToBank($transaction));
$this->dispatch($job);
}
One transaction has many banks so I am sending a transaction to all banks related:
Job:
public function handle(){
try {
$result = app($bankClass)::sendLoanApplication($this->transaction);
} catch (Exception $e) {
//Silent fail
}
}
The problem is that its failing on the first bank and just keeps retrying.
How should go and cofigure so If a job fails just release back to queue and continue the next one ?
Results:
Upvotes: 7
Views: 39588
Reputation: 89
When you catch the exception the job does not silently fail but it is actually considered successful. You should either explicitly fail the job in the catch clause with $this->fail()
or throw an exception. Throwing an exception will retry the job though, depending on your configuration.
Upvotes: 1
Reputation: 5876
You should not catch the Exception
to let the job fail properly. Now you are catching it and doing nothing (//Silent fail
)
You should make a table in your database to catch the failed jobs automatically with:
php artisan queue:failed-table
In the script running your queue you should add the number of tries before failing:
php artisan queue:listen --tries=3
It's also smart to add some kind of timeout:
php artisan queue:listen --tries=3 --timeout=60
And you can also call a webhook on fail by adding a failed method to you job:
public function failed()
{
// Called when the job is failing...
}
Upvotes: 9
Reputation: 15971
Running a command with limited tries you can run the following command
php artisan queue:work --retry=3
it will try to run your job only three-time
and programmatically you can use
public $tries = 3;
in your job class
Hope this helps
Upvotes: 3