♻️ 简化数据同步计划任务实现 Fix https://github.com/siyuan-note/siyuan/issues/5329

This commit is contained in:
Liang Ding 2022-07-02 19:36:17 +08:00
parent 9e1220faa7
commit 6aab504b7b
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 29 additions and 17 deletions

View file

@ -21,6 +21,7 @@ import (
"encoding/base64"
"errors"
"fmt"
"math"
"os"
"time"
@ -291,13 +292,14 @@ func indexRepoBeforeCloudSync() {
}
}
func syncRepo() (err error) {
func syncRepo(byHand bool) {
if 1 > len(Conf.Repo.Key) {
return
}
repo, err := newRepository()
if nil != err {
util.LogErrorf("sync repo failed: %s", err)
return
}
@ -320,6 +322,17 @@ func syncRepo() (err error) {
util.PushStatusBar(fmt.Sprintf(Conf.Language(149)+" [%s]", elapsed.Seconds(), latest.ID[:7]))
if 1 > len(mergeUpserts) && 1 > len(mergeRemoves) {
// 没有数据变更,直接返回
syncSameCount++
if 10 < syncSameCount {
syncSameCount = 5
}
if !byHand {
after := time.Minute * time.Duration(int(math.Pow(2, float64(syncSameCount))))
if fixSyncInterval.Minutes() > after.Minutes() {
after = time.Minute * 8
}
planSyncAfter(after)
}
return
}

View file

@ -48,8 +48,7 @@ var (
syncSameCount = 0
syncDownloadErrCount = 0
fixSyncInterval = 5 * time.Minute
syncInterval = fixSyncInterval
syncPlanTime = time.Now().Add(syncInterval)
syncPlanTime = time.Now().Add(fixSyncInterval)
BootSyncSucc = -1 // -1未执行0执行成功1执行失败
ExitSyncSucc = -1
@ -60,7 +59,6 @@ func AutoSync() {
time.Sleep(5 * time.Second)
if time.Now().After(syncPlanTime) {
SyncData(false, false, false)
syncPlanTime = time.Now().Add(syncInterval)
}
}
}
@ -74,7 +72,7 @@ func SyncData(boot, exit, byHand bool) {
if util.IsMutexLocked(&syncLock) {
util.LogWarnf("sync has been locked")
syncInterval = 30 * time.Second
planSyncAfter(30 * time.Second)
return
}
@ -113,7 +111,7 @@ func SyncData(boot, exit, byHand bool) {
if 7 < syncDownloadErrCount && !byHand {
util.LogErrorf("sync download error too many times, cancel auto sync, try to sync by hand")
util.PushErrMsg(Conf.Language(125), 1000*60*60)
syncInterval = 64 * time.Minute
planSyncAfter(64 * time.Minute)
return
}
@ -137,9 +135,7 @@ func SyncData(boot, exit, byHand bool) {
indexRepoBeforeCloudSync()
// 同步数据仓库 https://github.com/siyuan-note/siyuan/issues/5142
if syncRepoErr := syncRepo(); nil != syncRepoErr {
util.LogErrorf("sync repo failed: %s", syncRepoErr)
}
syncRepo(byHand)
return // TODO: 测试
@ -201,11 +197,11 @@ func SyncData(boot, exit, byHand bool) {
syncSameCount = 5
}
if !byHand {
syncInterval = time.Minute * time.Duration(int(math.Pow(2, float64(syncSameCount))))
if fixSyncInterval.Minutes() > syncInterval.Minutes() {
syncInterval = time.Minute * 8
after := time.Minute * time.Duration(int(math.Pow(2, float64(syncSameCount))))
if fixSyncInterval.Minutes() > after.Minutes() {
after = time.Minute * 8
}
util.LogInfof("set sync interval to [%dm]", int(syncInterval.Minutes()))
planSyncAfter(after)
}
Conf.Sync.Stat = Conf.Language(133)
@ -284,7 +280,7 @@ func SyncData(boot, exit, byHand bool) {
BootSyncSucc = 0
ExitSyncSucc = 0
if !byHand {
syncInterval = fixSyncInterval
planSyncAfter(fixSyncInterval)
}
return
}
@ -399,7 +395,7 @@ func SyncData(boot, exit, byHand bool) {
BootSyncSucc = 0
ExitSyncSucc = 0
if !byHand {
syncInterval = fixSyncInterval
planSyncAfter(fixSyncInterval)
}
if boot && gulu.File.IsExist(util.BlockTreePath) {
@ -1337,8 +1333,7 @@ func GetSyncDirection(cloudDirName string) (code int, msg string) { // 0
func IncWorkspaceDataVer() {
filesys.IncWorkspaceDataVer(true, Conf.System.ID)
syncSameCount = 0
syncInterval = fixSyncInterval
syncPlanTime = time.Now().Add(30 * time.Second)
planSyncAfter(30 * time.Second)
}
func stableCopy(src, dest string) (err error) {
@ -1372,3 +1367,7 @@ func stableCopy(src, dest string) (err error) {
}
return gulu.File.Copy(src, dest)
}
func planSyncAfter(d time.Duration) {
syncPlanTime = time.Now().Add(d)
}