Skip to content

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

:recycle: 后台任务队列支持设置超时 Fix https://github.com/siyuan-note/siyuan/issues/7331

Liang Ding5e254500ef7d
diff --git a/kernel/model/index.go b/kernel/model/index.go
index f03446067a..f93c07955c 100644
--- a/kernel/model/index.go
+++ b/kernel/model/index.go
@@ -209,7 +209,7 @@ func IndexRefs() {
// IndexEmbedBlockJob 嵌入块支持搜索 https://github.com/siyuan-note/siyuan/issues/7112
func IndexEmbedBlockJob() {
embedBlocks := sql.QueryEmptyContentEmbedBlocks()
- task.AppendTask(task.DatabaseIndexEmbedBlock, autoIndexEmbedBlock, embedBlocks)
+ task.AppendTaskWithTimeout(task.DatabaseIndexEmbedBlock, 30*time.Second, autoIndexEmbedBlock, embedBlocks)
}
func autoIndexEmbedBlock(embedBlocks []*sql.Block) {
diff --git a/kernel/model/ocr.go b/kernel/model/ocr.go
index 8ebeb4f61b..f6944965e7 100644
--- a/kernel/model/ocr.go
+++ b/kernel/model/ocr.go
@@ -21,7 +21,7 @@ func OCRAssetsJob() {
return
}
- task.AppendTask(task.OCRImage, autoOCRAssets)
+ task.AppendTaskWithTimeout(task.OCRImage, 7*time.Second, autoOCRAssets)
}
func autoOCRAssets() {
@@ -39,7 +39,7 @@ func autoOCRAssets() {
util.AssetsTextsLock.Unlock()
util.AssetsTextsChanged = true
- if 16 <= i { // 一次任务中最多处理 16 张图片,防止卡顿
+ if 4 <= i { // 一次任务中最多处理 4 张图片,防止卡顿
break
}
}
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 4168c6b441..9d0b60ccb2 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -97,7 +97,7 @@ func searchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMod
}
// 嵌入块支持搜索 https://github.com/siyuan-note/siyuan/issues/7112
- task.AppendTask(task.DatabaseIndexEmbedBlock, updateEmbedBlockContent, embedBlockID, ret)
+ task.AppendTaskWithTimeout(task.DatabaseIndexEmbedBlock, 30*time.Second, updateEmbedBlockContent, embedBlockID, ret)
// 添加笔记本名称
var boxIDs []string
diff --git a/kernel/task/queue.go b/kernel/task/queue.go
index fcc18d274d..86d6b290fd 100644
--- a/kernel/task/queue.go
+++ b/kernel/task/queue.go
@@ -37,9 +37,14 @@ type Task struct {
Handler reflect.Value
Args []interface{}
Created time.Time
+ Timeout time.Duration
}
func AppendTask(action string, handler interface{}, args ...interface{}) {
+ AppendTaskWithTimeout(action, 24*time.Hour, handler, args...)
+}
+
+func AppendTaskWithTimeout(action string, timeout time.Duration, handler interface{}, args ...interface{}) {
if util.IsExiting {
//logging.LogWarnf("task queue is paused, action [%s] will be ignored", action)
return
@@ -53,16 +58,13 @@ func AppendTask(action string, handler interface{}, args ...interface{}) {
queueLock.Lock()
defer queueLock.Unlock()
- taskQueue = append(taskQueue, newTask(action, handler, args...))
-}
-
-func newTask(action string, handler interface{}, args ...interface{}) *Task {
- return &Task{
+ taskQueue = append(taskQueue, &Task{
Action: action,
+ Timeout: timeout,
Handler: reflect.ValueOf(handler),
Args: args,
Created: time.Now(),
- }
+ })
}
func getCurrentActions() (ret []string) {
@@ -166,8 +168,6 @@ func ExecTaskJob() {
execTask(task)
}
-var currentTaskAction string
-
func popTask() (ret *Task) {
queueLock.Lock()
defer queueLock.Unlock()
@@ -181,6 +181,8 @@ func popTask() (ret *Task) {
return
}
+var currentTaskAction string
+
func execTask(task *Task) {
defer logging.Recover()
@@ -195,7 +197,7 @@ func execTask(task *Task) {
currentTaskAction = task.Action
- ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ ctx, cancel := context.WithTimeout(context.Background(), task.Timeout)
defer cancel()
ch := make(chan bool, 1)
go func() {