From bdab8961b3b35d981f70ce9b2a73da324e185fc0 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 24 Jan 2023 15:51:37 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=95=B0=E6=8D=AE=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E5=89=8D=E5=85=88=E5=88=A4=E6=96=AD=E7=BD=91=E7=BB=9C=E8=BF=9E?= =?UTF-8?q?=E9=80=9A=E6=80=A7=20Fix=20https://github.com/siyuan-note/siyua?= =?UTF-8?q?n/issues/7156?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/sync.go | 32 +++++++++++--------------------- kernel/util/net.go | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/kernel/model/sync.go b/kernel/model/sync.go index 54b47c358..037e2e7fd 100644 --- a/kernel/model/sync.go +++ b/kernel/model/sync.go @@ -59,9 +59,7 @@ func AutoSync() { func BootSyncData() { defer logging.Recover() - if util.IsMutexLocked(&syncLock) { - logging.LogWarnf("sync is in progress") - planSyncAfter(30 * time.Second) + if !checkSync(true, false, false) { return } @@ -70,27 +68,10 @@ func BootSyncData() { util.IncBootProgress(3, "Syncing data from the cloud...") 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") - 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() Conf.Sync.Synced = now - util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil) err := bootSyncRepo() 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) Conf.Sync.Stat = msg Conf.Save() - util.BroadcastByType("main", "syncing", 1, msg, nil) + code := 1 + if nil != err { + code = 2 + } + util.BroadcastByType("main", "syncing", code, msg, nil) return } @@ -197,6 +182,11 @@ func checkSync(boot, exit, byHand bool) bool { planSyncAfter(64 * time.Minute) return false } + + if !util.IsOnline() { + util.BroadcastByType("main", "syncing", 2, Conf.Language(28), nil) + return false + } return true } diff --git a/kernel/util/net.go b/kernel/util/net.go index 59e5930dd..3cf013cec 100644 --- a/kernel/util/net.go +++ b/kernel/util/net.go @@ -17,15 +17,29 @@ package util import ( + "github.com/imroc/req/v3" "github.com/siyuan-note/httpclient" "net/http" "strings" + "time" "github.com/88250/gulu" "github.com/gin-gonic/gin" "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 { ret := session.Request.Header.Get("X-forwarded-for") ret = strings.TrimSpace(ret)