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 ,
@@ -392,7 +396,7 @@ async def remove_guest_user_with_all_its_resources(
392
396
await remove_all_projects_for_user (app = app , user_id = user_id )
393
397
await remove_user (app = app , user_id = user_id )
394
398
395
- except psycopg2 . DatabaseError :
399
+ except database_errors :
396
400
logger .warning (
397
401
"Could not remove GUEST with id=%s. Check the logs above for details" ,
398
402
user_id ,
@@ -587,7 +591,7 @@ async def replace_current_owner(
587
591
app = app , primary_gid = int (new_project_owner_gid )
588
592
)
589
593
590
- except psycopg2 . DatabaseError :
594
+ except database_errors :
591
595
logger .exception (
592
596
"Could not recover new user id from gid %s" , new_project_owner_gid
593
597
)
@@ -615,7 +619,7 @@ async def replace_current_owner(
615
619
project_data = project ,
616
620
project_uuid = project_uuid ,
617
621
)
618
- except psycopg2 . DatabaseError :
622
+ except database_errors :
619
623
logger .exception (
620
624
"Could not remove old owner and replaced it with user %s" ,
621
625
new_project_owner_id ,
@@ -626,7 +630,7 @@ async def remove_user(app: web.Application, user_id: int) -> None:
626
630
"""Tries to remove a user, if the users still exists a warning message will be displayed"""
627
631
try :
628
632
await delete_user (app , user_id )
629
- except psycopg2 . DatabaseError :
633
+ except database_errors :
630
634
logger .warning (
631
635
"User '%s' still has some projects, could not be deleted" , user_id
632
636
)
0 commit comments