From 436bf0ef46cafbf6f64968c10c3e4787bee0cae0 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Sat, 31 Jan 2026 21:55:13 -0600 Subject: [PATCH] Build: Add retry logic for database connection in install script. The `wp config create` command can fail with "Connection refused" when the database container reports healthy but isn't yet accepting connections. This race condition causes intermittent CI failures. Add `wp_cli_with_retry()` function with exponential backoff to handle database startup timing issues gracefully. --- tools/local-env/scripts/install.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/tools/local-env/scripts/install.js b/tools/local-env/scripts/install.js index 80b5c7eb19836..8aae308e14150 100644 --- a/tools/local-env/scripts/install.js +++ b/tools/local-env/scripts/install.js @@ -9,8 +9,8 @@ const local_env_utils = require( './utils' ); dotenvExpand.expand( dotenv.config() ); -// Create wp-config.php. -wp_cli( `config create --dbname=wordpress_develop --dbuser=root --dbpass=password --dbhost=mysql --force --config-file="wp-config.php"` ); +// Create wp-config.php. Use retry logic to handle database startup race conditions. +wp_cli_with_retry( `config create --dbname=wordpress_develop --dbuser=root --dbpass=password --dbhost=mysql --force --config-file="wp-config.php"` ); // Add the debug settings to wp-config.php. // Windows requires this to be done as an additional step, rather than using the --extra-php option in the previous step. @@ -60,3 +60,26 @@ wait_on( { function wp_cli( cmd ) { execSync( `npm --silent run env:cli -- ${cmd} --path=/var/www/${process.env.LOCAL_DIR}`, { stdio: 'inherit' } ); } + +/** + * Runs WP-CLI commands with retry logic for database connection issues. + * + * @param {string} cmd The WP-CLI command to run. + * @param {number} maxRetries Maximum number of retry attempts. Default 5. + * @param {number} delay Initial delay in milliseconds between retries. Default 1000. + */ +function wp_cli_with_retry( cmd, maxRetries = 5, delay = 1000 ) { + for ( let i = 0; i < maxRetries; i++ ) { + try { + wp_cli( cmd ); + return; + } catch ( err ) { + if ( i === maxRetries - 1 ) { + throw err; + } + const waitTime = delay * Math.pow( 2, i ); + console.log( `Database connection failed. Retrying in ${ waitTime }ms... (attempt ${ i + 1 }/${ maxRetries })` ); + execSync( `sleep ${ waitTime / 1000 }` ); + } + } +}