2020-01-07 19:23:09 +08:00
|
|
|
// 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 queue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChannelQueueType is the type for channel queue
|
|
|
|
const ChannelQueueType Type = "channel"
|
|
|
|
|
|
|
|
// ChannelQueueConfiguration is the configuration for a ChannelQueue
|
|
|
|
type ChannelQueueConfiguration struct {
|
2020-01-29 09:01:06 +08:00
|
|
|
WorkerPoolConfiguration
|
|
|
|
Workers int
|
|
|
|
Name string
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
|
|
|
|
2020-01-29 09:01:06 +08:00
|
|
|
// ChannelQueue implements Queue
|
|
|
|
//
|
|
|
|
// A channel queue is not persistable and does not shutdown or terminate cleanly
|
|
|
|
// It is basically a very thin wrapper around a WorkerPool
|
2020-01-07 19:23:09 +08:00
|
|
|
type ChannelQueue struct {
|
2020-01-29 09:01:06 +08:00
|
|
|
*WorkerPool
|
2020-01-07 19:23:09 +08:00
|
|
|
exemplar interface{}
|
|
|
|
workers int
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2020-01-29 09:01:06 +08:00
|
|
|
// NewChannelQueue creates a memory channel queue
|
2020-01-07 19:23:09 +08:00
|
|
|
func NewChannelQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error) {
|
|
|
|
configInterface, err := toConfig(ChannelQueueConfiguration{}, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config := configInterface.(ChannelQueueConfiguration)
|
|
|
|
if config.BatchLength == 0 {
|
|
|
|
config.BatchLength = 1
|
|
|
|
}
|
|
|
|
queue := &ChannelQueue{
|
2020-01-29 09:01:06 +08:00
|
|
|
WorkerPool: NewWorkerPool(handle, config.WorkerPoolConfiguration),
|
|
|
|
exemplar: exemplar,
|
|
|
|
workers: config.Workers,
|
|
|
|
name: config.Name,
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
2020-01-29 09:01:06 +08:00
|
|
|
queue.qid = GetManager().Add(queue, ChannelQueueType, config, exemplar)
|
2020-01-07 19:23:09 +08:00
|
|
|
return queue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts to run the queue
|
2020-02-03 07:19:58 +08:00
|
|
|
func (q *ChannelQueue) Run(atShutdown, atTerminate func(context.Context, func())) {
|
2020-01-07 19:23:09 +08:00
|
|
|
atShutdown(context.Background(), func() {
|
2020-02-03 07:19:58 +08:00
|
|
|
log.Warn("ChannelQueue: %s is not shutdownable!", q.name)
|
2020-01-07 19:23:09 +08:00
|
|
|
})
|
|
|
|
atTerminate(context.Background(), func() {
|
2020-02-03 07:19:58 +08:00
|
|
|
log.Warn("ChannelQueue: %s is not terminatable!", q.name)
|
2020-01-07 19:23:09 +08:00
|
|
|
})
|
2020-02-03 07:19:58 +08:00
|
|
|
log.Debug("ChannelQueue: %s Starting", q.name)
|
2020-01-07 19:23:09 +08:00
|
|
|
go func() {
|
2020-02-03 07:19:58 +08:00
|
|
|
_ = q.AddWorkers(q.workers, 0)
|
2020-01-07 19:23:09 +08:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push will push data into the queue
|
2020-02-03 07:19:58 +08:00
|
|
|
func (q *ChannelQueue) Push(data Data) error {
|
|
|
|
if !assignableTo(data, q.exemplar) {
|
|
|
|
return fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in queue: %s", data, q.exemplar, q.name)
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
2020-02-03 07:19:58 +08:00
|
|
|
q.WorkerPool.Push(data)
|
2020-01-07 19:23:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of this queue
|
2020-02-03 07:19:58 +08:00
|
|
|
func (q *ChannelQueue) Name() string {
|
|
|
|
return q.name
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
queuesMap[ChannelQueueType] = NewChannelQueue
|
|
|
|
}
|