Skip to content

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

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

Liang Dingc40355e47ffb
diff --git a/kernel/model/conf.go b/kernel/model/conf.go
index f96e509079..fa8ea985c0 100644
--- a/kernel/model/conf.go
+++ b/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)
diff --git a/kernel/model/repository.go b/kernel/model/repository.go
index 767fb2489b..c65660a62b 100644
--- a/kernel/model/repository.go
+++ b/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)
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 17098e9a0c..cda7666c9c 100644
--- a/kernel/model/transaction.go
+++ b/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)