diff --git a/tools/local-env/scripts/install.js b/tools/local-env/scripts/install.js index 19a0f46e08680..4ac7804f7b9e9 100644 --- a/tools/local-env/scripts/install.js +++ b/tools/local-env/scripts/install.js @@ -49,7 +49,7 @@ wait_on( { process.exit( 1 ); } ) .then( () => { - wp_cli( 'db reset --yes' ); + wp_cli_retry( 'db reset --yes' ); const installCommand = process.env.LOCAL_MULTISITE === 'true' ? 'multisite-install' : 'install'; wp_cli( `core ${ installCommand } --title="WordPress Develop" --admin_user=admin --admin_password=password --admin_email=test@example.com --skip-email --url=http://localhost:${process.env.LOCAL_PORT}` ); } ) @@ -66,3 +66,27 @@ wait_on( { function wp_cli( cmd ) { execSync( `npm --silent run env:cli -- ${cmd} --path=/var/www/${process.env.LOCAL_DIR}`, { stdio: 'inherit' } ); } + +/** + * Runs a WP-CLI command in the Docker environment with retry logic. + * + * If the command fails, the function will retry the command the + * specified number of times with a short delay between attempts. + * + * @param {string} cmd The WP-CLI command to run. + * @param {number} retries The number of retries left. Default to 5. + */ +function wp_cli_retry( cmd, retries = 5 ) { + try { + wp_cli( cmd ); + } catch ( e ) { + if ( retries > 0 ) { + console.warn( `Retrying command "${cmd}" (${retries} retries left)...` ); + // Wait a bit before retrying. + setTimeout( () => {}, 1000 ); + wp_cli_retry( cmd, retries - 1 ); + } else { + throw e; + } + } +}