1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 08:56:17 +00:00

Properly close database connections when they're not in use

CLI and web only
Ref #253
This commit is contained in:
Alban Féron 2023-04-20 16:40:51 +02:00
parent f3e743dece
commit 893a007f29
No known key found for this signature in database
GPG Key ID: 8CE0313646D16165
3 changed files with 21 additions and 5 deletions

View File

@ -1,7 +1,7 @@
# This file is part of Supysonic. # This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API. # Supysonic is a Python implementation of the Subsonic server API.
# #
# Copyright (C) 2013-2022 Alban 'spl0k' Féron # Copyright (C) 2013-2023 Alban 'spl0k' Féron
# #
# Distributed under terms of the GNU AGPLv3 license. # Distributed under terms of the GNU AGPLv3 license.
@ -364,8 +364,10 @@ def user_rename(name, newname):
def main(): def main():
config = IniConfig.from_common_locations() config = IniConfig.from_common_locations()
init_database(config.BASE["database_uri"]) init_database(config.BASE["database_uri"])
cli.main(obj=config) try:
release_database() cli.main(obj=config)
finally:
release_database()
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -679,3 +679,11 @@ def init_database(database_uri):
def release_database(): def release_database():
db.close() db.close()
db.initialize(None) db.initialize(None)
def open_connection():
db.connect()
def close_connection():
db.close()

View File

@ -1,7 +1,7 @@
# This file is part of Supysonic. # This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API. # Supysonic is a Python implementation of the Subsonic server API.
# #
# Copyright (C) 2013-2022 Alban 'spl0k' Féron # Copyright (C) 2013-2023 Alban 'spl0k' Féron
# 2018-2019 Carey 'pR0Ps' Metcalfe # 2018-2019 Carey 'pR0Ps' Metcalfe
# 2017 Óscar García Amor # 2017 Óscar García Amor
# #
@ -16,7 +16,7 @@ from os import makedirs, path
from .config import IniConfig from .config import IniConfig
from .cache import Cache from .cache import Cache
from .db import init_database from .db import init_database, open_connection, close_connection
from .utils import get_secret_key from .utils import get_secret_key
logger = logging.getLogger(__package__) logger = logging.getLogger(__package__)
@ -50,6 +50,9 @@ def create_application(config=None):
# Initialize database # Initialize database
init_database(app.config["BASE"]["database_uri"]) init_database(app.config["BASE"]["database_uri"])
if not app.testing:
app.before_request(open_connection)
app.teardown_request(lambda exc: close_connection())
# Insert unknown mimetypes # Insert unknown mimetypes
for k, v in app.config["MIMETYPES"].items(): for k, v in app.config["MIMETYPES"].items():
@ -83,4 +86,7 @@ def create_application(config=None):
app.register_blueprint(api, url_prefix="/rest") app.register_blueprint(api, url_prefix="/rest")
if not app.testing:
close_connection()
return app return app