Skip to content

Commit 3df0e7d

Browse files
committed
make helper class
1 parent ab44440 commit 3df0e7d

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
"autoload": {
1313
"psr-4": {
1414
"MysqlDeadlocks\\RetryHelper\\": "src/"
15-
}
15+
},
16+
"files": [
17+
"src/Helper.php"
18+
]
1619
},
1720
"extra": {
1821
"laravel": {

src/RetryHelper.php renamed to src/DBTransactionRetryHelper.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
use Illuminate\Support\Facades\DB;
66
use Illuminate\Database\QueryException;
77

8-
class RetryHelper
8+
class DBTransactionRetryHelper
99
{
1010
/**
1111
* Perform a database transaction with retry logic in case of deadlocks.
1212
*
1313
* @param callable $callback The transaction logic to execute.
1414
* @param int $maxRetries Number of times to retry on deadlock.
15-
* @param int $retryDelay Delay between retries in milliseconds.
15+
* @param int $retryDelay Delay between retries in seconds.
1616
* @return mixed
1717
* @throws QueryException
1818
*/
19-
public static function transactionWithRetry(callable $callback, $maxRetries = 5, $retryDelay = 200)
19+
public static function transactionWithRetry(callable $callback, int $maxRetries = 3, int $retryDelay = 2)
2020
{
2121
$attempt = 0;
2222

@@ -27,16 +27,26 @@ public static function transactionWithRetry(callable $callback, $maxRetries = 5,
2727

2828
} catch (QueryException $e) {
2929
// Check if the error is a deadlock (MySQL error code 1213)
30-
if ($e->getCode() === '1213') {
30+
if ($e->getCode() == 1213 || $e->getCode() == 40001 || $e->errorInfo[1] == 1213) {
3131
$attempt++;
3232

3333
if ($attempt >= $maxRetries) {
34+
// Generate log when max retries are reached
35+
generateLog([
36+
'ExceptionName' => get_class($e),
37+
'QueryException' => $e->getMessage(),
38+
'url' => request()->getUri() ?? null,
39+
'method' => request()->getMethod() ?? null,
40+
'token' => request()->header()['authorization'] ?? null,
41+
'userId' => request()->user()->id ?? null,
42+
'trace' => getDebugBacktraceArray() ?? null,
43+
],'mysql-deadlocks-logs');
3444
// Throw the exception if max retries are reached
3545
throw $e;
3646
}
3747

3848
// Wait before retrying
39-
usleep($retryDelay * 1000); // Convert milliseconds to microseconds
49+
sleep($retryDelay); // Convert seconds to microseconds
4050
} else {
4151
// Re-throw exception if it's not a deadlock
4252
throw $e;

src/Helper.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
4+
use Illuminate\Support\Facades\Log;
5+
6+
if (! function_exists('getDebugBacktraceArray')) {
7+
function getDebugBacktraceArray(): array
8+
{
9+
$steps = [];
10+
foreach (debug_backtrace() as $step) {
11+
$steps[] = [
12+
'file' => $step['file'] ?? '',
13+
'class' => $step['class'] ?? '',
14+
'function' => $step['function'] ?? '',
15+
'line' => $step['line'] ?? '',
16+
];
17+
}
18+
return $steps;
19+
}
20+
}
21+
22+
if (! function_exists('generateLog')) {
23+
function generateLog($var, $logFileName): void
24+
{
25+
if (is_null($logFileName)) {
26+
$logFilePath = storage_path('logs/general_'.now()->toDateString().'.log');
27+
} else {
28+
$logFilePath = storage_path("logs/{$logFileName}_".now()->toDateString().'.log');
29+
}
30+
Log::build([
31+
'driver' => 'single',
32+
'path' => $logFilePath,
33+
])->error(var_export($var, true));
34+
}
35+
}

0 commit comments

Comments
 (0)