queue.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. "github.com/siyuan-note/siyuan/kernel/util"
  19. "reflect"
  20. "sync"
  21. "time"
  22. "github.com/siyuan-note/logging"
  23. )
  24. var (
  25. taskQueue []*Task
  26. taskQueueStatus int
  27. queueLock = sync.Mutex{}
  28. taskLock = sync.Mutex{}
  29. )
  30. const (
  31. QueueStatusRunning = iota
  32. QueueStatusClosing
  33. )
  34. type Task struct {
  35. Action string
  36. Handler reflect.Value
  37. Args []interface{}
  38. Created time.Time
  39. }
  40. func PrependTask(action string, handler interface{}, args ...interface{}) {
  41. queueLock.Lock()
  42. defer queueLock.Unlock()
  43. if QueueStatusRunning != taskQueueStatus {
  44. //logging.LogWarnf("task queue is paused, action [%s] will be ignored", action)
  45. return
  46. }
  47. cancelTask(action)
  48. taskQueue = append([]*Task{newTask(action, handler, args...)}, taskQueue...)
  49. }
  50. func AppendTask(action string, handler interface{}, args ...interface{}) {
  51. queueLock.Lock()
  52. defer queueLock.Unlock()
  53. if QueueStatusRunning != taskQueueStatus {
  54. //logging.LogWarnf("task queue is paused, action [%s] will be ignored", action)
  55. return
  56. }
  57. cancelTask(action)
  58. taskQueue = append(taskQueue, newTask(action, handler, args...))
  59. }
  60. func CancelTask(actions ...string) {
  61. queueLock.Lock()
  62. defer queueLock.Unlock()
  63. cancelTask(actions...)
  64. }
  65. func cancelTask(actions ...string) {
  66. for i := len(taskQueue) - 1; i >= 0; i-- {
  67. task := taskQueue[i]
  68. for _, action := range actions {
  69. if action == task.Action {
  70. taskQueue = append(taskQueue[:i], taskQueue[i+1:]...)
  71. break
  72. }
  73. }
  74. }
  75. }
  76. func newTask(action string, handler interface{}, args ...interface{}) *Task {
  77. return &Task{
  78. Action: action,
  79. Handler: reflect.ValueOf(handler),
  80. Args: args,
  81. Created: time.Now(),
  82. }
  83. }
  84. const (
  85. CloudSync = "task.cloud.sync" // 数据同步
  86. RepoCheckout = "task.repo.checkout" // 从快照中检出
  87. DatabaseIndexFull = "task.database.index.full" // 重建索引
  88. DatabaseIndex = "task.database.index" // 数据库索引队列
  89. DatabaseIndexRef = "task.database.index.ref" // 数据库索引引用
  90. DatabaseIndexFix = "task.database.index.fix" // 数据库索引订正
  91. OCRImage = "task.ocr.image" // 图片 OCR 提取文本
  92. HistoryGenerateDoc = "task.history.generateDoc" // 生成文件历史
  93. DatabaseIndexEmbedBlock = "task.database.index.embedBlock" // 数据库索引嵌入块
  94. )
  95. func StatusLoop() {
  96. for {
  97. time.Sleep(1000 * time.Millisecond)
  98. tasks := taskQueue
  99. data := map[string]interface{}{}
  100. var items []map[string]interface{}
  101. for _, task := range tasks {
  102. if OCRImage == task.Action || DatabaseIndexEmbedBlock == task.Action {
  103. continue
  104. }
  105. item := map[string]interface{}{
  106. "action": task.Action,
  107. }
  108. items = append(items, item)
  109. }
  110. if 1 > len(items) {
  111. items = []map[string]interface{}{}
  112. }
  113. data["tasks"] = items
  114. util.PushBackgroundTask(data)
  115. if 0 < len(tasks) {
  116. time.Sleep(1000 * time.Millisecond)
  117. }
  118. }
  119. }
  120. func Loop() {
  121. for {
  122. time.Sleep(10 * time.Millisecond)
  123. if QueueStatusClosing == taskQueueStatus {
  124. clearQueue()
  125. break
  126. }
  127. task := popTask()
  128. if nil == task {
  129. continue
  130. }
  131. if util.IsExiting {
  132. break
  133. }
  134. execTask(task)
  135. }
  136. }
  137. func CloseWait() {
  138. taskQueueStatus = QueueStatusClosing
  139. for {
  140. time.Sleep(10 * time.Millisecond)
  141. if 1 > len(taskQueue) {
  142. break
  143. }
  144. }
  145. }
  146. func clearQueue() {
  147. queueLock.Lock()
  148. defer queueLock.Unlock()
  149. taskQueue = []*Task{}
  150. }
  151. func popTask() (ret *Task) {
  152. queueLock.Lock()
  153. defer queueLock.Unlock()
  154. if 0 == len(taskQueue) {
  155. return
  156. }
  157. ret = taskQueue[0]
  158. taskQueue = taskQueue[1:]
  159. return
  160. }
  161. func execTask(task *Task) {
  162. taskLock.Lock()
  163. defer taskLock.Unlock()
  164. defer logging.Recover()
  165. args := make([]reflect.Value, len(task.Args))
  166. for i, v := range task.Args {
  167. if nil == v {
  168. args[i] = reflect.New(task.Handler.Type().In(i)).Elem()
  169. } else {
  170. args[i] = reflect.ValueOf(v)
  171. }
  172. }
  173. task.Handler.Call(args)
  174. }