2020-04-06 09:41:42 +02:00
|
|
|
package workers
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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-04 17:19:34 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-04-05 22:26:53 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/remote"
|
2020-04-03 18:08:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Sync represents a sync worker.
|
|
|
|
type Sync struct {
|
|
|
|
conf *config.Config
|
2020-04-06 22:09:45 +02:00
|
|
|
q *query.Query
|
2020-04-03 18:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSync returns a new service sync worker.
|
|
|
|
func NewSync(conf *config.Config) *Sync {
|
2020-04-06 22:09:45 +02:00
|
|
|
return &Sync{
|
|
|
|
conf: conf,
|
|
|
|
q: query.New(conf.Db()),
|
|
|
|
}
|
2020-04-03 18:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the sync worker.
|
2020-04-04 17:19:34 +02:00
|
|
|
func (s *Sync) Start() (err error) {
|
2020-04-03 18:08:49 +02:00
|
|
|
if err := mutex.Sync.Start(); err != nil {
|
2020-04-04 17:19:34 +02:00
|
|
|
event.Error(fmt.Sprintf("sync: %s", err.Error()))
|
2020-04-03 18:08:49 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer mutex.Sync.Stop()
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
f := form.AccountSearch{
|
|
|
|
Sync: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
db := s.conf.Db()
|
2020-04-06 22:09:45 +02:00
|
|
|
q := s.q
|
2020-04-06 16:34:29 +02:00
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
accounts, err := q.Accounts(f)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Warnf("sync: %s failed more than %d times", a.AccName, a.RetryLimit)
|
|
|
|
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-04-07 12:51:01 +02:00
|
|
|
if complete, err := s.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:
|
|
|
|
if complete, err := s.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:
|
|
|
|
if complete, err := s.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
|
|
|
}
|
|
|
|
|
|
|
|
if mutex.Sync.Canceled() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-20 08:47:26 +02:00
|
|
|
if err := db.First(&a, a.ID).Error; err != nil {
|
|
|
|
log.Errorf("sync: %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only update the following fields to avoid overwriting other settings
|
|
|
|
a.AccError = accError
|
|
|
|
a.AccErrors = accErrors
|
|
|
|
a.SyncStatus = syncStatus
|
|
|
|
a.SyncDate = syncDate
|
|
|
|
|
2020-04-04 17:19:34 +02:00
|
|
|
if err := db.Save(&a).Error; err != nil {
|
|
|
|
log.Errorf("sync: %s", err.Error())
|
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
|
|
|
|
}
|