2020-05-26 19:27:29 +02:00
|
|
|
package workers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"runtime"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2020-06-29 13:35:38 +02:00
|
|
|
// Meta represents a background metadata maintenance worker.
|
|
|
|
type Meta struct {
|
2020-05-26 19:27:29 +02:00
|
|
|
conf *config.Config
|
|
|
|
}
|
|
|
|
|
2020-06-29 13:35:38 +02:00
|
|
|
// NewMeta returns a new background metadata maintenance worker.
|
|
|
|
func NewMeta(conf *config.Config) *Meta {
|
|
|
|
return &Meta{conf: conf}
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// logError logs an error message if err is not nil.
|
2020-06-29 13:35:38 +02:00
|
|
|
func (worker *Meta) logError(err error) {
|
2020-05-26 19:27:29 +02:00
|
|
|
if err != nil {
|
2020-06-29 13:35:38 +02:00
|
|
|
log.Errorf("metadata: %s", err.Error())
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// logWarn logs a warning message if err is not nil.
|
2020-06-29 13:35:38 +02:00
|
|
|
func (worker *Meta) logWarn(err error) {
|
2020-05-26 19:27:29 +02:00
|
|
|
if err != nil {
|
2020-06-29 13:35:38 +02:00
|
|
|
log.Warnf("metadata: %s", err.Error())
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// originalsPath returns the original media files path as string.
|
2020-06-29 13:35:38 +02:00
|
|
|
func (worker *Meta) originalsPath() string {
|
2020-05-26 19:27:29 +02:00
|
|
|
return worker.conf.OriginalsPath()
|
|
|
|
}
|
|
|
|
|
2020-06-29 13:35:38 +02:00
|
|
|
// Start starts the metadata worker.
|
|
|
|
func (worker *Meta) Start() (err error) {
|
|
|
|
if err := mutex.MetaWorker.Start(); err != nil {
|
2020-05-26 19:27:29 +02:00
|
|
|
worker.logWarn(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2020-06-29 13:35:38 +02:00
|
|
|
mutex.MetaWorker.Stop()
|
2020-05-26 19:27:29 +02:00
|
|
|
|
|
|
|
if err := recover(); err != nil {
|
2020-06-29 13:35:38 +02:00
|
|
|
log.Errorf("metadata: %s (worker panic)", err)
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-06-29 13:16:55 +02:00
|
|
|
log.Debugf("metadata: starting routine check")
|
|
|
|
|
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
|
|
|
|
|
|
|
for {
|
2020-06-24 08:34:23 +02:00
|
|
|
photos, err := query.PhotosCheck(limit, offset)
|
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-06-29 13:16:55 +02: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-06-29 13:16:55 +02:00
|
|
|
if updated, err := photo.Optimize(); err != nil {
|
|
|
|
log.Errorf("metadata: %s", err)
|
|
|
|
} else if updated {
|
|
|
|
optimized++
|
|
|
|
log.Debugf("metadata: optimized photo %s", photo.String())
|
|
|
|
}
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 13:35:38 +02:00
|
|
|
if mutex.MetaWorker.Canceled() {
|
2020-06-29 13:16:55 +02: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 {
|
|
|
|
log.Infof("metadata: optimized %d photos", optimized)
|
2020-05-26 19:27:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
worker.logError(query.ResetPhotoQuality())
|
|
|
|
|
|
|
|
worker.logError(entity.UpdatePhotoCounts())
|
|
|
|
|
2020-05-30 14:52:47 +02:00
|
|
|
moments := photoprism.NewMoments(worker.conf)
|
|
|
|
|
|
|
|
if err := moments.Start(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:27:29 +02:00
|
|
|
runtime.GC()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|