Skip to content

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

:art: Skip absent plugin hooks and summarize asset indexing logs https://github.com/siyuan-note/siyuan/issues/20364

Danielb81eeac79051
diff --git a/kernel/model/asset_content.go b/kernel/model/asset_content.go
index 539f598d99..b2504eb0a0 100644
--- a/kernel/model/asset_content.go
+++ b/kernel/model/asset_content.go
@@ -424,6 +424,9 @@ func (searcher *AssetsSearcher) fullIndex(writeBatch func([]*sql.AssetContent))
if !gulu.File.IsDir(assetsDir) {
return
}
+ started := time.Now()
+ scanned, parsed := 0, 0
+ logging.LogInfof("start asset content full indexing")
const maxBatchCount, maxBatchBytes = 128, 4 * 1024 * 1024
var batch []*sql.AssetContent
@@ -435,7 +438,7 @@ func (searcher *AssetsSearcher) fullIndex(writeBatch func([]*sql.AssetContent))
writeBatch(batch)
batch, batchBytes = nil, 0
}
- filelock.Walk(assetsDir, func(absPath string, d fs.DirEntry, err error) error {
+ walkErr := filelock.Walk(assetsDir, func(absPath string, d fs.DirEntry, err error) error {
if err != nil {
logging.LogErrorf("walk dir [%s] failed: %s", absPath, err)
return err
@@ -444,6 +447,7 @@ func (searcher *AssetsSearcher) fullIndex(writeBatch func([]*sql.AssetContent))
if d.IsDir() {
return nil
}
+ scanned++
// 加密笔记本的 asset 是密文,跳过全量内容索引(避免密文污染搜索索引、泄漏文件名集合)
if IsEncryptedAssetPath(absPath) {
@@ -456,7 +460,7 @@ func (searcher *AssetsSearcher) fullIndex(writeBatch func([]*sql.AssetContent))
return nil
}
- logging.LogInfof("parsing asset content [%s]", absPath)
+ logging.LogDebugf("parsing asset content [%s]", absPath)
result := parser.Parse(absPath)
if nil == result {
@@ -472,6 +476,7 @@ func (searcher *AssetsSearcher) fullIndex(writeBatch func([]*sql.AssetContent))
result.Path = "assets" + filepath.ToSlash(strings.TrimPrefix(absPath, assetsDir))
result.Size = info.Size()
result.Updated = info.ModTime().Unix()
+ parsed++
if batchBytes+len(result.Content) > maxBatchBytes {
flush()
}
@@ -491,6 +496,8 @@ func (searcher *AssetsSearcher) fullIndex(writeBatch func([]*sql.AssetContent))
return nil
})
flush()
+ logging.LogInfof("asset content full indexing finished: files=%d, parsed=%d, skipped=%d, elapsed=%s, error=%v",
+ scanned, parsed, scanned-parsed, time.Since(started), walkErr)
}
func NewAssetsSearcher() *AssetsSearcher {
diff --git a/kernel/plugin/lifecycle_optional_test.go b/kernel/plugin/lifecycle_optional_test.go
new file mode 100644
index 0000000000..7da5e5c6b6
--- /dev/null
+++ b/kernel/plugin/lifecycle_optional_test.go
@@ -0,0 +1,59 @@
+package plugin
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/dop251/goja"
+ "github.com/dop251/goja_nodejs/eventloop"
+ "github.com/siyuan-note/logging"
+ "github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestOptionalLifecycleHooks(t *testing.T) {
+ logPath := filepath.Join(t.TempDir(), "lifecycle.log")
+ logging.SetLogPath(logPath)
+ t.Cleanup(func() { logging.SetLogPath(util.LogPath) })
+ for _, test := range []struct {
+ name, hook string
+ wantError bool
+ }{
+ {"null", "null", false},
+ {"undefined", "undefined", false},
+ {"function", "function () {}", false},
+ {"promise", "function () { return Promise.resolve(); }", false},
+ {"invalid", "42", true},
+ {"throws", "function () { throw new Error('hook-failed'); }", true},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ loop := eventloop.NewEventLoop()
+ loop.Start()
+ defer loop.Terminate()
+ plugin := &KernelPlugin{Petal: &model.Petal{Name: test.name}}
+ plugin.worker.Start(loop)
+ if _, err := plugin.worker.RunSync(func(rt *goja.Runtime) (any, error) {
+ return rt.RunString("globalThis.siyuan = {plugin: {lifecycle: {onrunning: " + test.hook + "}}};")
+ }); err != nil {
+ t.Fatal(err)
+ }
+ done := make(chan struct{})
+ go func() { plugin.invokeHook("onrunning"); close(done) }()
+ select {
+ case <-done:
+ case <-time.After(5 * time.Second):
+ t.Fatal("lifecycle invocation did not complete")
+ }
+ data, err := os.ReadFile(logPath)
+ if err != nil && !os.IsNotExist(err) {
+ t.Fatal(err)
+ }
+ if found := strings.Contains(string(data), "[plugin:"+test.name+"] lifecycle hook"); found != test.wantError {
+ t.Fatalf("lifecycle error logged=%v, want %v", found, test.wantError)
+ }
+ })
+ }
+}
diff --git a/kernel/plugin/plugin.go b/kernel/plugin/plugin.go
index f3cce4f986..bc15cd3c16 100644
--- a/kernel/plugin/plugin.go
+++ b/kernel/plugin/plugin.go
@@ -885,7 +885,7 @@ func storageWatchOperations(event fsnotify.Event) (ret []string) {
return
}
-// invokeHook calls a lifecycle hook (e.g. onload) if it exists, awaiting if it returns a Promise.
+// invokeHook 调用已绑定的生命周期钩子,等待其返回的 Promise;未实现的可选钩子直接跳过。
func (p *KernelPlugin) invokeHook(name string) {
var err error
defer func() {
@@ -917,6 +917,10 @@ func (p *KernelPlugin) invokeHook(name string) {
}
hookValue := pluginObj.Get(name)
+ if hookValue == nil || goja.IsNull(hookValue) || goja.IsUndefined(hookValue) {
+ done <- TaskResult{}
+ return
+ }
hook, ok := goja.AssertFunction(hookValue)
if !ok {
err = fmt.Errorf("globalThis.siyuan.plugin.lifecycle.%s not bound to a function", name)