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_channel.go 949B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 task
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/log"
  8. )
  9. var (
  10. _ Queue = &ChannelQueue{}
  11. )
  12. // ChannelQueue implements
  13. type ChannelQueue struct {
  14. queue chan *models.Task
  15. }
  16. // NewChannelQueue create a memory channel queue
  17. func NewChannelQueue(queueLen int) *ChannelQueue {
  18. return &ChannelQueue{
  19. queue: make(chan *models.Task, queueLen),
  20. }
  21. }
  22. // Run starts to run the queue
  23. func (c *ChannelQueue) Run() error {
  24. for task := range c.queue {
  25. err := Run(task)
  26. if err != nil {
  27. log.Error("Run task failed: %s", err.Error())
  28. }
  29. }
  30. return nil
  31. }
  32. // Push will push the task ID to queue
  33. func (c *ChannelQueue) Push(task *models.Task) error {
  34. c.queue <- task
  35. return nil
  36. }
  37. // Stop stop the queue
  38. func (c *ChannelQueue) Stop() {
  39. close(c.queue)
  40. }