Skip to content

Commit

Permalink
Moments: Reduce activity of background workers #4237 #4243
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Mayer <[email protected]>
  • Loading branch information
lastzero committed May 14, 2024
1 parent f90075e commit 7bec344
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
9 changes: 5 additions & 4 deletions internal/entity/album.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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{}{
Expand Down
11 changes: 11 additions & 0 deletions internal/mutex/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package mutex
import (
"errors"
"sync"
"time"
)

// Activity represents work that can be started and stopped.
type Activity struct {
busy bool
canceled bool
mutex sync.Mutex
lastRun time.Time
}

// Running checks if the Activity is currently running.
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
5 changes: 5 additions & 0 deletions internal/mutex/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@ package mutex

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

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())
}
Expand Down
7 changes: 1 addition & 6 deletions internal/workers/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 2 additions & 10 deletions internal/workers/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package workers
import (
"errors"
"fmt"
"runtime"
"runtime/debug"
"time"

Expand All @@ -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.
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}

0 comments on commit 7bec344

Please sign in to comment.