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.
|
2022-10-03 22:59:29 +02:00
|
|
|
func (w *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.
|
2022-10-03 22:59:29 +02:00
|
|
|
func (w *Sync) logWarn(err error) {
|
2020-05-26 15:15:14 +02:00
|
|
|
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.
|
2022-10-03 22:59:29 +02:00
|
|
|
func (w *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
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
f := form.SearchServices{
|
2020-04-04 17:19:34 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-31 13:25:02 +01:00
|
|
|
// Failed too often?
|
2022-03-27 21:37:11 +02:00
|
|
|
if a.RetryLimit > 0 && a.AccErrors > a.RetryLimit {
|
2020-05-19 19:55:20 +02:00
|
|
|
a.AccSync = false
|
|
|
|
|
|
|
|
if err := entity.Db().Save(&a).Error; err != nil {
|
2022-10-03 22:59:29 +02:00
|
|
|
w.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 {
|
2022-10-15 21:54:11 +02:00
|
|
|
case entity.SyncStatusRefresh:
|
2022-10-03 22:59:29 +02:00
|
|
|
if complete, err := w.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 {
|
2022-10-15 21:54:11 +02:00
|
|
|
syncStatus = entity.SyncStatusDownload
|
2020-04-04 17:19:34 +02:00
|
|
|
} else if a.SyncUpload {
|
2022-10-15 21:54:11 +02:00
|
|
|
syncStatus = entity.SyncStatusUpload
|
2020-04-04 17:19:34 +02:00
|
|
|
} else {
|
2022-10-15 21:54:11 +02:00
|
|
|
syncStatus = entity.SyncStatusSynced
|
2020-04-20 08:47:26 +02:00
|
|
|
syncDate.Time = time.Now()
|
|
|
|
syncDate.Valid = true
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-15 21:54:11 +02:00
|
|
|
case entity.SyncStatusDownload:
|
2022-10-03 22:59:29 +02:00
|
|
|
if complete, err := w.download(a); err != nil {
|
2020-04-20 08:47:26 +02:00
|
|
|
accErrors++
|
|
|
|
accError = err.Error()
|
2022-10-15 21:54:11 +02:00
|
|
|
syncStatus = entity.SyncStatusRefresh
|
2020-04-04 17:19:34 +02:00
|
|
|
} else if complete {
|
2020-04-06 16:34:29 +02:00
|
|
|
if a.SyncUpload {
|
2022-10-15 21:54:11 +02:00
|
|
|
syncStatus = entity.SyncStatusUpload
|
2020-04-06 16:34:29 +02:00
|
|
|
} else {
|
2020-04-20 08:47:26 +02:00
|
|
|
synced = true
|
2022-10-15 21:54:11 +02:00
|
|
|
syncStatus = entity.SyncStatusSynced
|
2020-04-20 08:47:26 +02:00
|
|
|
syncDate.Time = time.Now()
|
|
|
|
syncDate.Valid = true
|
2020-04-06 16:34:29 +02:00
|
|
|
}
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
2022-10-15 21:54:11 +02:00
|
|
|
case entity.SyncStatusUpload:
|
2022-10-03 22:59:29 +02:00
|
|
|
if complete, err := w.upload(a); err != nil {
|
2020-04-20 08:47:26 +02:00
|
|
|
accErrors++
|
|
|
|
accError = err.Error()
|
2022-10-15 21:54:11 +02:00
|
|
|
syncStatus = entity.SyncStatusRefresh
|
2020-04-04 17:19:34 +02:00
|
|
|
} else if complete {
|
2020-04-20 08:47:26 +02:00
|
|
|
synced = true
|
2022-10-15 21:54:11 +02:00
|
|
|
syncStatus = entity.SyncStatusSynced
|
2020-04-20 08:47:26 +02:00
|
|
|
syncDate.Time = time.Now()
|
|
|
|
syncDate.Valid = true
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
2022-10-15 21:54:11 +02:00
|
|
|
case entity.SyncStatusSynced:
|
2020-04-04 17:19:34 +02:00
|
|
|
if a.SyncDate.Valid && a.SyncDate.Time.Before(time.Now().Add(time.Duration(-1*a.SyncInterval)*time.Second)) {
|
2022-10-15 21:54:11 +02:00
|
|
|
syncStatus = entity.SyncStatusRefresh
|
2020-04-04 17:19:34 +02:00
|
|
|
}
|
|
|
|
default:
|
2022-10-15 21:54:11 +02:00
|
|
|
syncStatus = entity.SyncStatusRefresh
|
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 {
|
2022-10-03 22:59:29 +02:00
|
|
|
w.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
|
|
|
|
}
|