From 75b814e26ca833fbba4c6a8a36a1aca3e4886833 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Sat, 19 Mar 2022 20:30:14 +0000 Subject: [PATCH] Session acquisitiong logging --- app/dbconfig.py | 20 +++++++++++++++++++- app/musicmuster.py | 3 ++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/dbconfig.py b/app/dbconfig.py index 9968bc4..4264323 100644 --- a/app/dbconfig.py +++ b/app/dbconfig.py @@ -1,7 +1,9 @@ +import inspect import os import sqlalchemy from config import Config +from contextlib import contextmanager from sqlalchemy.orm import (sessionmaker, scoped_session) MM_ENV = os.environ.get('MM_ENV', 'PRODUCTION') @@ -35,4 +37,20 @@ engine = sqlalchemy.create_engine( pool_pre_ping=True ) -Session = scoped_session(sessionmaker(bind=engine)) + +# Session = scoped_session(sessionmaker(bind=engine)) +@contextmanager +def Session(): + frame = inspect.stack()[2] + file = frame.filename + function = frame.function + lineno = frame.lineno + print(f"Session acquired, {file=}, {function=}, {lineno=}") + # yield scoped_session(sessionmaker(bind=engine)) + Session = scoped_session(sessionmaker(bind=engine)) + yield Session + print(" Session released") + Session.commit() + print(" Session committed") + Session.close() + print(" Session closed") diff --git a/app/musicmuster.py b/app/musicmuster.py index 4cebaa5..2dbb31c 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -518,6 +518,8 @@ class Window(QMainWindow, Ui_MainWindow): DEBUG("musicmuster.play_next(): no next track selected", True) return + self.kae() + with Session() as session: # If there's currently a track playing, fade it. self.stop_playing(fade=True) @@ -994,7 +996,6 @@ class SelectPlaylistDialog(QDialog): if __name__ == "__main__": try: Base.metadata.create_all(dbconfig.engine) - Session = dbconfig.Session app = QApplication(sys.argv) win = Window() win.show()