Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:recycle: 重构同步检查逻辑

Liang Dingc099f3be2e29
diff --git a/kernel/model/sync.go b/kernel/model/sync.go
index aa67f59669..2702580f10 100644
--- a/kernel/model/sync.go
+++ b/kernel/model/sync.go
@@ -107,19 +107,18 @@ func BootSyncData() {
}
func SyncData(boot, exit, byHand bool) {
+ if !checkSync(boot, exit, byHand) {
+ return
+ }
+
+ util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil)
task.PrependTask(task.CloudSync, syncData, boot, exit, byHand)
}
func syncData(boot, exit, byHand bool) {
defer logging.Recover()
- if !boot && !exit && 2 == Conf.Sync.Mode && !byHand {
- return
- }
-
- if util.IsMutexLocked(&syncLock) {
- logging.LogWarnf("sync is in progress")
- planSyncAfter(30 * time.Second)
+ if !checkSync(boot, exit, byHand) {
return
}
@@ -129,64 +128,72 @@ func syncData(boot, exit, byHand bool) {
if boot {
util.IncBootProgress(3, "Syncing data from the cloud...")
BootSyncSucc = 0
+ logging.LogInfof("sync before boot")
}
if exit {
ExitSyncSucc = 0
+ logging.LogInfof("sync before exit")
+ util.PushMsg(Conf.Language(81), 1000*60*15)
+ }
+
+ now := util.CurrentTimeMillis()
+ Conf.Sync.Synced = now
+
+ util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil)
+ err := syncRepo(exit, byHand)
+ synced := util.Millisecond2Time(Conf.Sync.Synced).Format("2006-01-02 15:04:05") + "\n\n"
+ if nil == err {
+ synced += Conf.Sync.Stat
+ } else {
+ synced += fmt.Sprintf(Conf.Language(80), formatErrorMsg(err))
+ }
+ msg := fmt.Sprintf(Conf.Language(82), synced)
+ Conf.Sync.Stat = msg
+ Conf.Save()
+ util.BroadcastByType("main", "syncing", 1, msg, nil)
+ return
+}
+
+func checkSync(boot, exit, byHand bool) bool {
+ if !boot && !exit && 2 == Conf.Sync.Mode && !byHand {
+ return false
}
if !Conf.Sync.Enabled {
if byHand {
util.PushMsg(Conf.Language(124), 5000)
}
- return
+ return false
}
if !cloud.IsValidCloudDirName(Conf.Sync.CloudName) {
if byHand {
util.PushMsg(Conf.Language(123), 5000)
}
- return
+ return false
}
if !IsSubscriber() && conf.ProviderSiYuan == Conf.Sync.Provider {
- return
+ return false
}
if !cloud.IsValidCloudDirName(Conf.Sync.CloudName) {
- return
+ return false
}
- if boot {
- logging.LogInfof("sync before boot")
- }
- if exit {
- logging.LogInfof("sync before exit")
- util.PushMsg(Conf.Language(81), 1000*60*15)
+ if util.IsMutexLocked(&syncLock) {
+ logging.LogWarnf("sync is in progress")
+ planSyncAfter(30 * time.Second)
+ return false
}
if 7 < syncDownloadErrCount && !byHand {
logging.LogErrorf("sync download error too many times, cancel auto sync, try to sync by hand")
util.PushErrMsg(Conf.Language(125), 1000*60*60)
planSyncAfter(64 * time.Minute)
- return
+ return false
}
-
- now := util.CurrentTimeMillis()
- Conf.Sync.Synced = now
-
- util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil)
- err := syncRepo(exit, byHand)
- synced := util.Millisecond2Time(Conf.Sync.Synced).Format("2006-01-02 15:04:05") + "\n\n"
- if nil == err {
- synced += Conf.Sync.Stat
- } else {
- synced += fmt.Sprintf(Conf.Language(80), formatErrorMsg(err))
- }
- msg := fmt.Sprintf(Conf.Language(82), synced)
- Conf.Sync.Stat = msg
- Conf.Save()
- util.BroadcastByType("main", "syncing", 1, msg, nil)
- return
+ return true
}
// incReindex 增量重建索引。