Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically reconnecting to the database when safe to do so
This has been anf often requested feature for the database layer, specially by people using the database in long running background tasks. Instead of instructing them to figure out how to configure their database to not drop connections, better just give them an extra hand by reconnecting to the database whenever it is safe to do so. That is, reconnection are attempted only on fresh commands to the database outside of a transaction.
- Loading branch information
Showing
6 changed files
with
489 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* @link https://cakephp.org CakePHP(tm) Project | ||
* @since 3.6.0 | ||
* @license https://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Cake\Core\Retry; | ||
|
||
use Exception; | ||
|
||
/** | ||
* Allows any action to be retried in case of an exception. | ||
* | ||
* This class can be parametrized with an strategy, which will be followed | ||
* to determine whether or not the action should be retried. | ||
*/ | ||
class CommandRetry | ||
{ | ||
|
||
/** | ||
* The strategy to follow should the executed action fail | ||
* | ||
* @var \Cake\Core\Retry\RetryStrategyInterface | ||
*/ | ||
protected $strategy; | ||
|
||
/** | ||
* The number of retries to perform in case of failure | ||
* | ||
* @var int | ||
*/ | ||
protected $retries; | ||
|
||
/** | ||
* Creates the CommandRetry object with the given strategy and retry count | ||
* | ||
* @param \Cake\Core\Retry\RetryStrategyInterface $strategy The strategy to follow should the action fail | ||
* @param int $retries The number of times the action has been already called | ||
*/ | ||
public function __construct(RetryStrategyInterface $strategy, $retries = 1) | ||
{ | ||
$this->strategy = $strategy; | ||
$this->retries = $retries; | ||
} | ||
|
||
/** | ||
* The number of retries to perform in case of failure | ||
* | ||
* @param callable $action The callable action to execute with a retry strategy | ||
* @return mixed The return value of the passed action callable | ||
*/ | ||
public function run(callable $action) | ||
{ | ||
$retryCount = 0; | ||
$lastException = null; | ||
while ($this->retries > $retryCount) { | ||
$retryCount++; | ||
try { | ||
return $action(); | ||
} catch (Exception $e) { | ||
$lastException = $e; | ||
if (!$this->strategy->shouldRetry($e, $retryCount)) { | ||
throw $e; | ||
} | ||
} | ||
} | ||
|
||
if ($lastException !== null) { | ||
throw $lastException; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* @link https://cakephp.org CakePHP(tm) Project | ||
* @since 3.6.0 | ||
* @license https://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Cake\Core\Retry; | ||
|
||
use Exception; | ||
|
||
/** | ||
* Makes sure the connection to the database is alive before authorizing | ||
* the retry of an action. | ||
*/ | ||
interface RetryStrategyInterface | ||
{ | ||
/** | ||
* Returns true if the action can be retried, false otherwise. | ||
* | ||
* @param Exception $exception The exception that caused the action to fail | ||
* @param int $retryCount The number of times the action has been already called | ||
* @return bool Whether or not it is OK to retry the action | ||
*/ | ||
public function shouldRetry(Exception $exception, $retryCount); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.