mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-04 03:31:31 +08:00
♻️ 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
@ -28,6 +28,7 @@ import (
|
|||||||
|
|
||||||
func StartCron() {
|
func StartCron() {
|
||||||
go every(100*time.Millisecond, task.ExecTaskJob)
|
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, task.StatusJob)
|
||||||
go every(5*time.Second, model.SyncDataJob)
|
go every(5*time.Second, model.SyncDataJob)
|
||||||
go every(2*time.Hour, model.StatJob)
|
go every(2*time.Hour, model.StatJob)
|
||||||
|
@ -656,7 +656,7 @@ func checkoutRepo(id string) {
|
|||||||
task.AppendTask(task.ReloadUI, util.ReloadUIResetScroll)
|
task.AppendTask(task.ReloadUI, util.ReloadUIResetScroll)
|
||||||
|
|
||||||
if syncEnabled {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
@ -790,10 +790,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids
|
|||||||
|
|
||||||
WaitForWritingFiles()
|
WaitForWritingFiles()
|
||||||
if 0 < len(ids) {
|
if 0 < len(ids) {
|
||||||
go func() {
|
task.AppendAsyncTaskWithDelay(task.ReloadUI, 500*time.Millisecond, util.ReloadUI)
|
||||||
time.Sleep(time.Millisecond * 500)
|
|
||||||
util.ReloadUI()
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -205,8 +205,8 @@ func StatusJob() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ExecTaskJob() {
|
func ExecTaskJob() {
|
||||||
syncTask, asyncTasks := popTasks()
|
task := popTask()
|
||||||
if nil == syncTask && 1 > len(asyncTasks) {
|
if nil == task {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,40 +214,70 @@ func ExecTaskJob() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, asyncTask := range asyncTasks {
|
execTask(task)
|
||||||
go func() {
|
|
||||||
execTask(asyncTask)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
if nil != syncTask {
|
|
||||||
execTask(syncTask)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func popTasks() (syncTask *Task, asyncTasks []*Task) {
|
func popTask() (ret *Task) {
|
||||||
queueLock.Lock()
|
queueLock.Lock()
|
||||||
defer queueLock.Unlock()
|
defer queueLock.Unlock()
|
||||||
|
|
||||||
if 0 == len(taskQueue) {
|
if 1 > len(taskQueue) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var popedIndexes []int
|
|
||||||
for i, task := range taskQueue {
|
for i, task := range taskQueue {
|
||||||
if time.Since(task.Created) <= task.Delay {
|
if time.Since(task.Created) <= task.Delay {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if task.Async {
|
if !task.Async {
|
||||||
asyncTasks = append(asyncTasks, task)
|
ret = task
|
||||||
popedIndexes = append(popedIndexes, i)
|
taskQueue = append(taskQueue[:i], taskQueue[i+1:]...)
|
||||||
} else {
|
return
|
||||||
if nil == syncTask {
|
|
||||||
syncTask = task
|
|
||||||
popedIndexes = append(popedIndexes, i)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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 {
|
||||||
|
ret = append(ret, task)
|
||||||
|
popedIndexes = append(popedIndexes, i)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if 0 < len(popedIndexes) {
|
if 0 < len(popedIndexes) {
|
||||||
@ -301,7 +331,7 @@ func execTask(task *Task) {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
logging.LogWarnf("task [%s] timeout", task.Action)
|
logging.LogWarnf("task [%s] timeout", task.Action)
|
||||||
case <-ch:
|
case <-ch:
|
||||||
logging.LogInfof("task [%s] done", task.Action)
|
//logging.LogInfof("task [%s] done", task.Action)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !task.Async {
|
if !task.Async {
|
||||||
|
Loading…
Reference in New Issue
Block a user