An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:recycle: Implement some delayed kernel events using task queues https://github.com/siyuan-note/siyuan/issues/12393
Daniel653a765b1720
diff --git a/kernel/job/cron.go b/kernel/job/cron.goindex 90db0d75f7..5db9154609 100644--- a/kernel/job/cron.go+++ b/kernel/job/cron.go@@ -28,6 +28,7 @@ import (func StartCron() {go every(100*time.Millisecond, task.ExecTaskJob)+ go every(100*time.Millisecond, task.ExecAsyncTaskJob)go every(5*time.Second, task.StatusJob)go every(5*time.Second, model.SyncDataJob)go every(2*time.Hour, model.StatJob)diff --git a/kernel/model/repository.go b/kernel/model/repository.goindex 0115268f74..bb505a1cf8 100644--- a/kernel/model/repository.go+++ b/kernel/model/repository.go@@ -656,7 +656,7 @@ func checkoutRepo(id string) {task.AppendTask(task.ReloadUI, util.ReloadUIResetScroll)if syncEnabled {- task.AppendAsyncTaskWithDelay(task.PushMsg, 3*time.Second, util.PushMsg, Conf.Language(134), 0)+ task.AppendAsyncTaskWithDelay(task.PushMsg, 7*time.Second, util.PushMsg, Conf.Language(134), 0)}return}diff --git a/kernel/model/search.go b/kernel/model/search.goindex 19e1521096..ed06876918 100644--- a/kernel/model/search.go+++ b/kernel/model/search.go@@ -790,10 +790,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, idsWaitForWritingFiles()if 0 < len(ids) {- go func() {- time.Sleep(time.Millisecond * 500)- util.ReloadUI()- }()+ task.AppendAsyncTaskWithDelay(task.ReloadUI, 500*time.Millisecond, util.ReloadUI)}return}diff --git a/kernel/task/queue.go b/kernel/task/queue.goindex e8a8cd7b2e..db339e5ea3 100644--- a/kernel/task/queue.go+++ b/kernel/task/queue.go@@ -205,8 +205,8 @@ func StatusJob() {}func ExecTaskJob() {- syncTask, asyncTasks := popTasks()- if nil == syncTask && 1 > len(asyncTasks) {+ task := popTask()+ if nil == task {return}@@ -214,39 +214,69 @@ func ExecTaskJob() {return}- for _, asyncTask := range asyncTasks {- go func() {- execTask(asyncTask)- }()+ execTask(task)+}++func popTask() (ret *Task) {+ queueLock.Lock()+ defer queueLock.Unlock()++ if 1 > len(taskQueue) {+ return}- if nil != syncTask {- execTask(syncTask)+ for i, task := range taskQueue {+ if time.Since(task.Created) <= task.Delay {+ continue+ }++ if !task.Async {+ ret = task+ taskQueue = append(taskQueue[:i], taskQueue[i+1:]...)+ return+ }}+ return}-func popTasks() (syncTask *Task, asyncTasks []*Task) {+func ExecAsyncTaskJob() {+ tasks := popAsyncTasks()+ if 1 > len(tasks) {+ return+ }++ if util.IsExiting.Load() {+ return+ }++ for _, task := range tasks {+ go func() {+ execTask(task)+ }()+ }+}++func popAsyncTasks() (ret []*Task) {queueLock.Lock()defer queueLock.Unlock()- if 0 == len(taskQueue) {+ if 1 > len(taskQueue) {return}var popedIndexes []intfor i, task := range taskQueue {+ if !task.Async {+ continue+ }+if time.Since(task.Created) <= task.Delay {continue}if task.Async {- asyncTasks = append(asyncTasks, task)+ ret = append(ret, task)popedIndexes = append(popedIndexes, i)- } else {- if nil == syncTask {- syncTask = task- popedIndexes = append(popedIndexes, i)- }}}@@ -301,7 +331,7 @@ func execTask(task *Task) {case <-ctx.Done():logging.LogWarnf("task [%s] timeout", task.Action)case <-ch:- logging.LogInfof("task [%s] done", task.Action)+ //logging.LogInfof("task [%s] done", task.Action)}if !task.Async {