Non-fork face detection, avoiding a race condition. (#1243)

* Non-fork face detection, avoiding a race condition.

* Update scanner test.
This commit is contained in:
Googol Lee
2025-07-11 11:16:41 +02:00
committed by GitHub
parent 882c6abc84
commit 06d2e05ae4
2 changed files with 14 additions and 39 deletions

View File

@@ -18,14 +18,13 @@ func (t FaceDetectionTask) AfterProcessMedia(ctx scanner_task.TaskContext, media
didProcess := len(updatedURLs) > 0
if didProcess && mediaData.Media.Type == models.MediaTypePhoto {
go func(media *models.Media) {
if face_detection.GlobalFaceDetector == nil {
return
}
if err := face_detection.GlobalFaceDetector.DetectFaces(ctx.GetDB(), media); err != nil {
scanner_utils.ScannerError(ctx, "Error detecting faces in image (%s): %s", media.Path, err)
}
}(mediaData.Media)
media := mediaData.Media
if face_detection.GlobalFaceDetector == nil {
return nil
}
if err := face_detection.GlobalFaceDetector.DetectFaces(ctx.GetDB(), media); err != nil {
scanner_utils.ScannerError(ctx, "Error detecting faces in image (%s): %s", media.Path, err)
}
}
return nil

View File

@@ -1,14 +1,12 @@
package scanner_test
import (
"context"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/photoview/photoview/api/graphql/models"
@@ -178,18 +176,14 @@ func TestFullScan(t *testing.T) {
})
t.Run("CheckFaceGroup", func(t *testing.T) {
ctx, done := context.WithTimeout(t.Context(), time.Second*5)
defer done()
var allFaceGroups []*models.FaceGroup
if err := db.Find(&allFaceGroups).Error; err != nil {
t.Fatal("get face groups error:", err)
}
waitFor(ctx, t, time.Second/2, func() bool {
var allFaceGroups []*models.FaceGroup
if err := db.Find(&allFaceGroups).Error; err != nil {
t.Fatal("get face groups error:", err)
return false
}
return len(allFaceGroups) == len(wantFaceGroups)
})
if got, want := len(allFaceGroups), len(wantFaceGroups); got != want {
t.Errorf("len(allFaceGroups) = %d, want: %d", got, want)
}
})
t.Run("CheckFaces", func(t *testing.T) {
@@ -231,24 +225,6 @@ func equalNameWithoutSuffix(a, b string) bool {
return true
}
func waitFor(ctx context.Context, t *testing.T, interval time.Duration, checkFn func() bool) {
t.Helper()
ticker := time.NewTicker(interval)
for {
select {
case <-ctx.Done():
t.Fatal("check timeout")
return
case <-ticker.C:
}
if checkFn() {
return
}
}
}
func groupMediaWithFaces(medias []*models.ImageFace) [][]string {
grouped := make(map[int][]string)