Skip to content

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

:art: 后台任务按任务加入先后顺序去重执行 Fix https://github.com/siyuan-note/siyuan/issues/7270

Liang Ding8a7df8af788e
diff --git a/kernel/model/box.go b/kernel/model/box.go
index 1cad8fa817..20e7c77104 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -505,7 +505,7 @@ func ReloadUI() {
}
func FullReindex() {
- task.PrependTask(task.DatabaseIndexFull, fullReindex)
+ task.AppendTask(task.DatabaseIndexFull, fullReindex)
task.AppendTask(task.DatabaseCache, sql.EnableCache)
task.AppendTask(task.DatabaseIndexRef, IndexRefs)
task.AppendTask(task.ReloadUI, util.ReloadUI)
diff --git a/kernel/model/file.go b/kernel/model/file.go
index 1483d25d9e..a9fec85224 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -1256,7 +1256,7 @@ func removeDoc(box *Box, p string) {
}
util.PushEvent(evt)
- task.PrependTask(task.DatabaseIndex, removeDoc0, box, p, childrenDir)
+ task.AppendTask(task.DatabaseIndex, removeDoc0, box, p, childrenDir)
}
func removeDoc0(box *Box, p, childrenDir string) {
diff --git a/kernel/model/history.go b/kernel/model/history.go
index 05ec3ed88d..046cea6dfa 100644
--- a/kernel/model/history.go
+++ b/kernel/model/history.go
@@ -50,7 +50,7 @@ func AutoGenerateDocHistory() {
ChangeHistoryTick(Conf.Editor.GenerateHistoryInterval)
for {
<-historyTicker.C
- task.PrependTask(task.HistoryGenerateDoc, generateDocHistory)
+ task.AppendTask(task.HistoryGenerateDoc, generateDocHistory)
}
}
diff --git a/kernel/model/index.go b/kernel/model/index.go
index 533648040d..2d321826bf 100644
--- a/kernel/model/index.go
+++ b/kernel/model/index.go
@@ -39,7 +39,7 @@ import (
)
func (box *Box) Unindex() {
- task.PrependTask(task.DatabaseIndex, unindex, box.ID)
+ task.AppendTask(task.DatabaseIndex, unindex, box.ID)
}
func unindex(boxID string) {
@@ -49,7 +49,7 @@ func unindex(boxID string) {
}
func (box *Box) Index() {
- task.PrependTask(task.DatabaseIndex, index, box.ID)
+ task.AppendTask(task.DatabaseIndex, index, box.ID)
task.AppendTask(task.DatabaseIndexRef, IndexRefs)
}
diff --git a/kernel/model/repository.go b/kernel/model/repository.go
index 6b42965e6a..5238b6f5ba 100644
--- a/kernel/model/repository.go
+++ b/kernel/model/repository.go
@@ -504,7 +504,7 @@ func InitRepoKey() (err error) {
}
func CheckoutRepo(id string) {
- task.PrependTask(task.RepoCheckout, checkoutRepo, id)
+ task.AppendTask(task.RepoCheckout, checkoutRepo, id)
}
func checkoutRepo(id string) {
diff --git a/kernel/task/queue.go b/kernel/task/queue.go
index ad74551f88..99c3324a3f 100644
--- a/kernel/task/queue.go
+++ b/kernel/task/queue.go
@@ -39,27 +39,20 @@ type Task struct {
Created time.Time
}
-func PrependTask(action string, handler interface{}, args ...interface{}) {
- queueLock.Lock()
- defer queueLock.Unlock()
-
+func AppendTask(action string, handler interface{}, args ...interface{}) {
if util.IsExiting {
//logging.LogWarnf("task queue is paused, action [%s] will be ignored", action)
return
}
- taskQueue = append([]*Task{newTask(action, handler, args...)}, taskQueue...)
-}
-
-func AppendTask(action string, handler interface{}, args ...interface{}) {
- queueLock.Lock()
- defer queueLock.Unlock()
-
- if util.IsExiting {
- //logging.LogWarnf("task queue is paused, action [%s] will be ignored", action)
+ currentActions := getCurrentActions()
+ if gulu.Str.Contains(action, currentActions) && gulu.Str.Contains(action, uniqueActions) {
+ //logging.LogWarnf("task [%s] is already in queue, will be ignored", action)
return
}
+ queueLock.Lock()
+ defer queueLock.Unlock()
taskQueue = append(taskQueue, newTask(action, handler, args...))
}
@@ -72,6 +65,20 @@ func newTask(action string, handler interface{}, args ...interface{}) *Task {
}
}
+func getCurrentActions() (ret []string) {
+ queueLock.Lock()
+ defer queueLock.Unlock()
+
+ if "" != currentTaskAction {
+ ret = append(ret, currentTaskAction)
+ }
+
+ for _, task := range taskQueue {
+ ret = append(ret, task.Action)
+ }
+ return
+}
+
const (
RepoCheckout = "task.repo.checkout" // 从快照中检出
DatabaseIndexFull = "task.database.index.full" // 重建索引
@@ -86,6 +93,17 @@ const (
ReloadUI = "task.reload.ui" // 重载 UI
)
+// uniqueActions 描述了唯一的任务,即队列中只能存在一个在执行的任务。
+var uniqueActions = []string{
+ RepoCheckout,
+ DatabaseIndexFull,
+ DatabaseIndexCommit,
+ DatabaseIndexFix,
+ OCRImage,
+ HistoryGenerateDoc,
+ DatabaseIndexEmbedBlock,
+}
+
func Contain(action string, moreActions ...string) bool {
actions := append(moreActions, action)
actions = gulu.Str.RemoveDuplicatedElem(actions)
@@ -106,7 +124,7 @@ func StatusJob() {
for _, task := range tasks {
action := task.Action
if c := count[action]; 2 < c {
- //logging.LogWarnf("too many tasks [%s], ignore show its status", action)
+ logging.LogWarnf("too many tasks [%s], ignore show its status", action)
continue
}
count[action]++