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

Daemon startup, background scans
Ref #253
This commit is contained in:
Alban Féron 2023-04-20 18:17:10 +02:00
parent 893a007f29
commit 32a74706c2
No known key found for this signature in database
GPG Key ID: 8CE0313646D16165
4 changed files with 20 additions and 7 deletions

View File

@ -1,7 +1,7 @@
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2019-2022 Alban 'spl0k' Féron
# Copyright (C) 2019-2023 Alban 'spl0k' Féron
#
# Distributed under terms of the GNU AGPLv3 license.
@ -12,7 +12,7 @@ from multiprocessing.connection import Listener, Client
from threading import Thread, Event
from .client import DaemonCommand
from ..db import Folder
from ..db import Folder, open_connection, close_connection
from ..jukebox import Jukebox
from ..scanner import Scanner
from ..utils import get_secret_key
@ -59,6 +59,8 @@ class Daemon:
if self.__config.DAEMON["jukebox_command"]:
self.__jukebox = Jukebox(self.__config.DAEMON["jukebox_command"])
close_connection()
Thread(target=self.__listen).start()
while not self.__stopped.is_set():
time.sleep(1)
@ -72,9 +74,11 @@ class Daemon:
def start_scan(self, folders=[], force=False):
if not folders:
open_connection()
folders = [
t[0] for t in Folder.select(Folder.name).where(Folder.root).tuples()
]
close_connection()
if self.__scanner is not None and self.__scanner.is_alive():
for f in folders:

View File

@ -681,8 +681,8 @@ def release_database():
db.initialize(None)
def open_connection():
db.connect()
def open_connection(reuse=False):
return db.connect(reuse)
def close_connection():

View File

@ -1,7 +1,7 @@
# This file is part of Supysonic.
# 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.
@ -16,7 +16,7 @@ from queue import Queue, Empty as QueueEmpty
from threading import Thread, Event
from .covers import find_cover_in_folder, CoverFile
from .db import Folder, Artist, Album, Track
from .db import Folder, Artist, Album, Track, open_connection, close_connection
logger = logging.getLogger(__name__)
@ -95,6 +95,8 @@ class Scanner(Thread):
self.__queue.put(folder_name)
def run(self):
opened = open_connection(True)
while not self.__stopped.is_set():
try:
folder_name = self.__queue.get(False)
@ -113,6 +115,9 @@ class Scanner(Thread):
if self.__on_done is not None:
self.__on_done()
if opened:
close_connection()
def stop(self):
self.__stopped.set()

View File

@ -51,7 +51,11 @@ def create_application(config=None):
# Initialize database
init_database(app.config["BASE"]["database_uri"])
if not app.testing:
app.before_request(open_connection)
def open_conn(): # Just to discard the return value
open_connection()
app.before_request(open_conn)
app.teardown_request(lambda exc: close_connection())
# Insert unknown mimetypes