Skip to content
Closed
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
26 changes: 25 additions & 1 deletion tools/local-env/scripts/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}` );
} )
Expand All @@ -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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setTimeout() delays the execution of its callback, not the code that follows it, so this doesn't delay anything.

wp_cli_retry( cmd, retries - 1 );
} else {
throw e;
}
}
}
Loading