Przeglądaj źródła

:recycle: Implement some delayed kernel events using task queues https://github.com/siyuan-note/siyuan/issues/12393

Daniel 10 miesięcy temu
rodzic
commit
653a765b17
4 zmienionych plików z 50 dodań i 22 usunięć
  1. 1 0
      kernel/job/cron.go
  2. 1 1
      kernel/model/repository.go
  3. 1 4
      kernel/model/search.go
  4. 47 17
      kernel/task/queue.go

+ 1 - 0
kernel/job/cron.go

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

+ 1 - 1
kernel/model/repository.go

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

+ 1 - 4
kernel/model/search.go

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

+ 47 - 17
kernel/task/queue.go

@@ -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)
-		}()
+	execTask(task)
+}
+
+func popTask() (ret *Task) {
+	queueLock.Lock()
+	defer queueLock.Unlock()
+
+	if 1 > len(taskQueue) {
+		return
 	}
 
-	if nil != syncTask {
-		execTask(syncTask)
+	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 popTasks() (syncTask *Task, asyncTasks []*Task) {
+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 0 == len(taskQueue) {
+	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 {