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.
|
|
|
|
func (m *Meta) Start(delay time.Duration) (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
|
|
|
|
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 {
|
2020-12-11 22:09:11 +01:00
|
|
|
photos, err := query.PhotosCheck(limit, offset, delay)
|
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
|
|
|
|
|
2020-12-30 11:11:58 +01:00
|
|
|
updated, merged, err := photo.Optimize(settings.StackMeta(), settings.StackUUID(), settings.Features.Estimates)
|
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++
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Debugf("metadata: optimized 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 {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Infof("metadata: optimized %d photos", optimized)
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
|
2021-08-14 14:24:48 +02:00
|
|
|
// Explicitly set quality of photos without primary file to -1.
|
2020-07-13 10:41:45 +02:00
|
|
|
if err := query.ResetPhotoQuality(); err != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Warnf("metadata: %s (reset photo 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
|
|
|
// Update photo counts for labels and places.
|
2020-07-13 10:41:45 +02:00
|
|
|
if err := entity.UpdatePhotoCounts(); err != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Warnf("metadata: %s (update photo counts)", 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 {
|
|
|
|
log.Errorf("moments: failed creating worker")
|
|
|
|
} else if err := w.Start(); err != nil {
|
2020-07-18 13:21:43 +02:00
|
|
|
log.Warnf("moments: %s", err)
|
2020-05-30 14:52:47 +02:00
|
|
|
}
|
|
|
|
|
2021-08-14 14:24:48 +02:00
|
|
|
// Run faces worker.
|
|
|
|
if w := photoprism.NewFaces(m.conf); w == nil {
|
|
|
|
log.Errorf("faces: failed creating worker")
|
|
|
|
} else if w.Disabled() {
|
|
|
|
// Do nothing.
|
2021-08-16 00:29:36 +02:00
|
|
|
} else if err := w.Start(photoprism.FacesOptions{}); err != nil {
|
2021-08-14 14:24:48 +02:00
|
|
|
log.Warnf("faces: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run garbage collection.
|
2020-05-26 19:27:29 +02:00
|
|
|
runtime.GC()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|