fix: Add container readiness wait after spawn

After spawning a new container, wait until it's fully started and services
are ready before returning to the frontend. This prevents race conditions
where the frontend opens the container URL before the service is ready.

Solution: Wait loop (max 30s) for container.status == 'running',
then additional 2s for service startup (code-server, PlatformIO, etc).

This fixes the 404 error when opening a freshly spawned container.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
XPS\Micro 2026-03-19 13:39:52 +01:00
parent 0d1f4f1db9
commit 24afef32e4

View File

@ -282,6 +282,29 @@ class ContainerManager:
container.remove(force=True)
raise Exception(f"Konnte Container nicht an Netzwerk '{Config.TRAEFIK_NETWORK}' verbinden: {str(e)}")
# Warte bis Container bereit ist (Health-Check)
print(f"[SPAWNER] Warte auf Container-Startup...")
import time
max_retries = 30 # 30 Sekunden max
retry_count = 0
while retry_count < max_retries:
try:
container.reload() # Aktualisiere Container-Status
if container.status == 'running':
# Container läuft - warte noch 2 Sekunden für Service-Startup
print(f"[SPAWNER] Container läuft, warte 2 Sekunden auf Service-Startup...")
time.sleep(2)
print(f"[SPAWNER] Container bereit!")
break
except Exception as e:
print(f"[SPAWNER] Fehler beim Status-Check: {str(e)}")
retry_count += 1
time.sleep(1)
if retry_count >= max_retries:
print(f"[SPAWNER] WARNUNG: Container wurde nicht bereit nach {max_retries}s")
print(f"[SPAWNER] {container_type.upper()} container created: {container.id[:12]}")
print(f"[SPAWNER] URL: {Config.PREFERRED_URL_SCHEME}://{base_host}/{slug_with_suffix}")
return container.id, 8080