mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 19:39:19 +00:00
Make TaskContext as context.Context. (#1232)
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user