# We use an 'until' loop. This will continue to run as long as the command
# We now wait for the Moodle web application itself to be fully responsive.
# fails (returns a non-zero exit code). It will only stop when the mysql
# We repeatedly curl the login page until we get a 200 OK status code.
# command is 100% successful, which guarantees the database is fully installed.
# This is the most reliable way to know the site is ready.
echo" -> Waiting for Moodle first-time installation to complete..."
echo" -> Waiting for Moodle web server to be ready..."
timeout=300 # 5 minute timeout
timeout=300 # 5 minute timeout
until docker exec"$DB_CONTAINER_NAME" mysql -u root -p"$DB_ROOT_PASSWORD""$DB_NAME"-e"SELECT 1 FROM mdl_config WHERE name = 'version' LIMIT 1;"> /dev/null 2>&1
until curl --output /dev/null --silent--head--fail"$MOODLE_URL/login/index.php";do
do
timeout=$((timeout-5))
timeout=$((timeout-5))
if[$timeout-le 0 ];then
if[$timeout-le 0 ];then
echo"❌ ERROR: Moodle installation timed out after 5 minutes."
echo"❌ ERROR: Moodle web server did not become ready after 5 minutes."
echo"--- MOODLE CONTAINER LOGS ---"
echo"--- MOODLE CONTAINER LOGS ---"
docker logs "$MOODLE_CONTAINER"
docker logs "$MOODLE_CONTAINER"
exit 1
exit 1
fi
fi
echo" -> Database not yet ready, waiting 5 more seconds..."
echo" -> Moodle web server not yet ready, waiting 5 more seconds..."
sleep 5
sleep 5
done
done
echo"✅ Moodle installation complete (database version found)."