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
|
|
|
|
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 {
|
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-09-30 15:50:10 +02:00
|
|
|
// Set photo quality scores to -1 if files are missing.
|
2020-07-13 10:41:45 +02:00
|
|
|
if err := query.ResetPhotoQuality(); 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-10-01 00:05:49 +02:00
|
|
|
log.Debugf("updating photo counts")
|
2021-09-23 16:06:59 +02:00
|
|
|
|
|
|
|
// Update precalculated photo and file counts.
|
|
|
|
if err := entity.UpdatePhotoCounts(); err != nil {
|
|
|
|
log.Warnf("metadata: %s (update counts)", err.Error())
|
|
|
|
}
|
|
|
|
|
2021-10-01 00:05:49 +02:00
|
|
|
log.Debugf("updating preview thumbs")
|
2021-08-30 18:58:27 +02:00
|
|
|
|
2021-09-23 16:06:59 +02:00
|
|
|
// Update album, subject, and label preview thumbs.
|
2021-08-30 18:58:27 +02:00
|
|
|
if err := query.UpdatePreviews(); err != nil {
|
|
|
|
log.Errorf("metadata: %s (update previews)", err)
|
2021-08-14 14:24:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run garbage collection.
|
2020-05-26 19:27:29 +02:00
|
|
|
runtime.GC()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|