queue.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. util.PushBackgroundTask(data)
  111. if 0 < len(tasks) {
  112. time.Sleep(1000 * time.Millisecond)
  113. }
  114. }
  115. }
  116. func Loop() {
  117. for {
  118. time.Sleep(10 * time.Millisecond)
  119. if QueueStatusClosing == taskQueueStatus {
  120. clearQueue()
  121. break
  122. }
  123. task := popTask()
  124. if nil == task {
  125. continue
  126. }
  127. if util.IsExiting {
  128. break
  129. }
  130. execTask(task)
  131. }
  132. }
  133. func CloseWait() {
  134. taskQueueStatus = QueueStatusClosing
  135. for {
  136. time.Sleep(10 * time.Millisecond)
  137. if 1 > len(taskQueue) {
  138. break
  139. }
  140. }
  141. }
  142. func clearQueue() {
  143. queueLock.Lock()
  144. defer queueLock.Unlock()
  145. taskQueue = []*Task{}
  146. }
  147. func popTask() (ret *Task) {
  148. queueLock.Lock()
  149. defer queueLock.Unlock()
  150. if 0 == len(taskQueue) {
  151. return
  152. }
  153. ret = taskQueue[0]
  154. taskQueue = taskQueue[1:]
  155. return
  156. }
  157. func execTask(task *Task) {
  158. taskLock.Lock()
  159. defer taskLock.Unlock()
  160. defer logging.Recover()
  161. args := make([]reflect.Value, len(task.Args))
  162. for i, v := range task.Args {
  163. if nil == v {
  164. args[i] = reflect.New(task.Handler.Type().In(i)).Elem()
  165. } else {
  166. args[i] = reflect.ValueOf(v)
  167. }
  168. }
  169. task.Handler.Call(args)
  170. }