|
5 | 5 | require_once('feature_init.php'); |
6 | 6 |
|
7 | 7 |
|
| 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 | + |
8 | 56 | task('feature:sync', function () { |
9 | 57 |
|
10 | 58 | if ((has('feature_setup') && !get('feature_setup')) || !input()->getOption('feature')) return; |
|
13 | 61 | $synced = false; |
14 | 62 | $optionalVerbose = isVerbose() ? '-v' : ''; |
15 | 63 |
|
| 64 | + waitForDatabaseHost(); |
| 65 | + |
16 | 66 | /* |
17 | 67 | * db_sync_tool |
18 | 68 | * https://github.com/jackd248/db-sync-tool |
|
0 commit comments