2020-05-26 19:27:29 +02:00
|
|
|
package workers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-07-13 10:41:45 +02:00
|
|
|
"fmt"
|
2020-05-26 19:27:29 +02:00
|
|
|
"runtime"
|
2020-07-13 10:41:45 +02:00
|
|
|
"runtime/debug"
|
2020-05-26 19:27:29 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2020-05-30 14:52:47 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
2020-05-26 19:27:29 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
|
|
|
)
|
|
|
|
|
2021-08-14 14:24:48 +02:00
|
|
|
// Meta represents a background metadata optimization worker.
|
2020-06-29 13:35:38 +02:00
|
|
|
type Meta struct {
|
2020-05-26 19:27:29 +02:00
|
|
|
conf *config.Config
|
|
|
|
}
|
|
|
|
|
2021-08-14 14:24:48 +02:00
|
|
|
// NewMeta returns a new Meta worker.
|
2020-06-29 13:35:38 +02:00
|
|
|
func NewMeta(conf *config.Config) *Meta {
|
|
|
|
return &Meta{conf: conf}
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// originalsPath returns the original media files path as string.
|
2021-08-14 14:24:48 +02:00
|
|
|
func (m *Meta) originalsPath() string {
|
|
|
|
return m.conf.OriginalsPath()
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
|
2021-08-14 14:24:48 +02:00
|
|
|
// Start metadata optimization routine.
|
2021-11-20 19:14:00 +01:00
|
|
|
func (m *Meta) Start(delay, interval time.Duration, force bool) (err error) {
|
2020-07-13 10:41:45 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
err = fmt.Errorf("metadata: %s (panic)\nstack: %s", r, debug.Stack())
|
2020-07-13 10:41:45 +02:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-06-29 13:35:38 +02:00
|
|
|
if err := mutex.MetaWorker.Start(); err != nil {
|
2020-05-26 19:27:29 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-13 10:41:45 +02:00
|
|
|
defer mutex.MetaWorker.Stop()
|
2020-05-26 19:27:29 +02:00
|
|
|
|
2021-09-29 20:09:34 +02:00
|
|
|
log.Debugf("metadata: running facial recognition")
|
|
|
|
|
|
|
|
// Run faces worker.
|
|
|
|
if w := photoprism.NewFaces(m.conf); w.Disabled() {
|
|
|
|
log.Debugf("metadata: skipping facial recognition")
|
|
|
|
} else if err := w.Start(photoprism.FacesOptions{}); err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
}
|
|
|
|
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Debugf("metadata: starting routine check")
|
2020-06-29 13:16:55 +02:00
|
|
|
|
2021-08-14 14:24:48 +02:00
|
|
|
settings := m.conf.Settings()
|
2020-05-26 19:27:29 +02:00
|
|
|
done := make(map[string]bool)
|
|
|
|
|
|
|
|
limit := 50
|
|
|
|
offset := 0
|
2020-06-29 13:16:55 +02:00
|
|
|
optimized := 0
|
2020-05-26 19:27:29 +02:00
|
|
|
|
2021-08-14 14:24:48 +02:00
|
|
|
// Run index optimization.
|
2020-05-26 19:27:29 +02:00
|
|
|
for {
|
2021-11-20 19:14:00 +01:00
|
|
|
photos, err := query.PhotosMetadataUpdate(limit, offset, delay, interval)
|
2020-05-26 19:27:29 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(photos) == 0 {
|
|
|
|
break
|
|
|
|
} else if offset == 0 {
|
2020-06-29 13:16:55 +02:00
|
|
|
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, photo := range photos {
|
2020-06-29 13:35:38 +02:00
|
|
|
if mutex.MetaWorker.Canceled() {
|
2020-12-05 00:13:44 +01:00
|
|
|
return errors.New("metadata: check canceled")
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if done[photo.PhotoUID] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
done[photo.PhotoUID] = true
|
|
|
|
|
2021-11-18 03:47:25 +01:00
|
|
|
updated, merged, err := photo.Optimize(settings.StackMeta(), settings.StackUUID(), settings.Features.Estimates, force)
|
2020-12-09 22:15:10 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Errorf("metadata: %s (optimize photo)", err)
|
2020-06-29 13:16:55 +02:00
|
|
|
} else if updated {
|
|
|
|
optimized++
|
2021-11-25 14:52:26 +01:00
|
|
|
log.Debugf("metadata: updated photo %s", photo.String())
|
2020-06-29 13:16:55 +02:00
|
|
|
}
|
2020-12-09 22:15:10 +01:00
|
|
|
|
2021-08-14 14:24:48 +02:00
|
|
|
for _, p := range merged {
|
|
|
|
log.Infof("metadata: merged %s", p.PhotoUID)
|
|
|
|
done[p.PhotoUID] = true
|
2020-12-09 22:15:10 +01:00
|
|
|
}
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 13:35:38 +02:00
|
|
|
if mutex.MetaWorker.Canceled() {
|
2020-12-05 00:13:44 +01:00
|
|
|
return errors.New("metadata: check canceled")
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
offset += limit
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2020-06-29 13:16:55 +02:00
|
|
|
if optimized > 0 {
|
2021-11-25 14:52:26 +01:00
|
|
|
log.Infof("metadata: updated %d photos", optimized)
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
|
2021-09-30 15:50:10 +02:00
|
|
|
// Set photo quality scores to -1 if files are missing.
|
2021-10-06 11:50:48 +02:00
|
|
|
if err := query.FlagHiddenPhotos(); err != nil {
|
2021-08-30 18:58:27 +02:00
|
|
|
log.Warnf("metadata: %s (reset quality)", err.Error())
|
2020-07-13 10:41:45 +02:00
|
|
|
}
|
2020-05-26 19:27:29 +02:00
|
|
|
|
2021-08-14 14:24:48 +02:00
|
|
|
// Run moments worker.
|
|
|
|
if w := photoprism.NewMoments(m.conf); w == nil {
|
2021-08-30 18:58:27 +02:00
|
|
|
log.Errorf("metadata: failed updating moments")
|
2021-08-14 14:24:48 +02:00
|
|
|
} else if err := w.Start(); err != nil {
|
2021-08-30 18:58:27 +02:00
|
|
|
log.Warn(err)
|
2020-05-30 14:52:47 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 16:06:59 +02:00
|
|
|
// Update precalculated photo and file counts.
|
2021-10-02 14:24:44 +02:00
|
|
|
if err := entity.UpdateCounts(); err != nil {
|
|
|
|
log.Warnf("index: %s (update counts)", err.Error())
|
2021-09-23 16:06:59 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// Update album, subject, and label cover thumbs.
|
|
|
|
if err := query.UpdateCovers(); err != nil {
|
2021-10-02 14:24:44 +02:00
|
|
|
log.Warnf("index: %s (update covers)", err)
|
2021-08-14 14:24:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run garbage collection.
|
2020-05-26 19:27:29 +02:00
|
|
|
runtime.GC()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|