🎨 数据同步前先判断网络连通性 Fix https://github.com/siyuan-note/siyuan/issues/7156

This commit is contained in:
Liang Ding 2023-01-24 15:51:37 +08:00
parent 1a00b75192
commit bdab8961b3
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 25 additions and 21 deletions

View file

@ -59,9 +59,7 @@ func AutoSync() {
func BootSyncData() { func BootSyncData() {
defer logging.Recover() defer logging.Recover()
if util.IsMutexLocked(&syncLock) { if !checkSync(true, false, false) {
logging.LogWarnf("sync is in progress")
planSyncAfter(30 * time.Second)
return return
} }
@ -70,27 +68,10 @@ func BootSyncData() {
util.IncBootProgress(3, "Syncing data from the cloud...") util.IncBootProgress(3, "Syncing data from the cloud...")
BootSyncSucc = 0 BootSyncSucc = 0
if !Conf.Sync.Enabled || !cloud.IsValidCloudDirName(Conf.Sync.CloudName) {
return
}
if !IsSubscriber() && conf.ProviderSiYuan == Conf.Sync.Provider {
return
}
logging.LogInfof("sync before boot") logging.LogInfof("sync before boot")
if 7 < syncDownloadErrCount {
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
}
now := util.CurrentTimeMillis() now := util.CurrentTimeMillis()
Conf.Sync.Synced = now Conf.Sync.Synced = now
util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil) util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil)
err := bootSyncRepo() err := bootSyncRepo()
synced := util.Millisecond2Time(Conf.Sync.Synced).Format("2006-01-02 15:04:05") + "\n\n" synced := util.Millisecond2Time(Conf.Sync.Synced).Format("2006-01-02 15:04:05") + "\n\n"
@ -102,7 +83,11 @@ func BootSyncData() {
msg := fmt.Sprintf(Conf.Language(82), synced) msg := fmt.Sprintf(Conf.Language(82), synced)
Conf.Sync.Stat = msg Conf.Sync.Stat = msg
Conf.Save() Conf.Save()
util.BroadcastByType("main", "syncing", 1, msg, nil) code := 1
if nil != err {
code = 2
}
util.BroadcastByType("main", "syncing", code, msg, nil)
return return
} }
@ -197,6 +182,11 @@ func checkSync(boot, exit, byHand bool) bool {
planSyncAfter(64 * time.Minute) planSyncAfter(64 * time.Minute)
return false return false
} }
if !util.IsOnline() {
util.BroadcastByType("main", "syncing", 2, Conf.Language(28), nil)
return false
}
return true return true
} }

View file

@ -17,15 +17,29 @@
package util package util
import ( import (
"github.com/imroc/req/v3"
"github.com/siyuan-note/httpclient" "github.com/siyuan-note/httpclient"
"net/http" "net/http"
"strings" "strings"
"time"
"github.com/88250/gulu" "github.com/88250/gulu"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/olahol/melody" "github.com/olahol/melody"
) )
func IsOnline() bool {
c := req.C().SetTimeout(1 * time.Second)
resp, err := c.R().Get("https://icanhazip.com")
if nil != err {
resp, err = c.R().Get("https://api.ipify.org")
if nil != err {
resp, err = c.R().Get("https://www.baidu.com")
}
}
return nil == err && nil != resp && 200 == resp.StatusCode
}
func GetRemoteAddr(session *melody.Session) string { func GetRemoteAddr(session *melody.Session) string {
ret := session.Request.Header.Get("X-forwarded-for") ret := session.Request.Header.Get("X-forwarded-for")
ret = strings.TrimSpace(ret) ret = strings.TrimSpace(ret)