queue.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SiYuan - Build Your Eternal Digital Garden
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package task
  17. import (
  18. "reflect"
  19. "sync"
  20. "time"
  21. "github.com/siyuan-note/logging"
  22. )
  23. var (
  24. taskQueue []*Task
  25. taskQueueStatus int
  26. queueLock = sync.Mutex{}
  27. taskLock = sync.Mutex{}
  28. )
  29. const (
  30. QueueStatusRunning = iota
  31. QueueStatusClosing
  32. )
  33. type Task struct {
  34. Action string
  35. Handler reflect.Value
  36. Args []interface{}
  37. Created time.Time
  38. }
  39. func PrependTask(action string, handler interface{}, args ...interface{}) {
  40. queueLock.Lock()
  41. defer queueLock.Unlock()
  42. if QueueStatusRunning != taskQueueStatus {
  43. logging.LogWarnf("task queue is paused, action [%s] will be ignored", action)
  44. return
  45. }
  46. cancelTask(action)
  47. taskQueue = append([]*Task{newTask(action, handler, args...)}, taskQueue...)
  48. }
  49. func AppendTask(action string, handler interface{}, args ...interface{}) {
  50. queueLock.Lock()
  51. defer queueLock.Unlock()
  52. if QueueStatusRunning != taskQueueStatus {
  53. logging.LogWarnf("task queue is paused, action [%s] will be ignored", action)
  54. return
  55. }
  56. cancelTask(action)
  57. taskQueue = append(taskQueue, newTask(action, handler, args...))
  58. }
  59. func CancelTask(actions ...string) {
  60. queueLock.Lock()
  61. defer queueLock.Unlock()
  62. cancelTask(actions...)
  63. }
  64. func cancelTask(actions ...string) {
  65. for i := len(taskQueue) - 1; i >= 0; i-- {
  66. task := taskQueue[i]
  67. for _, action := range actions {
  68. if action == task.Action {
  69. taskQueue = append(taskQueue[:i], taskQueue[i+1:]...)
  70. break
  71. }
  72. }
  73. }
  74. }
  75. func newTask(action string, handler interface{}, args ...interface{}) *Task {
  76. return &Task{
  77. Action: action,
  78. Handler: reflect.ValueOf(handler),
  79. Args: args,
  80. Created: time.Now(),
  81. }
  82. }
  83. const (
  84. CloudSync = "task.cloud.sync" // 数据同步
  85. RepoCheckout = "task.repo.checkout" // 从快照中检出
  86. DatabaseIndexFull = "task.database.index.full" // 重建索引
  87. DatabaseIndex = "task.database.index" // 数据库所以队列
  88. DatabaseIndexFix = "task.database.index.fix" // 数据库索引订正
  89. OCRImage = "task.ocr.image" // 图片 OCR 提取文本
  90. HistoryGenerateDoc = "task.history.generateDoc" // 生成文件历史
  91. DatabaseIndexEmbedBlock = "task.database.index.embedBlock" // 数据库索引嵌入块
  92. )
  93. func Loop() {
  94. for {
  95. time.Sleep(10 * time.Millisecond)
  96. task := popTask()
  97. if nil == task {
  98. continue
  99. }
  100. execTask(task)
  101. }
  102. }
  103. func CloseWait() {
  104. queueLock.Lock()
  105. defer queueLock.Unlock()
  106. taskQueueStatus = QueueStatusClosing
  107. for {
  108. time.Sleep(10 * time.Millisecond)
  109. if 1 > len(taskQueue) {
  110. break
  111. }
  112. }
  113. }
  114. func popTask() (ret *Task) {
  115. queueLock.Lock()
  116. defer queueLock.Unlock()
  117. if 0 == len(taskQueue) {
  118. return
  119. }
  120. ret = taskQueue[0]
  121. taskQueue = taskQueue[1:]
  122. return
  123. }
  124. func execTask(task *Task) {
  125. taskLock.Lock()
  126. defer taskLock.Unlock()
  127. defer logging.Recover()
  128. args := make([]reflect.Value, len(task.Args))
  129. for i, v := range task.Args {
  130. if nil == v {
  131. args[i] = reflect.New(task.Handler.Type().In(i)).Elem()
  132. } else {
  133. args[i] = reflect.ValueOf(v)
  134. }
  135. }
  136. task.Handler.Call(args)
  137. }