2020-04-06 09:41:42 +02:00
|
|
|
package workers
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-07-13 10:41:45 +02:00
|
|
|
"runtime/debug"
|
2020-04-04 17:19:34 +02:00
|
|
|
"time"
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-04-04 17:19:34 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-04-03 18:08:49 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-04-04 17:19:34 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-04-03 18:08:49 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2020-04-05 22:26:53 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/remote"
|
2021-09-18 15:32:39 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/search"
|
2020-04-03 18:08:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Sync represents a sync worker.
|
|
|
|
type Sync struct {
|
|
|
|
conf *config.Config
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:15:14 +02:00
|
|
|
// NewSync returns a new sync worker.
|
2020-04-03 18:08:49 +02:00
|
|
|
func NewSync(conf *config.Config) *Sync {
|
2020-04-06 22:09:45 +02:00
|
|
|
return &Sync{
|
|
|
|
conf: conf,
|
|
|
|
}
|
2020-04-03 18:08:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 15:15:14 +02:00
|
|
|
// logError logs an error message if err is not nil.
|
|
|
|
func (worker *Sync) logError(err error) {
|
2020-05-26 11:00:39 +02:00
|
|
|
if err != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Errorf("sync: %s", err.Error())
|
2020-05-26 11:00:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:15:14 +02:00
|
|
|
// logWarn logs a warning message if err is not nil.
|
|
|
|
func (worker *Sync) logWarn(err error) {
|
|
|
|
if err != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Warnf("sync: %s", err.Error())
|
2020-05-26 15:15:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:08:49 +02:00
|
|
|
// Start starts the sync worker.
|
2020-05-26 15:15:14 +02:00
|
|
|
func (worker *Sync) Start() (err error) {
|
2020-07-13 10:41:45 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
err = fmt.Errorf("sync: %s (panic)\nstack: %s", r, debug.Stack())
|
2020-07-13 10:41:45 +02:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-05-26 15:15:14 +02:00
|
|
|
if err := mutex.SyncWorker.Start(); err != nil {
|
2020-04-03 18:08:49 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:15:14 +02:00
|
|
|
defer mutex.SyncWorker.Stop()
|
2020-04-03 18:08:49 +02:00
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
f := form.AccountSearch{
|
|
|
|
Sync: true,
|
|
|
|
}
|
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
accounts, err := search.Accounts(f)
|
2020-04-04 17:19:34 +02:00
|
|
|
|
|
|
|
for _, a := range accounts {
|
2020-04-05 22:26:53 +02:00
|
|
|
if a.AccType != remote.ServiceWebDAV {
|
2020-04-04 17:19:34 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.AccErrors > a.RetryLimit {
|
2020-05-19 19:55:20 +02:00
|
|
|
a.AccErrors = 0
|
|
|
|
a.AccSync = false
|
|
|
|
|
|
|
|
if err := entity.Db().Save(&a).Error; err != nil {
|
2020-05-26 15:15:14 +02:00
|
|
|
worker.logError(err)
|
2020-05-19 19:55:20 +02:00
|
|
|
} else {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Warnf("sync: disabled sync, %s failed more than %d times", a.AccName, a.RetryLimit)
|
2020-05-19 19:55:20 +02:00
|
|
|
}
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-20 08:47:26 +02:00
|
|
|
// Values updated in account: AccError, AccErrors, SyncStatus, SyncDate
|
|
|
|
accError := a.AccError
|
|
|
|
accErrors := a.AccErrors
|
|
|
|
syncStatus := a.SyncStatus
|
|
|
|
syncDate := a.SyncDate
|
|
|
|
synced := false
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
switch a.SyncStatus {
|
|
|
|
case entity.AccountSyncStatusRefresh:
|
2020-05-26 15:15:14 +02:00
|
|
|
if complete, err := worker.refresh(a); err != nil {
|
2020-04-20 08:47:26 +02:00
|
|
|
accErrors++
|
|
|
|
accError = err.Error()
|
2020-04-04 17:19:34 +02:00
|
|
|
} else if complete {
|
2020-04-20 08:47:26 +02:00
|
|
|
accErrors = 0
|
|
|
|
accError = ""
|
2020-04-04 17:19:34 +02:00
|
|
|
|
|
|
|
if a.SyncDownload {
|
2020-04-20 08:47:26 +02:00
|
|
|
syncStatus = entity.AccountSyncStatusDownload
|
2020-04-04 17:19:34 +02:00
|
|
|
} else if a.SyncUpload {
|
2020-04-20 08:47:26 +02:00
|
|
|
syncStatus = entity.AccountSyncStatusUpload
|
2020-04-04 17:19:34 +02:00
|
|
|
} else {
|
2020-04-20 08:47:26 +02:00
|
|
|
syncStatus = entity.AccountSyncStatusSynced
|
|
|
|
syncDate.Time = time.Now()
|
|
|
|
syncDate.Valid = true
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case entity.AccountSyncStatusDownload:
|
2020-05-26 15:15:14 +02:00
|
|
|
if complete, err := worker.download(a); err != nil {
|
2020-04-20 08:47:26 +02:00
|
|
|
accErrors++
|
|
|
|
accError = err.Error()
|
2020-04-04 17:19:34 +02:00
|
|
|
} else if complete {
|
2020-04-06 16:34:29 +02:00
|
|
|
if a.SyncUpload {
|
2020-04-20 08:47:26 +02:00
|
|
|
syncStatus = entity.AccountSyncStatusUpload
|
2020-04-06 16:34:29 +02:00
|
|
|
} else {
|
2020-04-20 08:47:26 +02:00
|
|
|
synced = true
|
|
|
|
syncStatus = entity.AccountSyncStatusSynced
|
|
|
|
syncDate.Time = time.Now()
|
|
|
|
syncDate.Valid = true
|
2020-04-06 16:34:29 +02:00
|
|
|
}
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
case entity.AccountSyncStatusUpload:
|
2020-05-26 15:15:14 +02:00
|
|
|
if complete, err := worker.upload(a); err != nil {
|
2020-04-20 08:47:26 +02:00
|
|
|
accErrors++
|
|
|
|
accError = err.Error()
|
2020-04-04 17:19:34 +02:00
|
|
|
} else if complete {
|
2020-04-20 08:47:26 +02:00
|
|
|
synced = true
|
|
|
|
syncStatus = entity.AccountSyncStatusSynced
|
|
|
|
syncDate.Time = time.Now()
|
|
|
|
syncDate.Valid = true
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
case entity.AccountSyncStatusSynced:
|
|
|
|
if a.SyncDate.Valid && a.SyncDate.Time.Before(time.Now().Add(time.Duration(-1*a.SyncInterval)*time.Second)) {
|
2020-04-20 08:47:26 +02:00
|
|
|
syncStatus = entity.AccountSyncStatusRefresh
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
default:
|
2020-04-20 08:47:26 +02:00
|
|
|
syncStatus = entity.AccountSyncStatusRefresh
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 15:15:14 +02:00
|
|
|
if mutex.SyncWorker.Canceled() {
|
2020-04-04 17:19:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-20 08:47:26 +02:00
|
|
|
// Only update the following fields to avoid overwriting other settings
|
2020-05-26 11:00:39 +02:00
|
|
|
if err := a.Updates(map[string]interface{}{
|
2020-05-26 15:15:14 +02:00
|
|
|
"AccError": accError,
|
|
|
|
"AccErrors": accErrors,
|
2020-05-26 11:00:39 +02:00
|
|
|
"SyncStatus": syncStatus,
|
2020-05-26 15:15:14 +02:00
|
|
|
"SyncDate": syncDate}); err != nil {
|
|
|
|
worker.logError(err)
|
2020-04-20 08:47:26 +02:00
|
|
|
} else if synced {
|
|
|
|
event.Publish("sync.synced", event.Data{"account": a})
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|