fix: Auto-recreate missing containers instead of silently failing

- Improved exception handling in /api/container/launch/<container_type>
- Check if start_container() returns False and recreate container if failed
- Check if get_container_status() returns 'not_found' and recreate container
- Previously, start_container() returning False was silently ignored
- Now automatically spawns new container from template if old one is missing
- Better logging for container state transitions
This commit is contained in:
XPS\Micro 2026-03-18 17:54:59 +01:00
parent 5d59a67552
commit 458b2c605f

13
api.py
View File

@ -584,9 +584,16 @@ def api_container_launch(container_type):
# Container existiert - Status prüfen # Container existiert - Status prüfen
try: try:
status = container_mgr.get_container_status(user_container.container_id) status = container_mgr.get_container_status(user_container.container_id)
if status == 'not_found':
# Container nicht gefunden - neu erstellen
raise Exception(f"Container {user_container.container_id[:12]} nicht mehr vorhanden")
if status != 'running': if status != 'running':
# Container neu starten # Container neu starten
container_mgr.start_container(user_container.container_id) start_result = container_mgr.start_container(user_container.container_id)
if not start_result:
# Start fehlgeschlagen - neu erstellen
raise Exception(f"Container {user_container.container_id[:12]} konnte nicht gestartet werden")
current_app.logger.info(f"[MULTI-CONTAINER] Container {user_container.container_id[:12]} neu gestartet") current_app.logger.info(f"[MULTI-CONTAINER] Container {user_container.container_id[:12]} neu gestartet")
# last_used aktualisieren # last_used aktualisieren
@ -594,8 +601,8 @@ def api_container_launch(container_type):
db.session.commit() db.session.commit()
except Exception as e: except Exception as e:
# Container existiert nicht mehr - neu erstellen # Container existiert nicht mehr oder konnte nicht gestartet werden - neu erstellen
current_app.logger.warning(f"Container {user_container.container_id[:12]} nicht gefunden, erstelle neuen: {str(e)}") current_app.logger.warning(f"Container {user_container.container_id[:12]} nicht verfügbar, erstelle neuen: {str(e)}")
try: try:
template = current_app.config['CONTAINER_TEMPLATES'][container_type] template = current_app.config['CONTAINER_TEMPLATES'][container_type]
container_id, port = container_mgr.spawn_multi_container(user.id, user.slug, container_type) container_id, port = container_mgr.spawn_multi_container(user.id, user.slug, container_type)