mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
♻️ 简化数据同步计划任务实现 Fix https://github.com/siyuan-note/siyuan/issues/5329
This commit is contained in:
parent
9e1220faa7
commit
6aab504b7b
2 changed files with 29 additions and 17 deletions
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -291,13 +292,14 @@ func indexRepoBeforeCloudSync() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func syncRepo() (err error) {
|
func syncRepo(byHand bool) {
|
||||||
if 1 > len(Conf.Repo.Key) {
|
if 1 > len(Conf.Repo.Key) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
repo, err := newRepository()
|
repo, err := newRepository()
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
util.LogErrorf("sync repo failed: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -320,6 +322,17 @@ func syncRepo() (err error) {
|
||||||
util.PushStatusBar(fmt.Sprintf(Conf.Language(149)+" [%s]", elapsed.Seconds(), latest.ID[:7]))
|
util.PushStatusBar(fmt.Sprintf(Conf.Language(149)+" [%s]", elapsed.Seconds(), latest.ID[:7]))
|
||||||
if 1 > len(mergeUpserts) && 1 > len(mergeRemoves) {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,7 @@ var (
|
||||||
syncSameCount = 0
|
syncSameCount = 0
|
||||||
syncDownloadErrCount = 0
|
syncDownloadErrCount = 0
|
||||||
fixSyncInterval = 5 * time.Minute
|
fixSyncInterval = 5 * time.Minute
|
||||||
syncInterval = fixSyncInterval
|
syncPlanTime = time.Now().Add(fixSyncInterval)
|
||||||
syncPlanTime = time.Now().Add(syncInterval)
|
|
||||||
|
|
||||||
BootSyncSucc = -1 // -1:未执行,0:执行成功,1:执行失败
|
BootSyncSucc = -1 // -1:未执行,0:执行成功,1:执行失败
|
||||||
ExitSyncSucc = -1
|
ExitSyncSucc = -1
|
||||||
|
|
@ -60,7 +59,6 @@ func AutoSync() {
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
if time.Now().After(syncPlanTime) {
|
if time.Now().After(syncPlanTime) {
|
||||||
SyncData(false, false, false)
|
SyncData(false, false, false)
|
||||||
syncPlanTime = time.Now().Add(syncInterval)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -74,7 +72,7 @@ func SyncData(boot, exit, byHand bool) {
|
||||||
|
|
||||||
if util.IsMutexLocked(&syncLock) {
|
if util.IsMutexLocked(&syncLock) {
|
||||||
util.LogWarnf("sync has been locked")
|
util.LogWarnf("sync has been locked")
|
||||||
syncInterval = 30 * time.Second
|
planSyncAfter(30 * time.Second)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -113,7 +111,7 @@ func SyncData(boot, exit, byHand bool) {
|
||||||
if 7 < syncDownloadErrCount && !byHand {
|
if 7 < syncDownloadErrCount && !byHand {
|
||||||
util.LogErrorf("sync download error too many times, cancel auto sync, try to sync by hand")
|
util.LogErrorf("sync download error too many times, cancel auto sync, try to sync by hand")
|
||||||
util.PushErrMsg(Conf.Language(125), 1000*60*60)
|
util.PushErrMsg(Conf.Language(125), 1000*60*60)
|
||||||
syncInterval = 64 * time.Minute
|
planSyncAfter(64 * time.Minute)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,9 +135,7 @@ func SyncData(boot, exit, byHand bool) {
|
||||||
indexRepoBeforeCloudSync()
|
indexRepoBeforeCloudSync()
|
||||||
|
|
||||||
// 同步数据仓库 https://github.com/siyuan-note/siyuan/issues/5142
|
// 同步数据仓库 https://github.com/siyuan-note/siyuan/issues/5142
|
||||||
if syncRepoErr := syncRepo(); nil != syncRepoErr {
|
syncRepo(byHand)
|
||||||
util.LogErrorf("sync repo failed: %s", syncRepoErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return // TODO: 测试
|
return // TODO: 测试
|
||||||
|
|
||||||
|
|
@ -201,11 +197,11 @@ func SyncData(boot, exit, byHand bool) {
|
||||||
syncSameCount = 5
|
syncSameCount = 5
|
||||||
}
|
}
|
||||||
if !byHand {
|
if !byHand {
|
||||||
syncInterval = time.Minute * time.Duration(int(math.Pow(2, float64(syncSameCount))))
|
after := time.Minute * time.Duration(int(math.Pow(2, float64(syncSameCount))))
|
||||||
if fixSyncInterval.Minutes() > syncInterval.Minutes() {
|
if fixSyncInterval.Minutes() > after.Minutes() {
|
||||||
syncInterval = time.Minute * 8
|
after = time.Minute * 8
|
||||||
}
|
}
|
||||||
util.LogInfof("set sync interval to [%dm]", int(syncInterval.Minutes()))
|
planSyncAfter(after)
|
||||||
}
|
}
|
||||||
|
|
||||||
Conf.Sync.Stat = Conf.Language(133)
|
Conf.Sync.Stat = Conf.Language(133)
|
||||||
|
|
@ -284,7 +280,7 @@ func SyncData(boot, exit, byHand bool) {
|
||||||
BootSyncSucc = 0
|
BootSyncSucc = 0
|
||||||
ExitSyncSucc = 0
|
ExitSyncSucc = 0
|
||||||
if !byHand {
|
if !byHand {
|
||||||
syncInterval = fixSyncInterval
|
planSyncAfter(fixSyncInterval)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -399,7 +395,7 @@ func SyncData(boot, exit, byHand bool) {
|
||||||
BootSyncSucc = 0
|
BootSyncSucc = 0
|
||||||
ExitSyncSucc = 0
|
ExitSyncSucc = 0
|
||||||
if !byHand {
|
if !byHand {
|
||||||
syncInterval = fixSyncInterval
|
planSyncAfter(fixSyncInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
if boot && gulu.File.IsExist(util.BlockTreePath) {
|
if boot && gulu.File.IsExist(util.BlockTreePath) {
|
||||||
|
|
@ -1337,8 +1333,7 @@ func GetSyncDirection(cloudDirName string) (code int, msg string) { // 0:失
|
||||||
func IncWorkspaceDataVer() {
|
func IncWorkspaceDataVer() {
|
||||||
filesys.IncWorkspaceDataVer(true, Conf.System.ID)
|
filesys.IncWorkspaceDataVer(true, Conf.System.ID)
|
||||||
syncSameCount = 0
|
syncSameCount = 0
|
||||||
syncInterval = fixSyncInterval
|
planSyncAfter(30 * time.Second)
|
||||||
syncPlanTime = time.Now().Add(30 * time.Second)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func stableCopy(src, dest string) (err error) {
|
func stableCopy(src, dest string) (err error) {
|
||||||
|
|
@ -1372,3 +1367,7 @@ func stableCopy(src, dest string) (err error) {
|
||||||
}
|
}
|
||||||
return gulu.File.Copy(src, dest)
|
return gulu.File.Copy(src, dest)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func planSyncAfter(d time.Duration) {
|
||||||
|
syncPlanTime = time.Now().Add(d)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue