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

This commit is contained in:
Liang Ding 2023-02-07 09:20:32 +08:00
parent 9f491713ad
commit 8a7df8af78
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
6 changed files with 40 additions and 22 deletions

View file

@ -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)

View file

@ -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) {

View file

@ -50,7 +50,7 @@ func AutoGenerateDocHistory() {
ChangeHistoryTick(Conf.Editor.GenerateHistoryInterval)
for {
<-historyTicker.C
task.PrependTask(task.HistoryGenerateDoc, generateDocHistory)
task.AppendTask(task.HistoryGenerateDoc, generateDocHistory)
}
}

View file

@ -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)
}

View file

@ -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) {

View file

@ -39,27 +39,20 @@ type Task struct {
Created time.Time
}
func PrependTask(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)
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)
return
}
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]++