How to keep retrying some code until it works

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.

Last Updated: 2024-04-24

This function might fail if the DB is not provisioned yet (takes 10-40s on localhost)

install_wordpress_plugins() {
  wpcli core install \
    --title="${WORDPRESS_SITE_TITLE}" \
    --url="${WORDPRESS_SITE_URL}" \
    --admin_user="${WORDPRESS_ADMIN_USER}" \
    --admin_password="${WORDPRESS_ADMIN_PASSWORD}" \
    --admin_email="${WORDPRESS_ADMIN_EMAIL}"

  wpcli theme activate project_p 
}

How to run it until it succeeds? My first iteration "worked" but was brittle:

sleep 30
install_wordpress_plugins

Here's the improved version:

until install_wordpress_plugins; do
  printf "."
  sleep 5
done

This works because bash returns non-zero no matter what weird errors happen in that function. Until it returns 0, it keeps iterating. An improvement would be a max retry system.