Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions tools/local-env/scripts/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 }` );
}
}
}
Loading