From 0f380f15d8b00d67b31770359a285529808a7f3c Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 28 Jul 2025 13:59:21 +1000 Subject: [PATCH 1/2] waiton mysql server when starting environment. Silly. Quite silly. that will never work. --- tools/local-env/scripts/install.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tools/local-env/scripts/install.js b/tools/local-env/scripts/install.js index 19a0f46e08680..e2593e0a0a9d4 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,18 @@ wait_on( { function wp_cli( cmd ) { execSync( `npm --silent run env:cli -- ${cmd} --path=/var/www/${process.env.LOCAL_DIR}`, { stdio: 'inherit' } ); } + +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; + } + } +} From 8d5d3f76ead4db535bda5b37a8d03c3e15eb9c56 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 28 Jul 2025 15:34:14 +1000 Subject: [PATCH 2/2] Add docblock to the retry command. --- tools/local-env/scripts/install.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tools/local-env/scripts/install.js b/tools/local-env/scripts/install.js index e2593e0a0a9d4..4ac7804f7b9e9 100644 --- a/tools/local-env/scripts/install.js +++ b/tools/local-env/scripts/install.js @@ -67,6 +67,15 @@ 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 );