refactor(scheduler): migrate from per-user to system-wide scan settings

Update scheduler to use system-wide settings instead of per-user:
- Change Database interface to use GetSystemSetting
- Remove GetScanSettings (per-user method)
- Update checkAndScheduleScans to read system settings
- Apply system-wide scan frequency to all libraries

Scheduler now respects global scan settings for all library scanning,
enabling consistent system-wide scan behavior.
This commit is contained in:
2026-02-09 20:10:40 -05:00
parent 938e5eed53
commit 0551f17f0e
+41 -24
View File
@@ -5,6 +5,7 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"strconv"
"sync" "sync"
"time" "time"
@@ -24,16 +25,11 @@ type Scheduler struct {
} }
type Database interface { type Database interface {
GetScanSettings(ctx context.Context, id pgtype.UUID) (database.GetScanSettingsRow, error) GetSystemSetting(ctx context.Context, settingKey string) (string, error)
ListLibraries(ctx context.Context) ([]database.ListLibrariesRow, error) ListLibraries(ctx context.Context) ([]database.ListLibrariesRow, error)
GetLibraryFolders(ctx context.Context, libraryID pgtype.UUID) ([]database.LibraryFolders, error) GetLibraryFolders(ctx context.Context, libraryID pgtype.UUID) ([]database.LibraryFolders, error)
} }
type ScanSettingRow struct {
ScanFrequencyMinutes pgtype.Int4
AutoScanEnabled pgtype.Bool
}
type Library struct { type Library struct {
ID pgtype.UUID ID pgtype.UUID
Name string Name string
@@ -111,32 +107,53 @@ func (s *Scheduler) checkAndScheduleScans() {
return return
} }
autoScanEnabledStr, err := s.db.GetSystemSetting(ctx, "auto_scan_enabled")
if err != nil {
log.Printf("Error getting auto_scan_enabled setting: %v", err)
return
}
autoScanEnabled, err := strconv.ParseBool(autoScanEnabledStr)
if err != nil {
log.Printf("Error parsing auto_scan_enabled: %v", err)
return
}
if !autoScanEnabled {
return
}
scanFrequencyStr, err := s.db.GetSystemSetting(ctx, "scan_frequency_minutes")
if err != nil {
log.Printf("Error getting scan_frequency_minutes setting: %v", err)
return
}
scanFrequency, err := strconv.Atoi(scanFrequencyStr)
if err != nil {
log.Printf("Error parsing scan_frequency_minutes: %v", err)
return
}
if scanFrequency < 15 {
return
}
for _, library := range libraries { for _, library := range libraries {
settings, err := s.db.GetScanSettings(ctx, library.ID) libraryIDStr := fmt.Sprintf("%x", library.ID.Bytes)
if err != nil {
libraryIDStr := fmt.Sprintf("%x", library.ID.Bytes)
log.Printf("Error getting scan settings for library %s: %v", libraryIDStr, err)
continue
}
if !settings.AutoScanEnabled.Bool || settings.ScanFrequencyMinutes.Int32 < 15 {
continue
}
userID := fmt.Sprintf("%x", library.ID.Bytes)
s.mu.Lock() s.mu.Lock()
currentSetting, exists := s.scanSettings[userID] currentSetting, exists := s.scanSettings[libraryIDStr]
scanSetting := ScanSetting{ scanSetting := ScanSetting{
UserID: userID, UserID: libraryIDStr,
Enabled: settings.AutoScanEnabled.Bool, Enabled: autoScanEnabled,
Frequency: int(settings.ScanFrequencyMinutes.Int32), Frequency: scanFrequency,
} }
s.scanSettings[userID] = scanSetting s.scanSettings[libraryIDStr] = scanSetting
s.mu.Unlock() s.mu.Unlock()
if !exists || currentSetting.Frequency != scanSetting.Frequency { if !exists || currentSetting.Frequency != scanSetting.Frequency {
s.scheduleLibraryScan(ctx, library.ID, userID, scanSetting.Frequency) s.scheduleLibraryScan(ctx, library.ID, libraryIDStr, scanSetting.Frequency)
} }
} }
} }