retry method in laravel

The retry function attempts to execute the given callback until the given maximum attempt threshold is met. If the callback does not throw an exception, its return value will be returned. If the callback throws an exception, it will automatically be re…


This content originally appeared on DEV Community and was authored by Marcos Gad

The retry function attempts to execute the given callback until the given maximum attempt threshold is met. If the callback does not throw an exception, its return value will be returned. If the callback throws an exception, it will automatically be retried. If the maximum attempt count is exceeded, the exception will be thrown

function retry($times, callable $callback, $sleep = 0, $when = null);
return retry(3, function ($attempts) {
            // Attempt 3 times while resting 1000ms in between attempts...
            echo "Try: {$attempts} " . now()->toTimeString() . PHP_EOL;

            Throw new \Exception("Failed");
}, 1000);
/* 
Try: 1 10:57:48 Try: 2 10:57:49 Try: 3 10:57:50
Exception Failed
*/

with callback function true

return retry(3, function ($attempts) {
             // Attempt 3 times while resting 1000ms in between attempts...
             echo "Try: {$attempts} " . now()->toTimeString() . PHP_EOL;

             Throw new \Exception("Failed");
}, 1000, function(){
       return true;
});
/* 
Try: 1 10:57:48 Try: 2 10:57:49 Try: 3 10:57:50
Exception Failed
*/

with callback function false

return retry(3, function ($attempts) {
             // Attempt 3 times while resting 1000ms in between attempts...
             echo "Try: {$attempts} " . now()->toTimeString() . PHP_EOL;

             Throw new \Exception("Failed");
}, 1000, function(){
      return false;
});
/*
Try: 1 11:08:15
Exception Failed
*/

It can be used in real life with Http client

$response = retry(3, function () {
    return Http::get('http://example.com/users/1');
}, 200)

Retry in the Http client

$response = Http::retries(3, 200)->get('http://example.com/users/1');

I hope you enjoy the code.


This content originally appeared on DEV Community and was authored by Marcos Gad


Print Share Comment Cite Upload Translate Updates
APA

Marcos Gad | Sciencx (2021-11-20T09:23:43+00:00) retry method in laravel. Retrieved from https://www.scien.cx/2021/11/20/retry-method-in-laravel/

MLA
" » retry method in laravel." Marcos Gad | Sciencx - Saturday November 20, 2021, https://www.scien.cx/2021/11/20/retry-method-in-laravel/
HARVARD
Marcos Gad | Sciencx Saturday November 20, 2021 » retry method in laravel., viewed ,<https://www.scien.cx/2021/11/20/retry-method-in-laravel/>
VANCOUVER
Marcos Gad | Sciencx - » retry method in laravel. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/20/retry-method-in-laravel/
CHICAGO
" » retry method in laravel." Marcos Gad | Sciencx - Accessed . https://www.scien.cx/2021/11/20/retry-method-in-laravel/
IEEE
" » retry method in laravel." Marcos Gad | Sciencx [Online]. Available: https://www.scien.cx/2021/11/20/retry-method-in-laravel/. [Accessed: ]
rf:citation
» retry method in laravel | Marcos Gad | Sciencx | https://www.scien.cx/2021/11/20/retry-method-in-laravel/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.