Skip to content

Commit 744d6a2

Browse files
committed
feat(tracer): implement proper SQL connection retry logic
Signed-off-by: Alexandre Rulleau <[email protected]>
1 parent 0333ed0 commit 744d6a2

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

.gitlab/generate-package.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,20 @@
998998
- sed -i s/mirror.centos.org/vault.centos.org/g /etc/yum.repos.d/*.repo
999999
- sed -i s/^#.*baseurl=http/baseurl=http/g /etc/yum.repos.d/*.repo
10001000
- sed -i s/^mirrorlist=http/#mirrorlist=http/g /etc/yum.repos.d/*.repo
1001-
- yum update -y --nogpgcheck
1001+
- |
1002+
# Retry yum update as vault.centos.org can be slow/unreliable
1003+
for i in 1 2 3; do
1004+
if yum update -y; then
1005+
echo "yum update succeeded on attempt $i"
1006+
break
1007+
fi
1008+
echo "yum update failed (attempt $i/3), retrying in 5 seconds..."
1009+
sleep 5
1010+
if [ $i -eq 3 ]; then
1011+
echo "yum update failed after 3 attempts, exiting"
1012+
exit 1
1013+
fi
1014+
done
10021015

10031016
"verify debian":
10041017
extends: .verify_job

tests/Integrations/SQLSRV/SQLSRVTest.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -502,15 +502,39 @@ public function testNoFakeServices()
502502

503503
private function createConnection()
504504
{
505-
$conn = sqlsrv_connect(
506-
self::$host . ', ' . self::$port,
507-
[
508-
'PWD' => self::$password,
509-
'Database' => self::$db,
510-
'UID' => self::$user,
511-
'TrustServerCertificate' => true
512-
]
513-
);
505+
// Retry connection to handle SQL Server container startup time
506+
$maxAttempts = 30;
507+
$attempt = 0;
508+
$conn = false;
509+
$lastError = null;
510+
511+
while ($attempt < $maxAttempts && $conn === false) {
512+
$attempt++;
513+
$conn = sqlsrv_connect(
514+
self::$host . ', ' . self::$port,
515+
[
516+
'PWD' => self::$password,
517+
'Database' => self::$db,
518+
'UID' => self::$user,
519+
'TrustServerCertificate' => true,
520+
]
521+
);
522+
523+
if ($conn === false) {
524+
$errors = sqlsrv_errors();
525+
$lastError = $errors ? json_encode($errors) : 'Unknown error';
526+
527+
if ($attempt < $maxAttempts) {
528+
usleep(500000);
529+
}
530+
}
531+
}
532+
533+
if ($conn === false) {
534+
throw new \RuntimeException(
535+
"Failed to connect to SQL Server after {$maxAttempts} attempts. Last error: {$lastError}"
536+
);
537+
}
514538

515539
return $conn;
516540
}

0 commit comments

Comments
 (0)