Skip to content

Commit bbf6996

Browse files
committed
Catches all db errors
1 parent cbe3f78 commit bbf6996

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

services/web/server/src/simcore_service_webserver/resource_manager/garbage_collector.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from itertools import chain
55
from typing import Dict, List, Optional, Tuple
66

7+
import asyncpg.exceptions
78
import psycopg2
89
from aiohttp import web
910
from aioredlock import Aioredlock
@@ -49,6 +50,7 @@
4950
from .registry import RedisResourceRegistry, get_registry
5051

5152
logger = logging.getLogger(__name__)
53+
database_errors = (psycopg2.DatabaseError, asyncpg.exceptions.PostgresError)
5254

5355

5456
def setup_garbage_collector(app: web.Application):
@@ -60,11 +62,13 @@ async def _setup_background_task(app: web.Application):
6062

6163
yield
6264

65+
logger.info("Stopping garbage collector...")
6366
# on_cleanup
6467
with suppress(asyncio.CancelledError):
6568
task = app[APP_GARBAGE_COLLECTOR_KEY]
6669
task.cancel()
6770
await task
71+
logger.info("Garbage collector stopped")
6872

6973
app.cleanup_ctx.append(_setup_background_task)
7074

@@ -237,14 +241,15 @@ async def remove_disconnected_user_resources(
237241

238242
if resource_name == "project_id":
239243
# inform that the project can be closed on the backend side
244+
# WARNING: slot functions can raise any exception
240245
await emit(
241246
event="SIGNAL_PROJECT_CLOSE",
242247
user_id=None,
243248
project_uuid=resource_value,
244249
app=app,
245250
)
246251

247-
# if this user was a GUEST also remove it from the database
252+
# ONLY GUESTS: if this user was a GUEST also remove it from the database
248253
# with the only associated project owned
249254
await remove_guest_user_with_all_its_resources(
250255
app=app,
@@ -377,7 +382,7 @@ async def remove_guest_user_with_all_its_resources(
377382
await remove_all_projects_for_user(app=app, user_id=user_id)
378383
await remove_user(app=app, user_id=user_id)
379384

380-
except psycopg2.DatabaseError:
385+
except database_errors:
381386
logger.warning(
382387
"Could not remove GUEST with id=%s. Check the logs above for details",
383388
user_id,
@@ -572,7 +577,7 @@ async def replace_current_owner(
572577
app=app, primary_gid=int(new_project_owner_gid)
573578
)
574579

575-
except psycopg2.DatabaseError:
580+
except database_errors:
576581
logger.exception(
577582
"Could not recover new user id from gid %s", new_project_owner_gid
578583
)
@@ -600,7 +605,7 @@ async def replace_current_owner(
600605
project_data=project,
601606
project_uuid=project_uuid,
602607
)
603-
except psycopg2.DatabaseError:
608+
except database_errors:
604609
logger.exception(
605610
"Could not remove old owner and replaced it with user %s",
606611
new_project_owner_id,
@@ -611,7 +616,7 @@ async def remove_user(app: web.Application, user_id: int) -> None:
611616
"""Tries to remove a user, if the users still exists a warning message will be displayed"""
612617
try:
613618
await delete_user(app, user_id)
614-
except psycopg2.DatabaseError:
619+
except database_errors:
615620
logger.warning(
616621
"User '%s' still has some projects, could not be deleted", user_id
617622
)

0 commit comments

Comments
 (0)