@@ -38,13 +38,11 @@ def _on_init_done(self, future):
38
38
future .result ()
39
39
except Exception as e :
40
40
logging .error ("Initialization failed: %s" , e , exc_info = True )
41
- raise
42
- self ._initialized = True
43
- self ._initialized_event .set ()
41
+ raise e from e
44
42
45
43
async def _async_init (self ):
46
44
async with self ._lock :
47
- await self ._initialize_connection ()
45
+ self ._initialize_connection ()
48
46
self ._initialized = True
49
47
self ._initialized_event .set ()
50
48
@@ -54,11 +52,10 @@ async def __aenter__(self):
54
52
55
53
async def __aexit__ (self , exc_type , exc_value , traceback ):
56
54
await self ._initialized_event .wait ()
57
- self .close_connection ()
58
55
async with self ._lock :
59
56
self ._cleanup_instance ()
60
57
61
- async def _initialize_connection (self ):
58
+ def _initialize_connection (self ):
62
59
try :
63
60
self ._connection_string = Secrets .get_secret (
64
61
"MONGODB_CONNECTION_STRING"
@@ -81,6 +78,9 @@ async def _initialize_connection(self):
81
78
) from e
82
79
83
80
def _cleanup_instance (self ):
81
+ if hasattr (self , '_client' ):
82
+ self .client .close ()
83
+ logger .info ("Connection closed for %s" , self .__class__ )
84
84
self ._instances .pop (self .__class__ , None )
85
85
86
86
@property
@@ -114,8 +114,3 @@ def collection(self, value):
114
114
if not getattr (self , '_initialized' , False ):
115
115
raise RuntimeError ("Repository not initialized yet" )
116
116
self ._collection = value
117
-
118
- def close_connection (self ):
119
- if hasattr (self , '_client' ):
120
- self .client .close ()
121
- logger .info ("Connection closed for %s" , self .__class__ )
0 commit comments