queue.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. )
  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. taskQueue = append([]*Task{newTask(action, handler, args...)}, taskQueue...)
  47. }
  48. func AppendTask(action string, handler interface{}, args ...interface{}) {
  49. queueLock.Lock()
  50. defer queueLock.Unlock()
  51. if QueueStatusRunning != taskQueueStatus {
  52. //logging.LogWarnf("task queue is paused, action [%s] will be ignored", action)
  53. return
  54. }
  55. taskQueue = append(taskQueue, newTask(action, handler, args...))
  56. }
  57. func CancelTask(actions ...string) {
  58. queueLock.Lock()
  59. defer queueLock.Unlock()
  60. for i := len(taskQueue) - 1; i >= 0; i-- {
  61. task := taskQueue[i]
  62. for _, action := range actions {
  63. if action == task.Action {
  64. taskQueue = append(taskQueue[:i], taskQueue[i+1:]...)
  65. break
  66. }
  67. }
  68. }
  69. }
  70. func newTask(action string, handler interface{}, args ...interface{}) *Task {
  71. return &Task{
  72. Action: action,
  73. Handler: reflect.ValueOf(handler),
  74. Args: args,
  75. Created: time.Now(),
  76. }
  77. }
  78. const (
  79. RepoCheckout = "task.repo.checkout" // 从快照中检出
  80. DatabaseIndexFull = "task.database.index.full" // 重建索引
  81. DatabaseIndex = "task.database.index" // 数据库索引
  82. DatabaseIndexCommit = "task.database.index.commit" // 数据库索引提交
  83. DatabaseIndexRef = "task.database.index.ref" // 数据库索引引用
  84. DatabaseIndexFix = "task.database.index.fix" // 数据库索引订正
  85. OCRImage = "task.ocr.image" // 图片 OCR 提取文本
  86. HistoryGenerateDoc = "task.history.generateDoc" // 生成文件历史
  87. DatabaseIndexEmbedBlock = "task.database.index.embedBlock" // 数据库索引嵌入块
  88. )
  89. func StatusLoop() {
  90. for {
  91. time.Sleep(5 * time.Second)
  92. tasks := taskQueue
  93. data := map[string]interface{}{}
  94. var items []map[string]interface{}
  95. for _, task := range tasks {
  96. if OCRImage == task.Action || DatabaseIndexEmbedBlock == task.Action {
  97. continue
  98. }
  99. actionLangs := util.TaskActionLangs[util.Lang]
  100. action := task.Action
  101. if nil != actionLangs {
  102. if label := actionLangs[task.Action]; nil != label {
  103. action = label.(string)
  104. }
  105. }
  106. item := map[string]interface{}{
  107. "action": action,
  108. }
  109. items = append(items, item)
  110. }
  111. if 1 > len(items) {
  112. items = []map[string]interface{}{}
  113. }
  114. data["tasks"] = items
  115. util.PushBackgroundTask(data)
  116. if 0 < len(tasks) {
  117. time.Sleep(5 * time.Second)
  118. }
  119. }
  120. }
  121. var taskWaitGroup = sync.WaitGroup{}
  122. func Loop() {
  123. for {
  124. time.Sleep(10 * time.Millisecond)
  125. if QueueStatusClosing == taskQueueStatus {
  126. clearQueue()
  127. break
  128. }
  129. task := popTask()
  130. if nil == task {
  131. continue
  132. }
  133. if util.IsExiting {
  134. break
  135. }
  136. taskWaitGroup.Add(1)
  137. go execTask(task)
  138. taskWaitGroup.Wait()
  139. }
  140. }
  141. func clearQueue() {
  142. queueLock.Lock()
  143. defer queueLock.Unlock()
  144. taskQueue = []*Task{}
  145. }
  146. func popTask() (ret *Task) {
  147. queueLock.Lock()
  148. defer queueLock.Unlock()
  149. if 0 == len(taskQueue) {
  150. return
  151. }
  152. ret = taskQueue[0]
  153. taskQueue = taskQueue[1:]
  154. return
  155. }
  156. func execTask(task *Task) {
  157. defer logging.Recover()
  158. args := make([]reflect.Value, len(task.Args))
  159. for i, v := range task.Args {
  160. if nil == v {
  161. args[i] = reflect.New(task.Handler.Type().In(i)).Elem()
  162. } else {
  163. args[i] = reflect.ValueOf(v)
  164. }
  165. }
  166. task.Handler.Call(args)
  167. taskWaitGroup.Done()
  168. }