Skip to content

Commit 138dbd1

Browse files
fix: add pre-sync database host connectivity check
1 parent 6a764b0 commit 138dbd1

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

deployer/feature/task/feature_sync.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,54 @@
55
require_once('feature_init.php');
66

77

8+
/**
9+
* Verifies that the database host is reachable from the remote server before syncing.
10+
* This is necessary because DNS for newly created databases (e.g. Mittwald) can be
11+
* intermittent, and the time between feature:setup and feature:sync may cause DNS flapping.
12+
*/
13+
function waitForDatabaseHost(): void
14+
{
15+
if (!has('database_host')
16+
|| '127.0.0.1' === get('database_host')
17+
|| 'localhost' === get('database_host')
18+
) {
19+
return;
20+
}
21+
22+
$hostname = get('database_host');
23+
$port = (int) get('database_port', 3306);
24+
$waitingTime = (int) get('mittwald_database_wait', 30);
25+
$maxRetries = (int) get('mittwald_database_retries', 20);
26+
27+
while ($maxRetries > 0) {
28+
try {
29+
info("Verifying database host {$hostname}:{$port} before sync, remaining attempts: {$maxRetries}");
30+
$check = sprintf(
31+
'echo @fsockopen("%s", %d, $errno, $errstr, 5) ? "1" : "";',
32+
$hostname,
33+
$port
34+
);
35+
$result = run("php -r " . escapeshellarg($check));
36+
if ('1' === trim($result)) {
37+
info("Database host {$hostname}:{$port} is reachable.");
38+
return;
39+
}
40+
} catch (\Throwable $e) {
41+
debug("Pre-sync connectivity check failed: " . $e->getMessage());
42+
}
43+
44+
if ($maxRetries > 1) {
45+
sleep($waitingTime);
46+
}
47+
$maxRetries--;
48+
}
49+
50+
throw new \RuntimeException(
51+
"Database host {$hostname}:{$port} is not reachable before sync after all attempts."
52+
);
53+
}
54+
55+
856
task('feature:sync', function () {
957

1058
if ((has('feature_setup') && !get('feature_setup')) || !input()->getOption('feature')) return;
@@ -13,6 +61,8 @@
1361
$synced = false;
1462
$optionalVerbose = isVerbose() ? '-v' : '';
1563

64+
waitForDatabaseHost();
65+
1666
/*
1767
* db_sync_tool
1868
* https://github.com/jackd248/db-sync-tool

0 commit comments

Comments
 (0)