🩹 Ensure ORM initialization and tear-down
This commit is contained in:
parent
485ddc01fb
commit
b6d209926f
1 changed files with 26 additions and 1 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import asyncio
|
||||
import atexit
|
||||
import importlib
|
||||
import sys
|
||||
import typer
|
||||
|
|
@ -45,6 +46,28 @@ def shell(root: str = "."):
|
|||
project_path = Path(root).resolve()
|
||||
project = runtime.Project(project_path)
|
||||
|
||||
# Ensure the ORM is shutdown
|
||||
async def close_project():
|
||||
try:
|
||||
await project.close_orm()
|
||||
print("Tortoise ORM closed successfully.")
|
||||
except Exception as e:
|
||||
print(f"Error closing ORM: {e}")
|
||||
|
||||
def cleanup():
|
||||
loop = None
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
pass
|
||||
if loop and loop.is_running():
|
||||
asyncio.create_task(close_project())
|
||||
else:
|
||||
asyncio.run(close_project())
|
||||
|
||||
# Ensure the ORM is initialized
|
||||
asyncio.run(project.init_orm())
|
||||
|
||||
try:
|
||||
from IPython import start_ipython
|
||||
shell_vars = {
|
||||
|
|
@ -56,11 +79,13 @@ def shell(root: str = "."):
|
|||
c.TerminalInteractiveShell.banner2 = banner.format(**{
|
||||
"project_name": f"{f'{project.settings.PROJECT_NAME} ' if getattr(project.settings, 'PROJECT_NAME', '') else ''}[{Path(project_path).resolve()}]",
|
||||
})
|
||||
atexit.register(cleanup)
|
||||
start_ipython(argv=[], user_ns=shell_vars, config=c)
|
||||
except ImportError:
|
||||
typer.echo("IPython is not installed. Falling back to built-in Python shell.")
|
||||
import code
|
||||
code.interact(local={"settings": project.settings})
|
||||
atexit.register(cleanup)
|
||||
code.interact(local={"p": project})
|
||||
|
||||
|
||||
@app.command()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue