diff --git a/internal/services/scheduler.go b/internal/services/scheduler.go index c714f61..3340fce 100644 --- a/internal/services/scheduler.go +++ b/internal/services/scheduler.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "log" + "strconv" "sync" "time" @@ -24,16 +25,11 @@ type Scheduler struct { } 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) GetLibraryFolders(ctx context.Context, libraryID pgtype.UUID) ([]database.LibraryFolders, error) } -type ScanSettingRow struct { - ScanFrequencyMinutes pgtype.Int4 - AutoScanEnabled pgtype.Bool -} - type Library struct { ID pgtype.UUID Name string @@ -111,32 +107,53 @@ func (s *Scheduler) checkAndScheduleScans() { 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 { - settings, err := s.db.GetScanSettings(ctx, library.ID) - 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) + libraryIDStr := fmt.Sprintf("%x", library.ID.Bytes) s.mu.Lock() - currentSetting, exists := s.scanSettings[userID] + currentSetting, exists := s.scanSettings[libraryIDStr] scanSetting := ScanSetting{ - UserID: userID, - Enabled: settings.AutoScanEnabled.Bool, - Frequency: int(settings.ScanFrequencyMinutes.Int32), + UserID: libraryIDStr, + Enabled: autoScanEnabled, + Frequency: scanFrequency, } - s.scanSettings[userID] = scanSetting + s.scanSettings[libraryIDStr] = scanSetting s.mu.Unlock() if !exists || currentSetting.Frequency != scanSetting.Frequency { - s.scheduleLibraryScan(ctx, library.ID, userID, scanSetting.Frequency) + s.scheduleLibraryScan(ctx, library.ID, libraryIDStr, scanSetting.Frequency) } } }