@@ -21,18 +21,18 @@ class DeclarativeUtils:
2121 """
2222
2323 async def __new__ (
24- cls , raw_database : str , project_id : str , session_manager : SQLSessionManager , schema : str , raw_db : bool = False
24+ cls , raw_database : str , tenant_id : str , session_manager : SQLSessionManager , schema : str , raw_db : bool = False
2525 ) -> None :
2626 obj = super ().__new__ (cls )
27- obj .__init__ (raw_database , project_id , session_manager , schema , raw_db )
27+ obj .__init__ (raw_database , tenant_id , session_manager , schema , raw_db )
2828 await obj ._get_declarative_module ()
2929 return obj
3030
3131 def __init__ (
32- self , raw_database : str , project_id : str , session_manager : SQLSessionManager , schema : str , raw_db : bool = False
32+ self , raw_database : str , tenant_id : str , session_manager : SQLSessionManager , schema : str , raw_db : bool = False
3333 ) -> None :
3434 self .raw_database : str = raw_database
35- self .project_id : str = project_id
35+ self .tenant_id : str = tenant_id
3636 self .session_manager : SQLSessionManager = session_manager
3737 self .raw_db = raw_db
3838 self .schema = schema
@@ -43,29 +43,27 @@ async def _pre_check(self):
4343 await self ._get_declarative_module ()
4444
4545 async def _prepare_declarative_file (self , refresh : bool = False ):
46- declarative_project_directory = PathConfig .DECLARATIVES_PATH / self .project_id
47- if not declarative_project_directory .exists ():
48- declarative_project_directory .mkdir (parents = True )
49- project_init_file = declarative_project_directory / "__init__.py"
50- if not project_init_file .exists ():
51- with open (project_init_file , "w" ) as f :
46+ declarative_tenant_directory = PathConfig .DECLARATIVES_PATH / self .tenant_id
47+ if not declarative_tenant_directory .exists ():
48+ declarative_tenant_directory .mkdir (parents = True )
49+ tenant_init_file = declarative_tenant_directory / "__init__.py"
50+ if not tenant_init_file .exists ():
51+ with open (tenant_init_file , "w" ) as f :
5252 f .write ("" )
53- declarative_file = declarative_project_directory / f"async_{ self .raw_database } _{ self .schema } .py"
53+ declarative_file = declarative_tenant_directory / f"async_{ self .raw_database } _{ self .schema } .py"
5454 if declarative_file .exists () and ModuleConfig .DEFER_GEN_REFRESH and not refresh :
55- return f"{ self .project_id } .async_{ self .raw_database } _{ self .schema } "
55+ return f"{ self .tenant_id } .async_{ self .raw_database } _{ self .schema } "
5656 try :
5757 logging .debug (f"Attempting to create declarative file: { declarative_file } " )
5858 from sql_db_utils .asyncio .codegen import UTDeclarativeGenerator
5959
60- session = await self .session_manager .get_session (
61- self .raw_database , None if self .raw_db else self .project_id
62- )
60+ session = await self .session_manager .get_session (self .raw_database , None if self .raw_db else self .tenant_id )
6361 meta = MetaData ()
6462 async with session .bind .begin () as conn :
6563 await conn .run_sync (meta .reflect , schema = self .schema )
6664 with open (declarative_file , "w" , encoding = "utf-8" ) as f :
6765 generator = UTDeclarativeGenerator (
68- raw_database = self .raw_database if self .raw_db else f"{ self .project_id } __{ self .raw_database } " ,
66+ raw_database = self .raw_database if self .raw_db else f"{ self .tenant_id } __{ self .raw_database } " ,
6967 metadata = meta ,
7068 bind = session .bind ,
7169 options = set (),
@@ -79,7 +77,7 @@ async def _prepare_declarative_file(self, refresh: bool = False):
7977 except Exception as e :
8078 logging .error (f"Error creating declarative file: { e } " )
8179 return False
82- return f"{ self .project_id } .async_{ self .raw_database } _{ self .schema } "
80+ return f"{ self .tenant_id } .async_{ self .raw_database } _{ self .schema } "
8381
8482 async def _get_declarative_module (self ): # NOSONAR
8583 if declarative_module_path := await self ._prepare_declarative_file ():
@@ -97,8 +95,20 @@ async def _get_declarative_module(self): # NOSONAR
9795 try :
9896 import asyncio
9997
100- loop = asyncio .get_event_loop ()
101- loop .stop ()
98+ logging .warning ("Emergency shutdown required - gracefully canceling tasks" )
99+ loop = asyncio .get_running_loop ()
100+ tasks = [t for t in asyncio .all_tasks (loop ) if t is not asyncio .current_task ()]
101+ logging .debug (f"Canceling { len (tasks )} pending tasks" )
102+
103+ for task in tasks :
104+ task .cancel ()
105+
106+ # Wait for all tasks to complete with cancellation
107+ if tasks :
108+ await asyncio .gather (* tasks , return_exceptions = True )
109+
110+ logging .info ("Tasks gracefully canceled, exiting" )
111+ sys .exit (1 )
102112 except ImportError :
103113 logging .error ("Not asyncio module, stopping using sys.exit" )
104114 sys .exit (1 )
@@ -169,105 +179,54 @@ def get_declarative_utils_factory(
169179 self ,
170180 raw_database : str ,
171181 session_manager : SQLSessionManager ,
172- security_enabled : bool = True ,
173182 ):
174- if security_enabled :
175- try :
176- from ut_security_util import MetaInfoSchema
177-
178- async def get_declarative_utils (
179- meta : MetaInfoSchema ,
180- schema : Annotated [str , Query ] = PostgresConfig .PG_DEFAULT_SCHEMA ,
181- ) -> DeclarativeUtils :
182- global declarative_utils
183- if declarative_util := declarative_utils .get (f"{ raw_database } _{ meta .project_id } _{ schema } " ):
184- await declarative_util ._pre_check ()
185- return declarative_util
186- else :
187- declarative_util = await DeclarativeUtils (
188- raw_database , meta .project_id , session_manager , schema
189- )
190- declarative_utils [f"{ raw_database } _{ meta .project_id } _{ schema } " ] = declarative_util
191- return declarative_util
192-
193- return get_declarative_utils
194- except ImportError :
195- logging .error ("ut_security_util not installed, please install it to use security features" )
196- raise
197- else :
198-
199- async def get_declarative_utils (
200- project_id : Annotated [str , Cookie ], schema : Annotated [str , Query ] = PostgresConfig .PG_DEFAULT_SCHEMA
201- ) -> DeclarativeUtils :
202- global declarative_utils
203- if declarative_util := declarative_utils .get (f"{ raw_database } _{ project_id } _{ schema } " ):
204- await declarative_util ._pre_check ()
205- return declarative_util
206- else :
207- declarative_util = await DeclarativeUtils (raw_database , project_id , session_manager , schema )
208- declarative_utils [f"{ raw_database } _{ project_id } _{ schema } " ] = declarative_util
209- return declarative_util
210-
211- return get_declarative_utils
183+ async def get_declarative_utils (
184+ tenant_id : Annotated [str , Cookie ], schema : Annotated [str , Query ] = PostgresConfig .PG_DEFAULT_SCHEMA
185+ ) -> DeclarativeUtils :
186+ global declarative_utils
187+ if declarative_util := declarative_utils .get (f"{ raw_database } _{ tenant_id } _{ schema } " ):
188+ await declarative_util ._pre_check ()
189+ return declarative_util
190+ else :
191+ declarative_util = await DeclarativeUtils (raw_database , tenant_id , session_manager , schema )
192+ declarative_utils [f"{ raw_database } _{ tenant_id } _{ schema } " ] = declarative_util
193+ return declarative_util
194+
195+ return get_declarative_utils
212196
213197 def get_schema_mandated_declarative_utils_factory (
214198 self ,
215199 raw_database : str ,
216200 session_manager : SQLSessionManager ,
217201 schema : str ,
218- security_enabled : bool = True ,
219202 ):
220- if security_enabled :
221- try :
222- from ut_security_util import MetaInfoSchema
223-
224- async def get_declarative_utils (
225- meta : MetaInfoSchema ,
226- ) -> DeclarativeUtils :
227- global declarative_utils
228- if declarative_util := declarative_utils .get (f"{ raw_database } _{ meta .project_id } _{ schema } " ):
229- await declarative_util ._pre_check ()
230- return declarative_util
231- else :
232- declarative_util = await DeclarativeUtils (
233- raw_database , meta .project_id , session_manager , schema
234- )
235- declarative_utils [f"{ raw_database } _{ meta .project_id } _{ schema } " ] = declarative_util
236- return declarative_util
237-
238- return get_declarative_utils
239- except ImportError :
240- logging .error ("ut_security_util not installed, please install it to use security features" )
241- raise
242- else :
243-
244- async def get_declarative_utils (project_id : Annotated [str , Cookie ]) -> DeclarativeUtils :
245- global declarative_utils
246- if declarative_util := declarative_utils .get (f"{ raw_database } _{ project_id } _{ schema } " ):
247- await declarative_util ._pre_check ()
248- return declarative_util
249- else :
250- declarative_util = await DeclarativeUtils (raw_database , project_id , session_manager , schema )
251- declarative_utils [f"{ raw_database } _{ project_id } _{ schema } " ] = declarative_util
252- return declarative_util
253-
254- return get_declarative_utils
203+ async def get_declarative_utils (tenant_id : Annotated [str , Cookie ]) -> DeclarativeUtils :
204+ global declarative_utils
205+ if declarative_util := declarative_utils .get (f"{ raw_database } _{ tenant_id } _{ schema } " ):
206+ await declarative_util ._pre_check ()
207+ return declarative_util
208+ else :
209+ declarative_util = await DeclarativeUtils (raw_database , tenant_id , session_manager , schema )
210+ declarative_utils [f"{ raw_database } _{ tenant_id } _{ schema } " ] = declarative_util
211+ return declarative_util
212+
213+ return get_declarative_utils
255214
256215 async def get_declarative_utils (
257216 self ,
258217 raw_database : str ,
259- project_id : str ,
218+ tenant_id : str ,
260219 session_manager : SQLSessionManager ,
261220 schema : str = PostgresConfig .PG_DEFAULT_SCHEMA ,
262221 raw_db : bool = False ,
263222 ) -> DeclarativeUtils :
264223 global declarative_utils
265- if declarative_util := declarative_utils .get (f"{ raw_database } _{ project_id } _{ schema } " ):
224+ if declarative_util := declarative_utils .get (f"{ raw_database } _{ tenant_id } _{ schema } " ):
266225 await declarative_util ._pre_check ()
267226 return declarative_util
268227 else :
269- declarative_util = await DeclarativeUtils (raw_database , project_id , session_manager , schema , raw_db )
270- declarative_utils [f"{ raw_database } _{ project_id } _{ schema } " ] = declarative_util
228+ declarative_util = await DeclarativeUtils (raw_database , tenant_id , session_manager , schema , raw_db )
229+ declarative_utils [f"{ raw_database } _{ tenant_id } _{ schema } " ] = declarative_util
271230 return declarative_util
272231
273232
0 commit comments