Kaynağa Gözat

:art: 改进内核任务调度机制提升稳定性 https://github.com/siyuan-note/siyuan/issues/7113

Liang Ding 2 yıl önce
ebeveyn
işleme
c40355e47f
3 değiştirilmiş dosya ile 16 ekleme ve 63 silme
  1. 2 0
      kernel/model/conf.go
  2. 12 13
      kernel/model/repository.go
  3. 2 50
      kernel/model/transaction.go

+ 2 - 0
kernel/model/conf.go

@@ -37,6 +37,7 @@ import (
 	"github.com/siyuan-note/logging"
 	"github.com/siyuan-note/siyuan/kernel/conf"
 	"github.com/siyuan-note/siyuan/kernel/sql"
+	"github.com/siyuan-note/siyuan/kernel/task"
 	"github.com/siyuan-note/siyuan/kernel/treenode"
 	"github.com/siyuan-note/siyuan/kernel/util"
 	"golang.org/x/text/language"
@@ -425,6 +426,7 @@ func Close(force bool, execInstallPkg int) (exitCode int) {
 		}
 	}
 
+	task.CloseWait()
 	Conf.Close()
 	sql.CloseDatabase()
 	treenode.SaveBlockTree(false)

+ 12 - 13
kernel/model/repository.go

@@ -49,6 +49,7 @@ import (
 	"github.com/siyuan-note/siyuan/kernel/cache"
 	"github.com/siyuan-note/siyuan/kernel/conf"
 	"github.com/siyuan-note/siyuan/kernel/sql"
+	"github.com/siyuan-note/siyuan/kernel/task"
 	"github.com/siyuan-note/siyuan/kernel/treenode"
 	"github.com/siyuan-note/siyuan/kernel/util"
 	"github.com/studio-b12/gowebdav"
@@ -501,16 +502,21 @@ func InitRepoKey() (err error) {
 	return
 }
 
-var isCheckoutRepo bool
+func CheckoutRepo(id string) {
+	task.PrependTask(task.RepoCheckout, checkoutRepo, id)
+}
 
-func CheckoutRepo(id string) (err error) {
+func checkoutRepo(id string) {
+	var err error
 	if 1 > len(Conf.Repo.Key) {
-		err = errors.New(Conf.Language(26))
+		util.PushErrMsg(Conf.Language(26), 7000)
 		return
 	}
 
 	repo, err := newRepository()
 	if nil != err {
+		logging.LogErrorf("new repository failed: %s", err)
+		util.PushErrMsg(Conf.Language(141), 7000)
 		return
 	}
 
@@ -525,23 +531,16 @@ func CheckoutRepo(id string) (err error) {
 	Conf.Sync.Enabled = false
 	Conf.Save()
 
-	if util.IsMutexLocked(&syncLock) {
-		err = errors.New("sync is running, please try again later")
-		return
-	}
-
-	isCheckoutRepo = true
-	defer func() {
-		isCheckoutRepo = false
-	}()
-
 	_, _, err = repo.Checkout(id, map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBarAndProgress})
 	if nil != err {
+		logging.LogErrorf("checkout repository failed: %s", err)
 		util.PushClearProgress()
+		util.PushErrMsg(Conf.Language(141), 7000)
 		return
 	}
 
 	FullReindex()
+
 	if syncEnabled {
 		func() {
 			time.Sleep(5 * time.Second)

+ 2 - 50
kernel/model/transaction.go

@@ -20,7 +20,6 @@ import (
 	"bytes"
 	"errors"
 	"fmt"
-	"io"
 	"os"
 	"path"
 	"path/filepath"
@@ -38,6 +37,7 @@ import (
 	"github.com/siyuan-note/logging"
 	"github.com/siyuan-note/siyuan/kernel/cache"
 	"github.com/siyuan-note/siyuan/kernel/sql"
+	"github.com/siyuan-note/siyuan/kernel/task"
 	"github.com/siyuan-note/siyuan/kernel/treenode"
 	"github.com/siyuan-note/siyuan/kernel/util"
 )
@@ -106,12 +106,7 @@ func AutoFlushTx() {
 	}
 }
 
-var txLock = sync.Mutex{}
-
 func flushTx() {
-	txLock.Lock()
-	defer txLock.Unlock()
-
 	defer logging.Recover()
 
 	currentTx = mergeTx()
@@ -1224,7 +1219,7 @@ func updateRefText(refNode *ast.Node, changedDefNodes map[string]*ast.Node) (cha
 // AutoFixIndex 自动校验数据库索引 https://github.com/siyuan-note/siyuan/issues/7016
 func AutoFixIndex() {
 	for {
-		autoFixIndex()
+		task.AppendTask(task.DatabaseIndexFix, autoFixIndex)
 		time.Sleep(10 * time.Minute)
 	}
 }
@@ -1234,21 +1229,6 @@ var autoFixLock = sync.Mutex{}
 func autoFixIndex() {
 	defer logging.Recover()
 
-	if isFullReindexing {
-		logging.LogInfof("skip check index caused by full reindexing")
-		return
-	}
-
-	if util.IsMutexLocked(&syncLock) {
-		logging.LogInfof("skip check index caused by sync lock")
-		return
-	}
-
-	if isCheckoutRepo {
-		logging.LogInfof("skip check index caused by checkout repo")
-		return
-	}
-
 	if util.IsMutexLocked(&autoFixLock) {
 		return
 	}
@@ -1262,10 +1242,6 @@ func autoFixIndex() {
 		boxPath := filepath.Join(util.DataDir, box.ID)
 		var paths []string
 		filepath.Walk(boxPath, func(path string, info os.FileInfo, err error) error {
-			if isFullReindexing {
-				return io.EOF
-			}
-
 			if !info.IsDir() && filepath.Ext(path) == ".sy" {
 				p := path[len(boxPath):]
 				p = filepath.ToSlash(p)
@@ -1278,19 +1254,11 @@ func autoFixIndex() {
 
 		redundantPaths := treenode.GetRedundantPaths(box.ID, paths)
 		for _, p := range redundantPaths {
-			if isFullReindexing {
-				break
-			}
-
 			treenode.RemoveBlockTreesByPath(p)
 		}
 
 		missingPaths := treenode.GetNotExistPaths(box.ID, paths)
 		for i, p := range missingPaths {
-			if isFullReindexing {
-				break
-			}
-
 			id := path.Base(p)
 			id = strings.TrimSuffix(id, ".sy")
 			if !ast.IsNodeIDPattern(id) {
@@ -1308,10 +1276,6 @@ func autoFixIndex() {
 		i := -1
 		size := len(rootUpdatedMap)
 		for rootID, updated := range rootUpdatedMap {
-			if isFullReindexing {
-				break
-			}
-
 			i++
 
 			rootUpdated := dbRootUpdatedMap[rootID]
@@ -1341,10 +1305,6 @@ func autoFixIndex() {
 	duplicatedRootIDs := sql.GetDuplicatedRootIDs()
 	size := len(duplicatedRootIDs)
 	for i, rootID := range duplicatedRootIDs {
-		if isFullReindexing {
-			break
-		}
-
 		root := sql.GetBlock(rootID)
 		if nil == root {
 			continue
@@ -1359,10 +1319,6 @@ func autoFixIndex() {
 }
 
 func reindexTreeByPath(box, p string, i, size int) {
-	if isFullReindexing {
-		return
-	}
-
 	tree, err := LoadTree(box, p)
 	if nil != err {
 		return
@@ -1372,10 +1328,6 @@ func reindexTreeByPath(box, p string, i, size int) {
 }
 
 func reindexTree(rootID string, i, size int) {
-	if isFullReindexing {
-		return
-	}
-
 	root := treenode.GetBlockTree(rootID)
 	if nil == root {
 		logging.LogWarnf("root block not found", rootID)