Skip to content

Commit 0f1711b

Browse files
committed
Add retry connection mechanism for Nginx - PHP-FPM as well
Signed-off-by: Alexandre Rulleau <[email protected]>
1 parent 0b971bc commit 0f1711b

File tree

4 files changed

+116
-36
lines changed

4 files changed

+116
-36
lines changed

tests/Integrations/SQLSRV/SQLSRVTest.php

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,43 @@ public static function ddSetUpBeforeClass()
3434
{
3535
parent::ddSetUpBeforeClass();
3636
self::putenv('DD_SQLSRV_ANALYTICS_ENABLED=true');
37+
self::waitForSqlServerReady();
38+
}
39+
40+
private static function waitForSqlServerReady()
41+
{
42+
$maxAttempts = 30;
43+
$attempt = 0;
44+
$conn = false;
45+
46+
while ($attempt < $maxAttempts && $conn === false) {
47+
$attempt++;
48+
$conn = @sqlsrv_connect(
49+
self::$host . ', ' . self::$port,
50+
[
51+
'PWD' => self::$password,
52+
'Database' => self::$db,
53+
'UID' => self::$user,
54+
'TrustServerCertificate' => true,
55+
]
56+
);
57+
58+
if ($conn === false) {
59+
if ($attempt < $maxAttempts) {
60+
usleep(500000);
61+
}
62+
}
63+
}
64+
65+
if ($conn === false) {
66+
$errors = sqlsrv_errors();
67+
$lastError = $errors ? json_encode($errors) : 'Unknown error';
68+
throw new \RuntimeException(
69+
"SQL Server not ready after {$maxAttempts} attempts. Last error: {$lastError}"
70+
);
71+
}
72+
73+
sqlsrv_close($conn);
3774
}
3875

3976
public static function ddTearDownAfterClass()
@@ -502,39 +539,15 @@ public function testNoFakeServices()
502539

503540
private function createConnection()
504541
{
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-
}
542+
$conn = sqlsrv_connect(
543+
self::$host . ', ' . self::$port,
544+
[
545+
'PWD' => self::$password,
546+
'Database' => self::$db,
547+
'UID' => self::$user,
548+
'TrustServerCertificate' => true,
549+
]
550+
);
538551

539552
return $conn;
540553
}

tests/Nginx/NginxServer.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ final class NginxServer
2424
*/
2525
private $rootPath;
2626

27+
/**
28+
* @var string
29+
*/
30+
private $serverHost;
31+
32+
/**
33+
* @var int
34+
*/
35+
private $hostPort;
36+
2737
/**
2838
* @param string $indexFile
2939
* @param string $serverHost
@@ -35,6 +45,8 @@ final class NginxServer
3545
public function __construct($indexFile, $serverHost, $hostPort, $fastCGIHost, $fastCGIPort)
3646
{
3747
$this->rootPath = dirname($indexFile);
48+
$this->serverHost = $serverHost;
49+
$this->hostPort = $hostPort;
3850
$replacements = [
3951
'{{root_path}}' => $this->rootPath,
4052
'{{index_file}}' => basename($indexFile),
@@ -76,6 +88,27 @@ public function start()
7688

7789
$this->process = new Process($processCmd);
7890
$this->process->start();
91+
92+
if (!$this->waitUntilServerRunning()) {
93+
error_log("[nginx] Server never came up...");
94+
return;
95+
}
96+
error_log("[nginx] Server is up and responding...");
97+
}
98+
99+
public function waitUntilServerRunning()
100+
{
101+
//Let's wait until nginx is accepting connections
102+
for ($try = 0; $try < 40; $try++) {
103+
$socket = @fsockopen($this->serverHost, $this->hostPort);
104+
if ($socket !== false) {
105+
fclose($socket);
106+
return true;
107+
}
108+
usleep(50000);
109+
}
110+
111+
return false;
79112
}
80113

81114
public function stop()

tests/Sapi/PhpFpm/PhpFpm.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ final class PhpFpm implements Sapi
3434
*/
3535
private $logFile;
3636

37+
/**
38+
* @var string
39+
*/
40+
private $host;
41+
42+
/**
43+
* @var int
44+
*/
45+
private $port;
46+
3747
/**
3848
* @param string $rootPath
3949
* @param string $host
@@ -45,6 +55,8 @@ public function __construct($rootPath, $host, $port, array $envs = [], array $in
4555
{
4656
$this->envs = $envs;
4757
$this->inis = $inis;
58+
$this->host = $host;
59+
$this->port = $port;
4860

4961
$logPath = $rootPath . '/' . self::ERROR_LOG;
5062

@@ -88,6 +100,27 @@ public function start()
88100

89101
$this->process = new Process($processCmd);
90102
$this->process->start();
103+
104+
if (!$this->waitUntilServerRunning()) {
105+
error_log("[php-fpm] Server never came up...");
106+
return;
107+
}
108+
error_log("[php-fpm] Server is up and responding...");
109+
}
110+
111+
public function waitUntilServerRunning()
112+
{
113+
//Let's wait until PHP-FPM is accepting connections
114+
for ($try = 0; $try < 40; $try++) {
115+
$socket = @fsockopen($this->host, $this->port);
116+
if ($socket !== false) {
117+
fclose($socket);
118+
return true;
119+
}
120+
usleep(50000);
121+
}
122+
123+
return false;
91124
}
92125

93126
public function stop()

tests/WebServer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ public function start()
224224
}
225225
}
226226

227+
// Start PHP-FPM first (if FastCGI), as nginx depends on it
228+
$this->sapi->start();
229+
230+
// Then start nginx (if needed)
227231
if ($this->sapi->isFastCgi()) {
228232
$this->server = new NginxServer(
229233
$this->indexFile,
@@ -234,9 +238,6 @@ public function start()
234238
);
235239
$this->server->start();
236240
}
237-
238-
$this->sapi->start();
239-
usleep(500000);
240241
}
241242

242243
public function reload()

0 commit comments

Comments
 (0)