├── LICENSE ├── composer.json └── src └── RetryingMiddleware.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016-2018 Alt Three Services Limited 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "alt-three/retry", 3 | "description": "A Command Retry Middleware For Laravel 5", 4 | "keywords": ["retry", "retrying", "Retry", "Alt Three"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Alt Three", 9 | "email": "support@alt-three.com" 10 | } 11 | ], 12 | "require": { 13 | "php": "^7.1.3", 14 | "illuminate/contracts": "5.5.*|5.6.*|5.7.*", 15 | "illuminate/support": "5.5.*|5.6.*|5.7.*" 16 | }, 17 | "require-dev": { 18 | "graham-campbell/analyzer": "^2.1", 19 | "phpunit/phpunit": "^6.5|^7.0" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "AltThree\\Retry\\": "src/" 24 | } 25 | }, 26 | "autoload-dev": { 27 | "psr-4": { 28 | "AltThree\\Tests\\Retry\\": "tests/" 29 | } 30 | }, 31 | "config": { 32 | "preferred-install": "dist" 33 | }, 34 | "extra": { 35 | "branch-alias": { 36 | "dev-master": "3.1-dev" 37 | } 38 | }, 39 | "minimum-stability": "dev", 40 | "prefer-stable": true 41 | } 42 | -------------------------------------------------------------------------------- /src/RetryingMiddleware.php: -------------------------------------------------------------------------------- 1 | 23 | */ 24 | class RetryingMiddleware 25 | { 26 | /** 27 | * Retry the command execution. 28 | * 29 | * @param object $command 30 | * @param \Closure $next 31 | * 32 | * @return void 33 | */ 34 | public function handle($command, Closure $next) 35 | { 36 | if (property_exists($command, 'attempts')) { 37 | if ($backoff = property_exists($command, 'backoff')) { 38 | $min = $command->backoff * 500; 39 | $max = $command->backoff * 1500; 40 | } 41 | 42 | $attempts = 0; 43 | 44 | while (++$attempts) { 45 | try { 46 | return $next($command); 47 | } catch (Exception $e) { 48 | if ($attempts >= $command->attempts) { 49 | throw $e; 50 | } elseif ($backoff) { 51 | usleep(random_int($min, $max)); 52 | } 53 | } 54 | } 55 | } 56 | 57 | return $next($command); 58 | } 59 | } 60 | --------------------------------------------------------------------------------