♻️ Implement some delayed kernel events using task queues https://github.com/siyuan-note/siyuan/issues/12393
This commit is contained in:
parent
09eec52a02
commit
653a765b17
4 changed files with 54 additions and 26 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -790,10 +790,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
|
|||
|
||||
WaitForWritingFiles()
|
||||
if 0 < len(ids) {
|
||||
go func() {
|
||||
time.Sleep(time.Millisecond * 500)
|
||||
util.ReloadUI()
|
||||
}()
|
||||
task.AppendAsyncTaskWithDelay(task.ReloadUI, 500*time.Millisecond, util.ReloadUI)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}()
|
||||
}
|
||||
|
||||
if nil != syncTask {
|
||||
execTask(syncTask)
|
||||
}
|
||||
execTask(task)
|
||||
}
|
||||
|
||||
func popTasks() (syncTask *Task, asyncTasks []*Task) {
|
||||
func popTask() (ret *Task) {
|
||||
queueLock.Lock()
|
||||
defer queueLock.Unlock()
|
||||
|
||||
if 0 == len(taskQueue) {
|
||||
if 1 > len(taskQueue) {
|
||||
return
|
||||
}
|
||||
|
||||
var popedIndexes []int
|
||||
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 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 1 > len(taskQueue) {
|
||||
return
|
||||
}
|
||||
|
||||
var popedIndexes []int
|
||||
for 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 {
|
||||
|
|
Loading…
Add table
Reference in a new issue