refactor(router): check auto_scan_enabled before starting watch mode

- Add check for auto_scan_enabled setting in router before starting watch mode
- Update StartScanner handler to verify auto-scan is enabled
- Switch scanner to use watchModeCtx/watchModeCancel instead of ctx/cancel
- Update StartWatchModeForLibrary to use new MediaScanner signature
This commit is contained in:
2026-02-28 12:57:27 -05:00
parent ce72781ec0
commit 5a2e1fda65
2 changed files with 22 additions and 25 deletions
+7 -25
View File
@@ -136,7 +136,10 @@ func (h *Handler) StartScanner(c echo.Context) error {
h.scanner.SetAdminID(pgtype.UUID{Bytes: userUUID, Valid: true})
// Start watching for changes
h.scanner.WatchChanges(h.ctx)
if !h.scanner.GetAutoScanEnabled() {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "auto-scan disabled in settings"})
}
h.scanner.WatchChanges(h.watchModeCtx)
return c.JSON(http.StatusOK, map[string]string{"message": "scanner started"})
}
@@ -146,8 +149,8 @@ func (h *Handler) StopScanner(c echo.Context) error {
h.mu.Lock()
defer h.mu.Unlock()
h.cancel()
h.ctx, h.cancel = context.WithCancel(context.Background())
h.watchModeCancel()
h.watchModeCtx, h.watchModeCancel = context.WithCancel(context.Background())
return c.JSON(http.StatusOK, map[string]string{"message": "scanner stopped"})
}
@@ -197,7 +200,7 @@ func (h *Handler) StartWatchModeForLibrary(ctx context.Context, libraryID pgtype
folderPaths[i] = folder.FolderPath
}
scanner := services.NewMediaScanner(h.db, 3)
scanner := services.NewMediaScanner(h.db)
if err := scanner.SetFolders(folderPaths); err != nil {
return fmt.Errorf("failed to set scanner folders: %v", err)
}
@@ -329,24 +332,3 @@ func (h *Handler) StartWatchModeForAllLibraries(ctx context.Context) error {
return nil
}
// StartScheduler starts the auto-scan scheduler
func (h *Handler) StartScheduler() {
h.scheduler.Start()
}
// StopScheduler stops the auto-scan scheduler
func (h *Handler) StopScheduler() {
h.scheduler.Stop()
h.worker.Shutdown()
// Stop the sync queue processor
if h.queueCancel != nil {
h.queueCancel()
}
// Stop connection manager cleanup task
if h.cleanupTaskCancel != nil {
h.cleanupTaskCancel()
}
}
+15
View File
@@ -13,6 +13,7 @@ import (
"context"
"log"
"net/http"
"strconv"
"strings"
"time"
@@ -238,6 +239,20 @@ func RegisterRoutes(cfg *Config) *handlers.Handler {
// Start watch mode for all libraries (after 2 second delay for DB)
go func() {
time.Sleep(2 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
setting, err := cfg.Queries.GetSystemSetting(ctx, "auto_scan_enabled")
enabled := true // default
if err == nil && setting != "" {
enabled, _ = strconv.ParseBool(setting)
}
if !enabled {
log.Println("Auto-scan disabled in settings, skipping watch mode startup")
return
}
log.Println("Starting watch mode for all libraries...")
if err := scannerHandler.StartWatchModeForAllLibraries(context.Background()); err != nil {
log.Printf("Failed to start watch mode: %v", err)