diff --git a/api/scanner/scanner_album.go b/api/scanner/scanner_album.go index 708abfc3..efa293e5 100644 --- a/api/scanner/scanner_album.go +++ b/api/scanner/scanner_album.go @@ -102,7 +102,7 @@ func ScanAlbum(ctx scanner_task.TaskContext) error { mediaData := media_encoding.NewEncodeMediaData(media) if err := scanMedia(ctx, media, &mediaData, i, len(albumMedia)); err != nil { - scanner_utils.ScannerError("Error scanning media for album (%d) file (%s): %s\n", ctx.GetAlbum().ID, media.Path, err) + scanner_utils.ScannerError(ctx, "Error scanning media for album (%d) file (%s): %s\n", ctx.GetAlbum().ID, media.Path, err) } } @@ -160,7 +160,7 @@ func findMediaForAlbum(ctx scanner_task.TaskContext) ([]*models.Media, error) { }) if err != nil { - scanner_utils.ScannerError("Error scanning media for album (%d): %s\n", ctx.GetAlbum().ID, err) + scanner_utils.ScannerError(ctx, "Error scanning media for album (%d): %s\n", ctx.GetAlbum().ID, err) continue } } diff --git a/api/scanner/scanner_cache/cache.go b/api/scanner/scanner_cache/cache.go index d4078646..c06768a6 100644 --- a/api/scanner/scanner_cache/cache.go +++ b/api/scanner/scanner_cache/cache.go @@ -110,7 +110,7 @@ func (c *AlbumScannerCache) InsertAlbumIgnore(path string, ignoreData []string) func (c *AlbumScannerCache) IsPathMedia(mediaPath string) bool { mediaType, err := c.GetMediaType(mediaPath) if err != nil { - scanner_utils.ScannerError("IsPathMedia (%s): %s", mediaPath, err) + scanner_utils.ScannerError(nil, "IsPathMedia (%s): %s", mediaPath, err) return false } diff --git a/api/scanner/scanner_queue/queue.go b/api/scanner/scanner_queue/queue.go index 52437574..a24e8fdd 100644 --- a/api/scanner/scanner_queue/queue.go +++ b/api/scanner/scanner_queue/queue.go @@ -36,7 +36,7 @@ func NewScannerJob(ctx scanner_task.TaskContext) ScannerJob { func (job *ScannerJob) Run(db *gorm.DB) { err := scanner.ScanAlbum(job.ctx) if err != nil { - scanner_utils.ScannerError("Failed to scan album: %v", err) + scanner_utils.ScannerError(nil, "Failed to scan album: %v", err) } } @@ -181,7 +181,7 @@ func (queue *ScannerQueue) processQueue(notifyThrottle *utils.Throttle) { }) if err := scanner.GenerateBlurhashes(queue.db); err != nil { - scanner_utils.ScannerError("Failed to generate blurhashes: %v", err) + scanner_utils.ScannerError(nil, "Failed to generate blurhashes: %v", err) } notification.BroadcastNotification(&models.Notification{ diff --git a/api/scanner/scanner_task/scanner_task.go b/api/scanner/scanner_task/scanner_task.go index 110238a7..cfff88f6 100644 --- a/api/scanner/scanner_task/scanner_task.go +++ b/api/scanner/scanner_task/scanner_task.go @@ -36,11 +36,11 @@ type ScannerTask interface { } type TaskContext struct { - ctx context.Context + context.Context } func NewTaskContext(parent context.Context, db *gorm.DB, album *models.Album, cache *scanner_cache.AlbumScannerCache) TaskContext { - ctx := TaskContext{ctx: parent} + ctx := TaskContext{Context: parent} ctx = ctx.WithValue(taskCtxKeyAlbum, album) ctx = ctx.WithValue(taskCtxKeyAlbumCache, cache) ctx = ctx.WithDB(db) @@ -57,15 +57,15 @@ const ( ) func (c TaskContext) GetAlbum() *models.Album { - return c.ctx.Value(taskCtxKeyAlbum).(*models.Album) + return c.Context.Value(taskCtxKeyAlbum).(*models.Album) } func (c TaskContext) GetCache() *scanner_cache.AlbumScannerCache { - return c.ctx.Value(taskCtxKeyAlbumCache).(*scanner_cache.AlbumScannerCache) + return c.Context.Value(taskCtxKeyAlbumCache).(*scanner_cache.AlbumScannerCache) } func (c TaskContext) GetDB() *gorm.DB { - return c.ctx.Value(taskCtxKeyDatabase).(*gorm.DB) + return c.Context.Value(taskCtxKeyDatabase).(*gorm.DB) } func (c TaskContext) DatabaseTransaction(transFunc func(ctx TaskContext) error, opts ...*sql.TxOptions) error { @@ -76,27 +76,15 @@ func (c TaskContext) DatabaseTransaction(transFunc func(ctx TaskContext) error, func (c TaskContext) WithValue(key, val interface{}) TaskContext { return TaskContext{ - ctx: context.WithValue(c.ctx, key, val), + Context: context.WithValue(c.Context, key, val), } } -func (c TaskContext) Value(key interface{}) interface{} { - return c.ctx.Value(key) -} - func (c TaskContext) WithDB(db *gorm.DB) TaskContext { // Allow db to be nil in tests if db == nil && flag.Lookup("test.v") != nil { return c } - return c.WithValue(taskCtxKeyDatabase, db.WithContext(c.ctx)) -} - -func (c TaskContext) Done() <-chan struct{} { - return c.ctx.Done() -} - -func (c TaskContext) Err() error { - return c.ctx.Err() + return c.WithValue(taskCtxKeyDatabase, db.WithContext(c.Context)) } diff --git a/api/scanner/scanner_tasks/cleanup_tasks/cleanup_media.go b/api/scanner/scanner_tasks/cleanup_tasks/cleanup_media.go index fd53decb..a021d57c 100644 --- a/api/scanner/scanner_tasks/cleanup_tasks/cleanup_media.go +++ b/api/scanner/scanner_tasks/cleanup_tasks/cleanup_media.go @@ -121,7 +121,7 @@ func DeleteOldUserAlbums(db *gorm.DB, scannedAlbums []*models.Album, user *model }) if err != nil { - scanner_utils.ScannerError("Could not delete old albums from database:\n%s\n", err) + scanner_utils.ScannerError(nil, "Could not delete old albums from database:\n%s\n", err) deleteErrors = append(deleteErrors, err) } diff --git a/api/scanner/scanner_tasks/cleanup_tasks/media_cleanup_task.go b/api/scanner/scanner_tasks/cleanup_tasks/media_cleanup_task.go index 84177adf..74afb224 100644 --- a/api/scanner/scanner_tasks/cleanup_tasks/media_cleanup_task.go +++ b/api/scanner/scanner_tasks/cleanup_tasks/media_cleanup_task.go @@ -15,7 +15,7 @@ func (t MediaCleanupTask) AfterScanAlbum(ctx scanner_task.TaskContext, changedMe cleanupErrors := CleanupMedia(ctx.GetDB(), ctx.GetAlbum().ID, albumMedia) for _, err := range cleanupErrors { - scanner_utils.ScannerError("delete old media: %s", err) + scanner_utils.ScannerError(ctx, "delete old media: %s", err) } return nil diff --git a/api/scanner/scanner_tasks/face_detection_task.go b/api/scanner/scanner_tasks/face_detection_task.go index b24c8625..bca5f0f4 100644 --- a/api/scanner/scanner_tasks/face_detection_task.go +++ b/api/scanner/scanner_tasks/face_detection_task.go @@ -23,7 +23,7 @@ func (t FaceDetectionTask) AfterProcessMedia(ctx scanner_task.TaskContext, media return } if err := face_detection.GlobalFaceDetector.DetectFaces(ctx.GetDB(), media); err != nil { - scanner_utils.ScannerError("Error detecting faces in image (%s): %s", media.Path, err) + scanner_utils.ScannerError(ctx, "Error detecting faces in image (%s): %s", media.Path, err) } }(mediaData.Media) } diff --git a/api/scanner/scanner_user.go b/api/scanner/scanner_user.go index ef3cd8dc..9a3aa4fb 100644 --- a/api/scanner/scanner_user.go +++ b/api/scanner/scanner_user.go @@ -254,7 +254,7 @@ func directoryContainsPhotos(rootPath string, cache *scanner_cache.AlbumScannerC dirContent, err := os.ReadDir(dirPath) if err != nil { - scanner_utils.ScannerError("Could not read directory (%s): %s\n", dirPath, err.Error()) + scanner_utils.ScannerError(nil, "Could not read directory (%s): %s\n", dirPath, err.Error()) return false } diff --git a/api/scanner/scanner_utils/scanner_error.go b/api/scanner/scanner_utils/scanner_error.go index 45b21810..196ba4a1 100644 --- a/api/scanner/scanner_utils/scanner_error.go +++ b/api/scanner/scanner_utils/scanner_error.go @@ -1,18 +1,19 @@ package scanner_utils import ( + "context" "fmt" - "log" "github.com/photoview/photoview/api/graphql/models" "github.com/photoview/photoview/api/graphql/notification" + "github.com/photoview/photoview/api/log" "github.com/photoview/photoview/api/utils" ) -func ScannerError(format string, args ...interface{}) { +func ScannerError(ctx context.Context, format string, args ...any) { message := fmt.Sprintf(format, args...) - log.Printf("ERROR: %s", message) + log.Error(ctx, message) notification.BroadcastNotification(&models.Notification{ Key: utils.GenerateToken(), Type: models.NotificationTypeMessage,