Skip to content

Commit 9d48501

Browse files
author
Илья
committed
Add lambda on exception in retryTransaction
1 parent 89ab62b commit 9d48501

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/Table.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,21 +481,36 @@ public function retrySession(Closure $userFunc, bool $idempotent = false, RetryP
481481

482482
}
483483

484-
public function retryTransaction(Closure $userFunc, bool $idempotent = false, RetryParams $params = null){
484+
public function retryTransaction(Closure $userFunc, array|bool $idempotentOrParams = false, RetryParams $params = null){
485485

486-
return $this->retrySession(function (Session $session) use ($userFunc) {
486+
if (is_array($idempotentOrParams)){
487+
if (isset($idempotentOrParams['idempotent'])){
488+
$idempotentFlag = $idempotentOrParams['idempotent'];
489+
}
490+
if (isset($idempotentOrParams['retry_params'])){
491+
$params = $idempotentOrParams['retry_params'];
492+
}
493+
if (isset($idempotentOrParams['callback_on_error'])){
494+
$callbackOnError = $idempotentOrParams['callback_on_error'];
495+
}
496+
} else {
497+
$idempotentFlag = $idempotentOrParams;
498+
$callbackOnError = function (\Exception $exception){};
499+
}
500+
return $this->retrySession(function (Session $session) use ($callbackOnError, $userFunc) {
487501
try{
488502
$session->beginTransaction();
489503
$result = $userFunc($session);
490504
$session->commitTransaction();
491505
return $result;
492-
} catch (Exception $exception){
506+
} catch (\Exception $exception){
507+
$callbackOnError($exception);
493508
try {
494509
$session->rollbackTransaction();
495510
} catch (Exception $e){}
496511
throw $exception;
497512
}
498-
}, $idempotent, $params);
513+
}, $idempotentFlag, $params);
499514

500515
}
501516

tests/RetryOnExceptionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\TestCase;
66
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
7+
use YdbPlatform\Ydb\Exceptions\RetryableException;
78
use YdbPlatform\Ydb\Retry\RetryParams;
89
use YdbPlatform\Ydb\Session;
910
use YdbPlatform\Ydb\Table;
@@ -70,5 +71,15 @@ private function retryTest(Table $table)
7071
$tres
7172
);
7273
}, true, new RetryParams(2000));
74+
$i = 0;
75+
$table->retryTransaction(function (Session $session) use (&$i) {
76+
if($i == 0){
77+
throw new RetryableException('Test exception');
78+
}
79+
self::assertEquals(5, $i);
80+
}, [
81+
'idempotent' => true,
82+
'callback_on_error' => function (\Exception $exception) use (&$i) {$i=5;}
83+
]);
7384
}
7485
}

0 commit comments

Comments
 (0)