🎨 云端同步模式支持 完全手动同步 模式 https://github.com/siyuan-note/siyuan/issues/7295

This commit is contained in:
Liang Ding 2023-02-08 17:45:47 +08:00
parent dbce98d811
commit 7098a2c8b1
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
7 changed files with 271 additions and 272 deletions

View file

@ -772,6 +772,137 @@ func IsSyncingFile(rootID string) (ret bool) {
return
}
func syncRepoDownload() (err error) {
if 1 > len(Conf.Repo.Key) {
planSyncAfter(fixSyncInterval)
msg := Conf.Language(26)
util.PushStatusBar(msg)
util.PushErrMsg(msg, 0)
err = errors.New(msg)
return
}
repo, err := newRepository()
if nil != err {
planSyncAfter(fixSyncInterval)
msg := fmt.Sprintf("sync repo failed: %s", err)
logging.LogErrorf(msg)
util.PushStatusBar(msg)
util.PushErrMsg(msg, 0)
return
}
start := time.Now()
err = indexRepoBeforeCloudSync(repo)
if nil != err {
planSyncAfter(fixSyncInterval)
return
}
syncContext := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBar}
mergeResult, trafficStat, err := repo.SyncDownload(syncContext)
if errors.Is(err, dejavu.ErrRepoFatalErr) {
// 重置仓库并再次尝试同步
if _, resetErr := resetRepository(repo); nil == resetErr {
mergeResult, trafficStat, err = repo.SyncDownload(syncContext)
}
}
elapsed := time.Since(start)
if nil != err {
planSyncAfter(fixSyncInterval)
logging.LogErrorf("sync data repo download failed: %s", err)
msg := fmt.Sprintf(Conf.Language(80), formatErrorMsg(err))
if errors.Is(err, dejavu.ErrCloudStorageSizeExceeded) {
msg = fmt.Sprintf(Conf.Language(43), humanize.Bytes(uint64(Conf.User.UserSiYuanRepoSize)))
if 2 == Conf.User.UserSiYuanSubscriptionPlan {
msg = fmt.Sprintf(Conf.Language(68), humanize.Bytes(uint64(Conf.User.UserSiYuanRepoSize)))
}
}
Conf.Sync.Stat = msg
util.PushStatusBar(msg)
util.PushErrMsg(msg, 0)
return
}
util.PushStatusBar(fmt.Sprintf(Conf.Language(149), elapsed.Seconds()))
Conf.Sync.Synced = util.CurrentTimeMillis()
msg := fmt.Sprintf(Conf.Language(150), trafficStat.UploadFileCount, trafficStat.DownloadFileCount, trafficStat.UploadChunkCount, trafficStat.DownloadChunkCount, humanize.Bytes(uint64(trafficStat.UploadBytes)), humanize.Bytes(uint64(trafficStat.DownloadBytes)))
Conf.Sync.Stat = msg
syncDownloadErrCount = 0
logging.LogInfof("synced data repo download [provider=%d, ufc=%d, dfc=%d, ucc=%d, dcc=%d, ub=%s, db=%s] in [%.2fs]",
Conf.Sync.Provider, trafficStat.UploadFileCount, trafficStat.DownloadFileCount, trafficStat.UploadChunkCount, trafficStat.DownloadChunkCount, humanize.Bytes(uint64(trafficStat.UploadBytes)), humanize.Bytes(uint64(trafficStat.DownloadBytes)), elapsed.Seconds())
processSyncMergeResult(false, true, start, mergeResult)
return
}
func syncRepoUpload() (err error) {
if 1 > len(Conf.Repo.Key) {
planSyncAfter(fixSyncInterval)
msg := Conf.Language(26)
util.PushStatusBar(msg)
util.PushErrMsg(msg, 0)
err = errors.New(msg)
return
}
repo, err := newRepository()
if nil != err {
planSyncAfter(fixSyncInterval)
msg := fmt.Sprintf("sync repo failed: %s", err)
logging.LogErrorf(msg)
util.PushStatusBar(msg)
util.PushErrMsg(msg, 0)
return
}
start := time.Now()
err = indexRepoBeforeCloudSync(repo)
if nil != err {
planSyncAfter(fixSyncInterval)
return
}
syncContext := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBar}
trafficStat, err := repo.SyncUpload(syncContext)
if errors.Is(err, dejavu.ErrRepoFatalErr) {
// 重置仓库并再次尝试同步
if _, resetErr := resetRepository(repo); nil == resetErr {
trafficStat, err = repo.SyncUpload(syncContext)
}
}
elapsed := time.Since(start)
if nil != err {
planSyncAfter(fixSyncInterval)
logging.LogErrorf("sync data repo upload failed: %s", err)
msg := fmt.Sprintf(Conf.Language(80), formatErrorMsg(err))
if errors.Is(err, dejavu.ErrCloudStorageSizeExceeded) {
msg = fmt.Sprintf(Conf.Language(43), humanize.Bytes(uint64(Conf.User.UserSiYuanRepoSize)))
if 2 == Conf.User.UserSiYuanSubscriptionPlan {
msg = fmt.Sprintf(Conf.Language(68), humanize.Bytes(uint64(Conf.User.UserSiYuanRepoSize)))
}
}
Conf.Sync.Stat = msg
util.PushStatusBar(msg)
util.PushErrMsg(msg, 0)
return
}
util.PushStatusBar(fmt.Sprintf(Conf.Language(149), elapsed.Seconds()))
Conf.Sync.Synced = util.CurrentTimeMillis()
msg := fmt.Sprintf(Conf.Language(150), trafficStat.UploadFileCount, trafficStat.DownloadFileCount, trafficStat.UploadChunkCount, trafficStat.DownloadChunkCount, humanize.Bytes(uint64(trafficStat.UploadBytes)), humanize.Bytes(uint64(trafficStat.DownloadBytes)))
Conf.Sync.Stat = msg
logging.LogInfof("synced data repo upload [provider=%d, ufc=%d, dfc=%d, ucc=%d, dcc=%d, ub=%s, db=%s] in [%.2fs]",
Conf.Sync.Provider, trafficStat.UploadFileCount, trafficStat.DownloadFileCount, trafficStat.UploadChunkCount, trafficStat.DownloadChunkCount, humanize.Bytes(uint64(trafficStat.UploadBytes)), humanize.Bytes(uint64(trafficStat.DownloadBytes)), elapsed.Seconds())
return
}
func bootSyncRepo() (err error) {
if 1 > len(Conf.Repo.Key) {
syncDownloadErrCount++
@ -933,6 +1064,11 @@ func syncRepo(exit, byHand bool) (err error) {
logging.LogInfof("synced data repo [provider=%d, ufc=%d, dfc=%d, ucc=%d, dcc=%d, ub=%s, db=%s] in [%.2fs]",
Conf.Sync.Provider, trafficStat.UploadFileCount, trafficStat.DownloadFileCount, trafficStat.UploadChunkCount, trafficStat.DownloadChunkCount, humanize.Bytes(uint64(trafficStat.UploadBytes)), humanize.Bytes(uint64(trafficStat.DownloadBytes)), elapsed.Seconds())
processSyncMergeResult(exit, byHand, start, mergeResult)
return
}
func processSyncMergeResult(exit, byHand bool, start time.Time, mergeResult *dejavu.MergeResult) {
logSyncMergeResult(mergeResult)
if 0 < len(mergeResult.Conflicts) && Conf.Sync.GenerateConflictDoc {
@ -1028,14 +1164,13 @@ func syncRepo(exit, byHand bool) (err error) {
ReloadUI()
}
elapsed = time.Since(start)
elapsed := time.Since(start)
if !exit {
go func() {
time.Sleep(2 * time.Second)
util.PushStatusBar(fmt.Sprintf(Conf.Language(149), elapsed.Seconds()))
}()
}
return
}
func logSyncMergeResult(mergeResult *dejavu.MergeResult) {

View file

@ -37,6 +37,79 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func SyncDataDownload() {
defer logging.Recover()
if !checkSync(false, false, true) {
return
}
util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil)
if !util.IsOnline() { // 这个操作比较耗时,所以要先推送 syncing 事件后再判断网络,这样才能给用户更即时的反馈
util.BroadcastByType("main", "syncing", 2, Conf.Language(28), nil)
return
}
syncLock.Lock()
defer syncLock.Unlock()
now := util.CurrentTimeMillis()
Conf.Sync.Synced = now
err := syncRepoDownload()
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()
code := 1
if nil != err {
code = 2
}
util.BroadcastByType("main", "syncing", code, msg, nil)
}
func SyncDataUpload() {
defer logging.Recover()
if !checkSync(false, false, true) {
return
}
util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil)
if !util.IsOnline() { // 这个操作比较耗时,所以要先推送 syncing 事件后再判断网络,这样才能给用户更即时的反馈
util.BroadcastByType("main", "syncing", 2, Conf.Language(28), nil)
return
}
syncLock.Lock()
defer syncLock.Unlock()
now := util.CurrentTimeMillis()
Conf.Sync.Synced = now
err := syncRepoUpload()
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()
code := 1
if nil != err {
code = 2
}
util.BroadcastByType("main", "syncing", code, msg, nil)
return
}
var (
syncSameCount = 0
syncDownloadErrCount = 0
@ -147,7 +220,11 @@ func syncData(boot, exit, byHand bool) {
}
func checkSync(boot, exit, byHand bool) bool {
if !boot && !exit && 2 == Conf.Sync.Mode && !byHand {
if 2 == Conf.Sync.Mode && !boot && !exit && !byHand { // 手动模式下只有启动和退出进行同步
return false
}
if 3 == Conf.Sync.Mode && !byHand { // 完全手动模式下只有手动进行同步
return false
}