aboutsummaryrefslogtreecommitdiffstats
path: root/modules/task/queue_channel.go
blob: da541f47551f5d52c266b3021fc26621d0adaf1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package task

import (
	"code.gitea.io/gitea/models"
	"code.gitea.io/gitea/modules/log"
)

var (
	_ Queue = &ChannelQueue{}
)

// ChannelQueue implements
type ChannelQueue struct {
	queue chan *models.Task
}

// NewChannelQueue create a memory channel queue
func NewChannelQueue(queueLen int) *ChannelQueue {
	return &ChannelQueue{
		queue: make(chan *models.Task, queueLen),
	}
}

// Run starts to run the queue
func (c *ChannelQueue) Run() error {
	for task := range c.queue {
		err := Run(task)
		if err != nil {
			log.Error("Run task failed: %s", err.Error())
		}
	}
	return nil
}

// Push will push the task ID to queue
func (c *ChannelQueue) Push(task *models.Task) error {
	c.queue <- task
	return nil
}

// Stop stop the queue
func (c *ChannelQueue) Stop() {
	close(c.queue)
}