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
49
50
from .registry import RedisResourceRegistry , get_registry
50
51
51
52
logger = logging .getLogger (__name__ )
53
+ database_errors = (psycopg2 .DatabaseError , asyncpg .exceptions .PostgresError )
52
54
53
55
54
56
def setup_garbage_collector (app : web .Application ):
@@ -60,11 +62,13 @@ async def _setup_background_task(app: web.Application):
60
62
61
63
yield
62
64
65
+ logger .info ("Stopping garbage collector..." )
63
66
# on_cleanup
64
67
with suppress (asyncio .CancelledError ):
65
68
task = app [APP_GARBAGE_COLLECTOR_KEY ]
66
69
task .cancel ()
67
70
await task
71
+ logger .info ("Garbage collector stopped" )
68
72
69
73
app .cleanup_ctx .append (_setup_background_task )
70
74
@@ -237,14 +241,15 @@ async def remove_disconnected_user_resources(
237
241
238
242
if resource_name == "project_id" :
239
243
# inform that the project can be closed on the backend side
244
+ # WARNING: slot functions can raise any exception
240
245
await emit (
241
246
event = "SIGNAL_PROJECT_CLOSE" ,
242
247
user_id = None ,
243
248
project_uuid = resource_value ,
244
249
app = app ,
245
250
)
246
251
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
248
253
# with the only associated project owned
249
254
await remove_guest_user_with_all_its_resources (
250
255
app = app ,
@@ -377,7 +382,7 @@ async def remove_guest_user_with_all_its_resources(
377
382
await remove_all_projects_for_user (app = app , user_id = user_id )
378
383
await remove_user (app = app , user_id = user_id )
379
384
380
- except psycopg2 . DatabaseError :
385
+ except database_errors :
381
386
logger .warning (
382
387
"Could not remove GUEST with id=%s. Check the logs above for details" ,
383
388
user_id ,
@@ -572,7 +577,7 @@ async def replace_current_owner(
572
577
app = app , primary_gid = int (new_project_owner_gid )
573
578
)
574
579
575
- except psycopg2 . DatabaseError :
580
+ except database_errors :
576
581
logger .exception (
577
582
"Could not recover new user id from gid %s" , new_project_owner_gid
578
583
)
@@ -600,7 +605,7 @@ async def replace_current_owner(
600
605
project_data = project ,
601
606
project_uuid = project_uuid ,
602
607
)
603
- except psycopg2 . DatabaseError :
608
+ except database_errors :
604
609
logger .exception (
605
610
"Could not remove old owner and replaced it with user %s" ,
606
611
new_project_owner_id ,
@@ -611,7 +616,7 @@ async def remove_user(app: web.Application, user_id: int) -> None:
611
616
"""Tries to remove a user, if the users still exists a warning message will be displayed"""
612
617
try :
613
618
await delete_user (app , user_id )
614
- except psycopg2 . DatabaseError :
619
+ except database_errors :
615
620
logger .warning (
616
621
"User '%s' still has some projects, could not be deleted" , user_id
617
622
)
0 commit comments