From abe0b799688ee9181b7d04f5e67f6fd800e2ab36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alban=20F=C3=A9ron?= Date: Fri, 21 Apr 2023 17:04:44 +0200 Subject: [PATCH] Properly close database connections when they're not in use Watcher Closes #253 --- supysonic/watcher.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/supysonic/watcher.py b/supysonic/watcher.py index 9307843..76fa6d5 100644 --- a/supysonic/watcher.py +++ b/supysonic/watcher.py @@ -14,7 +14,7 @@ from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler from . import covers -from .db import Folder +from .db import Folder, open_connection, close_connection from .scanner import Scanner OP_SCAN = 1 @@ -45,16 +45,9 @@ class SupysonicWatcherEventHandler(PatternMatchingEventHandler): logger.debug("File created: '%s'", event.src_path) op = OP_SCAN | FLAG_CREATE - if not covers.is_valid_cover(event.src_path): - self.queue.put(event.src_path, op) - - dirname = os.path.dirname(event.src_path) - try: - Folder.get(path=dirname) - except Folder.DoesNotExist: - self.queue.put(dirname, op | FLAG_COVER) - else: - self.queue.put(event.src_path, op | FLAG_COVER) + if covers.is_valid_cover(event.src_path): + op |= FLAG_COVER + self.queue.put(event.src_path, op) def on_deleted(self, event): logger.debug("File deleted: '%s'", event.src_path) @@ -154,6 +147,7 @@ class ScannerProcessingQueue(Thread): continue logger.debug("Instantiating scanner") + open_connection() scanner = Scanner() item = self.__next_item() @@ -166,6 +160,7 @@ class ScannerProcessingQueue(Thread): item = self.__next_item() scanner.prune() + close_connection() logger.debug("Freeing scanner") def __process_regular_item(self, scanner, item):