refactor: improve worker type safety and scanner reliability

Worker improvements:
- Add strongly-typed result structs for all job types
- Replace map[string]interface{} with specific result types
- Add JSON tags to JobResult for proper API serialization
- Fix processJob to handle different result types correctly
- Improve directory scan job with proper library folder resolution
- Add debug logging for scan operations

Media scanner improvements:
- Add nil checks for database in GetPollInterval and GetAutoScanEnabled
- Fix pdfcpu API call signature (add validateOnly parameter)
- Add debug logging for scanDirectory with file counters
- Improve error handling and reporting

Test fixes:
- Fix default poll interval expectation from 30s to 60s
- Add settingsCache initialization to scanner tests
- Add folders initialization to ProcessDirtyDirectories test
This commit is contained in:
2026-03-06 01:52:42 -05:00
parent 2ac42a8d91
commit bb0158e8fb
4 changed files with 156 additions and 43 deletions
+17 -2
View File
@@ -128,6 +128,10 @@ func (s *MediaScanner) GetPollInterval() time.Duration {
}
}
if s.db == nil {
return 60 * time.Second
}
// Cache miss - query database
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
@@ -154,6 +158,10 @@ func (s *MediaScanner) GetAutoScanEnabled() bool {
return strings.ToLower(cached) == "true"
}
if s.db == nil {
return true
}
// Cache miss - query database
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
@@ -1088,7 +1096,7 @@ func (s *MediaScanner) extractPDFMetadata(path string) (*MediaMetadata, error) {
// Use pdfcpu API to read PDF metadata
// Configuration: nil = default (lenient mode)
pdfInfo, err := pdfcpuapi.PDFInfo(f, filepath.Base(path), nil, nil)
pdfInfo, err := pdfcpuapi.PDFInfo(f, filepath.Base(path), nil, false, nil)
if err != nil {
fmt.Printf("Warning: failed to read PDF info from %s: %v\n", path, err)
// Fall back to filename as title
@@ -1863,6 +1871,9 @@ func (s *MediaScanner) waitForFileStability(filePath string) bool {
}
func (s *MediaScanner) scanDirectory(ctx context.Context, dirPath string) {
// Debug: Track file processing
filesSeen := 0
filesProcessed := 0
// Prevent concurrent scans of ANY directory
// Simple mutex is enough - job queue already serializes by directory
s.scan_mutex.Lock()
@@ -1903,7 +1914,7 @@ func (s *MediaScanner) scanDirectory(ctx context.Context, dirPath string) {
if !s.waitForFileStability(path) {
return nil
}
filesSeen++
relPath := strings.TrimPrefix(path, rootFolder+"/")
_, err = s.db.GetMediaItemByFilePath(ctx, database.GetMediaItemByFilePathParams{
FilePath: relPath,
@@ -1917,10 +1928,14 @@ func (s *MediaScanner) scanDirectory(ctx context.Context, dirPath string) {
s.newItems++
}
s.totalFiles++
filesProcessed++
}
return nil
})
// Debug: Report scan results
fmt.Printf("DEBUG: scanDirectory of %s - filesSeen: %d, filesProcessed: %d, totalFiles: %d, newItems: %d, errors: %d\n",
dirPath, filesSeen, filesProcessed, s.totalFiles, s.newItems, s.errors)
}
// performInitialScan scans all root folders on startup