You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

queue_disk.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package queue
  5. import (
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "sync"
  10. "sync/atomic"
  11. "time"
  12. "code.gitea.io/gitea/modules/log"
  13. "gitea.com/lunny/levelqueue"
  14. )
  15. // LevelQueueType is the type for level queue
  16. const LevelQueueType Type = "level"
  17. // LevelQueueConfiguration is the configuration for a LevelQueue
  18. type LevelQueueConfiguration struct {
  19. WorkerPoolConfiguration
  20. DataDir string
  21. Workers int
  22. Name string
  23. }
  24. // LevelQueue implements a disk library queue
  25. type LevelQueue struct {
  26. *WorkerPool
  27. queue *levelqueue.Queue
  28. closed chan struct{}
  29. terminated chan struct{}
  30. lock sync.Mutex
  31. exemplar interface{}
  32. workers int
  33. name string
  34. }
  35. // NewLevelQueue creates a ledis local queue
  36. func NewLevelQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error) {
  37. configInterface, err := toConfig(LevelQueueConfiguration{}, cfg)
  38. if err != nil {
  39. return nil, err
  40. }
  41. config := configInterface.(LevelQueueConfiguration)
  42. internal, err := levelqueue.Open(config.DataDir)
  43. if err != nil {
  44. return nil, err
  45. }
  46. queue := &LevelQueue{
  47. WorkerPool: NewWorkerPool(handle, config.WorkerPoolConfiguration),
  48. queue: internal,
  49. exemplar: exemplar,
  50. closed: make(chan struct{}),
  51. terminated: make(chan struct{}),
  52. workers: config.Workers,
  53. name: config.Name,
  54. }
  55. queue.qid = GetManager().Add(queue, LevelQueueType, config, exemplar)
  56. return queue, nil
  57. }
  58. // Run starts to run the queue
  59. func (l *LevelQueue) Run(atShutdown, atTerminate func(context.Context, func())) {
  60. atShutdown(context.Background(), l.Shutdown)
  61. atTerminate(context.Background(), l.Terminate)
  62. log.Debug("LevelQueue: %s Starting", l.name)
  63. go func() {
  64. _ = l.AddWorkers(l.workers, 0)
  65. }()
  66. go l.readToChan()
  67. log.Trace("LevelQueue: %s Waiting til closed", l.name)
  68. <-l.closed
  69. log.Trace("LevelQueue: %s Waiting til done", l.name)
  70. l.Wait()
  71. log.Trace("LevelQueue: %s Waiting til cleaned", l.name)
  72. ctx, cancel := context.WithCancel(context.Background())
  73. atTerminate(ctx, cancel)
  74. l.CleanUp(ctx)
  75. cancel()
  76. log.Trace("LevelQueue: %s Cleaned", l.name)
  77. }
  78. func (l *LevelQueue) readToChan() {
  79. for {
  80. select {
  81. case <-l.closed:
  82. // tell the pool to shutdown.
  83. l.cancel()
  84. return
  85. default:
  86. atomic.AddInt64(&l.numInQueue, 1)
  87. bs, err := l.queue.RPop()
  88. if err != nil {
  89. if err != levelqueue.ErrNotFound {
  90. log.Error("LevelQueue: %s Error on RPop: %v", l.name, err)
  91. }
  92. atomic.AddInt64(&l.numInQueue, -1)
  93. time.Sleep(time.Millisecond * 100)
  94. continue
  95. }
  96. if len(bs) == 0 {
  97. atomic.AddInt64(&l.numInQueue, -1)
  98. time.Sleep(time.Millisecond * 100)
  99. continue
  100. }
  101. data, err := unmarshalAs(bs, l.exemplar)
  102. if err != nil {
  103. log.Error("LevelQueue: %s Failed to unmarshal with error: %v", l.name, err)
  104. atomic.AddInt64(&l.numInQueue, -1)
  105. time.Sleep(time.Millisecond * 100)
  106. continue
  107. }
  108. log.Trace("LevelQueue %s: Task found: %#v", l.name, data)
  109. l.WorkerPool.Push(data)
  110. atomic.AddInt64(&l.numInQueue, -1)
  111. }
  112. }
  113. }
  114. // Push will push the indexer data to queue
  115. func (l *LevelQueue) Push(data Data) error {
  116. if !assignableTo(data, l.exemplar) {
  117. return fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in %s", data, l.exemplar, l.name)
  118. }
  119. bs, err := json.Marshal(data)
  120. if err != nil {
  121. return err
  122. }
  123. return l.queue.LPush(bs)
  124. }
  125. // IsEmpty checks whether the queue is empty
  126. func (l *LevelQueue) IsEmpty() bool {
  127. if !l.WorkerPool.IsEmpty() {
  128. return false
  129. }
  130. return l.queue.Len() == 0
  131. }
  132. // Shutdown this queue and stop processing
  133. func (l *LevelQueue) Shutdown() {
  134. l.lock.Lock()
  135. defer l.lock.Unlock()
  136. log.Trace("LevelQueue: %s Shutting down", l.name)
  137. select {
  138. case <-l.closed:
  139. default:
  140. close(l.closed)
  141. }
  142. log.Debug("LevelQueue: %s Shutdown", l.name)
  143. }
  144. // Terminate this queue and close the queue
  145. func (l *LevelQueue) Terminate() {
  146. log.Trace("LevelQueue: %s Terminating", l.name)
  147. l.Shutdown()
  148. l.lock.Lock()
  149. select {
  150. case <-l.terminated:
  151. l.lock.Unlock()
  152. default:
  153. close(l.terminated)
  154. l.lock.Unlock()
  155. if log.IsDebug() {
  156. log.Debug("LevelQueue: %s Closing with %d tasks left in queue", l.name, l.queue.Len())
  157. }
  158. if err := l.queue.Close(); err != nil && err.Error() != "leveldb: closed" {
  159. log.Error("Error whilst closing internal queue in %s: %v", l.name, err)
  160. }
  161. }
  162. log.Debug("LevelQueue: %s Terminated", l.name)
  163. }
  164. // Name returns the name of this queue
  165. func (l *LevelQueue) Name() string {
  166. return l.name
  167. }
  168. func init() {
  169. queuesMap[LevelQueueType] = NewLevelQueue
  170. }