4
4
from itertools import chain
5
5
from typing import Dict , List , Optional , Tuple
6
6
7
+ import asyncpg .exceptions
7
8
import psycopg2
8
9
from aiohttp import web
9
10
from aioredlock import Aioredlock
48
49
from .registry import RedisResourceRegistry , get_registry
49
50
50
51
logger = logging .getLogger (__name__ )
52
+ database_errors = (psycopg2 .DatabaseError , asyncpg .exceptions .PostgresError )
51
53
52
54
53
55
def setup_garbage_collector (app : web .Application ):
@@ -58,6 +60,7 @@ async def _setup_background_task(app: web.Application):
58
60
59
61
yield
60
62
63
+ logger .info ("Stopping garbage collector..." )
61
64
# on_cleanup
62
65
with suppress (asyncio .CancelledError ):
63
66
cgp_task .cancel ()
@@ -235,14 +238,15 @@ async def remove_disconnected_user_resources(
235
238
236
239
if resource_name == "project_id" :
237
240
# inform that the project can be closed on the backend side
241
+ # WARNING: slot functions can raise any exception
238
242
await emit (
239
243
event = "SIGNAL_PROJECT_CLOSE" ,
240
244
user_id = None ,
241
245
project_uuid = resource_value ,
242
246
app = app ,
243
247
)
244
248
245
- # if this user was a GUEST also remove it from the database
249
+ # ONLY GUESTS: if this user was a GUEST also remove it from the database
246
250
# with the only associated project owned
247
251
await remove_guest_user_with_all_its_resources (
248
252
app = app ,
@@ -375,7 +379,7 @@ async def remove_guest_user_with_all_its_resources(
375
379
await remove_all_projects_for_user (app = app , user_id = user_id )
376
380
await remove_user (app = app , user_id = user_id )
377
381
378
- except psycopg2 . DatabaseError :
382
+ except database_errors :
379
383
logger .warning (
380
384
"Could not remove GUEST with id=%s. Check the logs above for details" ,
381
385
user_id ,
@@ -570,7 +574,7 @@ async def replace_current_owner(
570
574
app = app , primary_gid = int (new_project_owner_gid )
571
575
)
572
576
573
- except psycopg2 . DatabaseError :
577
+ except database_errors :
574
578
logger .exception (
575
579
"Could not recover new user id from gid %s" , new_project_owner_gid
576
580
)
@@ -598,7 +602,7 @@ async def replace_current_owner(
598
602
project_data = project ,
599
603
project_uuid = project_uuid ,
600
604
)
601
- except psycopg2 . DatabaseError :
605
+ except database_errors :
602
606
logger .exception (
603
607
"Could not remove old owner and replaced it with user %s" ,
604
608
new_project_owner_id ,
@@ -609,7 +613,7 @@ async def remove_user(app: web.Application, user_id: int) -> None:
609
613
"""Tries to remove a user, if the users still exists a warning message will be displayed"""
610
614
try :
611
615
await delete_user (app , user_id )
612
- except psycopg2 . DatabaseError :
616
+ except database_errors :
613
617
logger .warning (
614
618
"User '%s' still has some projects, could not be deleted" , user_id
615
619
)
0 commit comments