mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-09-22 00:20:47 +02:00
🎨 后台任务按任务加入先后顺序去重执行 Fix https://github.com/siyuan-note/siyuan/issues/7270
This commit is contained in:
parent
9f491713ad
commit
8a7df8af78
6 changed files with 40 additions and 22 deletions
|
@ -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]++
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue