Backend: Use goroutines image format conversion

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-01-19 12:50:44 +01:00
parent 492a9839ff
commit aafeda0919
6 changed files with 45 additions and 49 deletions

View file

@ -11,6 +11,7 @@ import (
_ "github.com/jinzhu/gorm/dialects/sqlite"
gc "github.com/patrickmn/go-cache"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/internal/thumb"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
@ -182,8 +183,10 @@ func (c *Config) Init(ctx context.Context) error {
return c.connectToDatabase(ctx)
}
// Shutdown closes open database connections.
// Shutdown services and workers.
func (c *Config) Shutdown() {
mutex.Worker.Cancel()
if err := c.CloseDb(); err != nil {
log.Errorf("could not close database connection: %s", err)
} else {

View file

@ -4,7 +4,6 @@ import (
"strings"
"testing"
"github.com/photoprism/photoprism/internal/maps/osm"
"github.com/photoprism/photoprism/internal/maps/places"
"github.com/photoprism/photoprism/pkg/s2"
"github.com/stretchr/testify/assert"
@ -27,23 +26,6 @@ func TestLocation_QueryPlaces(t *testing.T) {
})
}
func TestLocation_QueryOSM(t *testing.T) {
t.Run("BerlinFernsehturm", func(t *testing.T) {
lat := 52.5208
lng := 13.40953
id := s2.Token(lat, lng)
l := NewLocation(id)
if err := l.QueryOSM(); err != nil {
t.Fatal(err)
}
assert.Equal(t, "Fernsehturm Berlin", l.LocName)
assert.Equal(t, "Berlin, Germany", l.LocLabel)
})
}
func TestLocation_Assign(t *testing.T) {
t.Run("BerlinFernsehturm", func(t *testing.T) {
lat := 52.5208
@ -75,18 +57,15 @@ func TestLocation_Assign(t *testing.T) {
lng := -118.49700833333334
id := s2.Token(lat, lng)
o, err := osm.FindLocation(id)
o, err := places.FindLocation(id)
if err != nil {
t.Fatal(err)
}
assert.False(t, o.Cached)
assert.Equal(t, "Santa Monica Pier", o.LocName)
assert.Equal(t, "90401", o.Address.Postcode)
assert.Equal(t, "California", o.Address.State)
assert.Equal(t, "us", o.Address.CountryCode)
assert.Equal(t, "United States of America", o.Address.Country)
assert.Equal(t, "California", o.State())
assert.Equal(t, "us", o.CountryCode())
var l Location
@ -94,7 +73,6 @@ func TestLocation_Assign(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, "Santa Monica Pier", l.LocName)
assert.Equal(t, "Santa Monica, California, USA", l.LocLabel)
})
@ -103,20 +81,12 @@ func TestLocation_Assign(t *testing.T) {
lng := 8.557494444444446
id := s2.Token(lat, lng)
o, err := osm.FindLocation(id)
o, err := places.FindLocation(id)
if err != nil {
t.Fatal(err)
}
assert.False(t, o.Cached)
assert.Equal(t, "Dock A", o.LocName)
assert.Equal(t, "8302", o.Address.Postcode)
assert.Equal(t, "Zurich", o.Address.State)
assert.Equal(t, "ch", o.Address.CountryCode)
assert.Equal(t, "Switzerland", o.Address.Country)
var l Location
if err := l.Assign(o); err != nil {
@ -132,20 +102,12 @@ func TestLocation_Assign(t *testing.T) {
lng := 13.28895092010498
id := s2.Token(lat, lng)
o, err := osm.FindLocation(id)
o, err := places.FindLocation(id)
if err != nil {
t.Fatal(err)
}
assert.False(t, o.Cached)
assert.Equal(t, "TGL", o.LocName)
assert.Equal(t, "13405", o.Address.Postcode)
assert.Equal(t, "Berlin", o.Address.State)
assert.Equal(t, "de", o.Address.CountryCode)
assert.Equal(t, "Germany", o.Address.Country)
var l Location
if err := l.Assign(o); err != nil {

View file

@ -35,6 +35,19 @@ func (c *Convert) Start(path string) error {
defer mutex.Worker.Stop()
jobs := make(chan ConvertJob)
// Start a fixed number of goroutines to convert files.
var wg sync.WaitGroup
var numWorkers = c.conf.Workers()
wg.Add(numWorkers)
for i := 0; i < numWorkers; i++ {
go func() {
convertWorker(jobs)
wg.Done()
}()
}
err := filepath.Walk(path, func(filename string, fileInfo os.FileInfo, err error) error {
defer func() {
if err := recover(); err != nil {
@ -60,13 +73,17 @@ func (c *Convert) Start(path string) error {
return nil
}
if _, err := c.ToJpeg(mf); err != nil {
log.Warnf("convert: %s (%s)", err.Error(), filename)
jobs <- ConvertJob{
image: mf,
convert: c,
}
return nil
})
close(jobs)
wg.Wait()
return err
}

View file

@ -0,0 +1,14 @@
package photoprism
type ConvertJob struct {
image *MediaFile
convert *Convert
}
func convertWorker(jobs <-chan ConvertJob) {
for job := range jobs {
if _, err := job.convert.ToJpeg(job.image); err != nil {
log.Warnf("convert: %s (%s)", err.Error(), job.image.Filename())
}
}
}

View file

@ -64,13 +64,13 @@ func (imp *Import) Start(importPath string) {
jobs := make(chan ImportJob)
// Start a fixed number of goroutines to read and digest files.
// Start a fixed number of goroutines to import files.
var wg sync.WaitGroup
var numWorkers = ind.conf.Workers()
wg.Add(numWorkers)
for i := 0; i < numWorkers; i++ {
go func() {
importWorker(jobs) // HLc
importWorker(jobs)
wg.Done()
}()
}

View file

@ -68,7 +68,7 @@ func (ind *Index) Start(options IndexOptions) map[string]bool {
jobs := make(chan IndexJob)
// Start a fixed number of goroutines to read and digest files.
// Start a fixed number of goroutines to index files.
var wg sync.WaitGroup
var numWorkers = ind.conf.Workers()
wg.Add(numWorkers)