Moments: Reduce activity of background workers #4237 #4243

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2024-05-14 16:04:42 +02:00
parent f90075e043
commit 7bec34468e
5 changed files with 24 additions and 20 deletions

View File

@@ -526,13 +526,11 @@ func (m *Album) UpdateTitleAndLocation(title, location, state, country, slug str
changed = true
}
if !changed && state == m.AlbumState && country == m.AlbumCountry {
if !changed && state == m.AlbumState && (country == m.AlbumCountry || country == "" && m.AlbumCountry == "zz") {
return nil
}
if title != "" {
m.SetTitle(title)
}
m.SetTitle(title)
// Skip location?
if location == "" && state == "" && (country == "" || country == "zz") {
@@ -646,6 +644,9 @@ func (m *Album) UpdateFolder(albumPath, albumFilter string) error {
if albumSlug == "" || albumPath == "" || albumFilter == "" || !m.HasID() {
return fmt.Errorf("folder album must have a path and filter")
} else if m.AlbumPath == albumPath && m.AlbumFilter == albumFilter && m.AlbumSlug == albumSlug {
// Nothing changed.
return nil
}
if err := m.Updates(map[string]interface{}{

View File

@@ -3,6 +3,7 @@ package mutex
import (
"errors"
"sync"
"time"
)
// Activity represents work that can be started and stopped.
@@ -10,6 +11,7 @@ type Activity struct {
busy bool
canceled bool
mutex sync.Mutex
lastRun time.Time
}
// Running checks if the Activity is currently running.
@@ -46,6 +48,7 @@ func (b *Activity) Stop() {
b.busy = false
b.canceled = false
b.lastRun = time.Now().UTC()
}
// Cancel requests to stop the Activity.
@@ -65,3 +68,11 @@ func (b *Activity) Canceled() bool {
return b.canceled
}
// LastRun returns the time of last activity.
func (b *Activity) LastRun() time.Time {
b.mutex.Lock()
defer b.mutex.Unlock()
return b.lastRun
}

View File

@@ -2,6 +2,7 @@ package mutex
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
@@ -9,15 +10,19 @@ import (
func TestActivity_Running(t *testing.T) {
b := Activity{}
assert.True(t, b.LastRun().IsZero())
assert.False(t, b.Running())
assert.False(t, b.Canceled())
assert.Nil(t, b.Start())
assert.True(t, b.Running())
assert.False(t, b.Canceled())
assert.True(t, b.LastRun().IsZero())
b.Cancel()
assert.True(t, b.Canceled())
assert.True(t, b.Running())
assert.True(t, b.LastRun().IsZero())
b.Stop()
assert.Less(t, time.Now().UTC().Sub(b.LastRun()), time.Second)
assert.False(t, b.Canceled())
assert.False(t, b.Running())
}

View File

@@ -9,15 +9,13 @@ import (
"github.com/dustin/go-humanize/english"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/internal/photoprism"
)
// Backup represents a background backup worker.
type Backup struct {
conf *config.Config
lastRun time.Time
conf *config.Config
}
// NewBackup returns a new Backup worker.
@@ -80,9 +78,6 @@ func (w *Backup) Start(database, albums bool, force bool, retain int) (err error
}
}
// Remember time when worker was executed.
w.lastRun = entity.TimeStamp()
elapsed := time.Since(start)
// Log success message.

View File

@@ -3,7 +3,6 @@ package workers
import (
"errors"
"fmt"
"runtime"
"runtime/debug"
"time"
@@ -18,8 +17,7 @@ import (
// Meta represents a background index and metadata optimization worker.
type Meta struct {
conf *config.Config
lastRun time.Time
conf *config.Config
}
// NewMeta returns a new Meta worker.
@@ -48,7 +46,7 @@ func (w *Meta) Start(delay, interval time.Duration, force bool) (err error) {
defer mutex.MetaWorker.Stop()
// Check time when worker was last executed.
updateIndex := force || w.lastRun.Before(time.Now().Add(-1*entity.IndexUpdateInterval))
updateIndex := force || mutex.MetaWorker.LastRun().Before(time.Now().Add(-1*entity.IndexUpdateInterval))
// Run faces worker if needed.
if updateIndex || entity.UpdateFaces.Load() {
@@ -144,11 +142,5 @@ func (w *Meta) Start(delay, interval time.Duration, force bool) (err error) {
}
}
// Update time when worker was last executed.
w.lastRun = entity.TimeStamp()
// Run garbage collection.
runtime.GC()
return nil
}