This commit is contained in:
2026-04-12 11:38:21 +02:00
parent 99cab7be86
commit 88a3d2f67b

View File

@@ -12,7 +12,13 @@ from pathlib import Path
from typing import Callable from typing import Callable
try: try:
from watchdog.observers import Observer import sys
if sys.platform == "win32":
# WindowsApiObserver opretter en tråd per undermappe og løber tør
# for Windows handles ved store biblioteker — brug polling i stedet
from watchdog.observers.polling import PollingObserver as Observer
else:
from watchdog.observers import Observer
from watchdog.events import ( from watchdog.events import (
FileSystemEventHandler, FileSystemEventHandler,
FileCreatedEvent, FileCreatedEvent,
@@ -23,7 +29,6 @@ try:
WATCHDOG_AVAILABLE = True WATCHDOG_AVAILABLE = True
except ImportError: except ImportError:
WATCHDOG_AVAILABLE = False WATCHDOG_AVAILABLE = False
print("Advarsel: watchdog ikke installeret — fil-overvågning deaktiveret")
from local.tag_reader import is_supported, read_tags, get_file_modified_at from local.tag_reader import is_supported, read_tags, get_file_modified_at
from local.local_db import ( from local.local_db import (
@@ -184,16 +189,26 @@ class LibraryWatcher:
self._observer.schedule(handler, str(path), recursive=True) self._observer.schedule(handler, str(path), recursive=True)
def _full_scan_all(self): def _full_scan_all(self):
"""Kør fuld scan på alle aktive biblioteker — i baggrundstråde.""" """Kør fuld scan på alle aktive biblioteker — én ad gangen i baggrundstråd."""
for lib in get_libraries(active_only=True): def _run():
path = Path(lib["path"]) try:
if path.exists(): import sqlite3
t = threading.Thread( from local.local_db import DB_PATH
target=self._full_scan_library, conn = sqlite3.connect(str(DB_PATH))
args=(lib["id"], str(path)), conn.row_factory = sqlite3.Row
daemon=True libs = conn.execute(
) "SELECT id, path FROM libraries WHERE is_active=1"
t.start() ).fetchall()
conn.close()
for lib in libs:
lib_path = Path(lib["path"])
if lib_path.exists():
self._full_scan_library(lib["id"], str(lib_path))
except Exception as e:
logger.error(f"_full_scan_all fejl: {e}")
# Kør i én baggrundstråd — undgår Windows handle-udmattelse
threading.Thread(target=_run, daemon=True).start()
def _full_scan_library(self, library_id: int, library_path: str): def _full_scan_library(self, library_id: int, library_path: str):
"""Scan ét bibliotek med sin egen SQLite-forbindelse — blokerer aldrig GUI.""" """Scan ét bibliotek med sin egen SQLite-forbindelse — blokerer aldrig GUI."""