From 2f5da21a5b733838fc4f516012a856e517d9310a Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Mon, 6 Apr 2020 22:27:05 +0200 Subject: [PATCH] Sync: Code clean-up #225 Signed-off-by: Michael Mayer --- internal/commands/config.go | 2 +- internal/query/file_sync.go | 7 +++++-- internal/service/service.go | 2 +- internal/workers/sync.go | 16 ++++++++++------ internal/workers/workers.go | 2 +- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/internal/commands/config.go b/internal/commands/config.go index f5f0bef3f..8256856df 100644 --- a/internal/commands/config.go +++ b/internal/commands/config.go @@ -36,7 +36,7 @@ func configAction(ctx *cli.Context) error { fmt.Printf("public %t\n", conf.Public()) fmt.Printf("experimental %t\n", conf.Experimental()) fmt.Printf("workers %d\n", conf.Workers()) - fmt.Printf("wakeup-interval %d\n", conf.WakeupInterval() / time.Second) + fmt.Printf("wakeup-interval %d\n", conf.WakeupInterval()/time.Second) fmt.Printf("log-level %s\n", conf.LogLevel()) fmt.Printf("log-filename %s\n", conf.LogFilename()) fmt.Printf("pid-filename %s\n", conf.PIDFilename()) diff --git a/internal/query/file_sync.go b/internal/query/file_sync.go index 7a810f134..bc50ae906 100644 --- a/internal/query/file_sync.go +++ b/internal/query/file_sync.go @@ -8,7 +8,7 @@ import ( ) // FileSyncs returns up to 100 file syncs for a given account id and status. -func (q *Query) FileSyncs(accountId uint, status string) (result []entity.FileSync, err error) { +func (q *Query) FileSyncs(accountId uint, status string, limit int) (result []entity.FileSync, err error) { s := q.db.Where(&entity.FileSync{}) if accountId > 0 { @@ -20,7 +20,10 @@ func (q *Query) FileSyncs(accountId uint, status string) (result []entity.FileSy } s = s.Order("remote_name ASC") - s = s.Limit(1000).Offset(0) + + if limit > 0 { + s = s.Limit(limit).Offset(0) + } s = s.Preload("File") diff --git a/internal/service/service.go b/internal/service/service.go index 55b4b5cf8..1a0f244ca 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -18,7 +18,7 @@ var services struct { Classify *classify.TensorFlow } -func SetConfig(c *config.Config) { +func SetConfig(c *config.Config) { if c == nil { panic("config is nil") } diff --git a/internal/workers/sync.go b/internal/workers/sync.go index 1d703d5bc..5ef6f1676 100644 --- a/internal/workers/sync.go +++ b/internal/workers/sync.go @@ -184,19 +184,23 @@ func (s *Sync) getRemoteFiles(a entity.Account) (complete bool, err error) { func (s *Sync) relatedDownloads(a entity.Account) (result Downloads, err error) { result = make(Downloads) + maxResults := 1000 - files, err := s.q.FileSyncs(a.ID, entity.FileSyncNew) + // Get remote files from database + files, err := s.q.FileSyncs(a.ID, entity.FileSyncNew, maxResults) if err != nil { return result, err } + // Group results by directory and base name for i, file := range files { k := fs.AbsBase(file.RemoteName) result[k] = append(result[k], file) - if i > 990 { + // Skip last 50 to make sure we see all related files + if i > (maxResults - 50) { return result, nil } } @@ -314,11 +318,11 @@ func (s *Sync) download(a entity.Account) (complete bool, err error) { } else { log.Infof("sync: importing %s and related files", file.RemoteName) importJobs <- photoprism.ImportJob{ - FileName: mf.FileName(), - Related: related, - IndexOpt: photoprism.IndexOptionsAll(), + FileName: mf.FileName(), + Related: related, + IndexOpt: photoprism.IndexOptionsAll(), ImportOpt: photoprism.ImportOptionsMove(baseDir), - Imp: service.Import(), + Imp: service.Import(), } } } diff --git a/internal/workers/workers.go b/internal/workers/workers.go index 8761c1723..7416844fd 100644 --- a/internal/workers/workers.go +++ b/internal/workers/workers.go @@ -11,7 +11,7 @@ import ( var log = event.Log var stop = make(chan bool, 1) -// Start runs the service workers every 10 minutes. +// Start runs PhotoPrism background workers every wakeup interval. func Start(conf *config.Config) { ticker := time.NewTicker(conf.WakeupInterval())