2020-01-07 04:23:09 -07: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 (
|
2021-02-10 14:28:32 -07:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2020-01-07 04:23:09 -07:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-09-27 15:09:46 -06:00
|
|
|
"code.gitea.io/gitea/modules/nosql"
|
2020-01-07 04:23:09 -07:00
|
|
|
|
2021-02-10 14:28:32 -07:00
|
|
|
"github.com/go-redis/redis/v8"
|
2020-01-07 04:23:09 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// RedisQueueType is the type for redis queue
|
|
|
|
const RedisQueueType Type = "redis"
|
|
|
|
|
2020-02-02 16:19:58 -07:00
|
|
|
// RedisQueueConfiguration is the configuration for the redis queue
|
|
|
|
type RedisQueueConfiguration struct {
|
|
|
|
ByteFIFOQueueConfiguration
|
|
|
|
RedisByteFIFOConfiguration
|
|
|
|
}
|
|
|
|
|
|
|
|
// RedisQueue redis queue
|
|
|
|
type RedisQueue struct {
|
|
|
|
*ByteFIFOQueue
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRedisQueue creates single redis or cluster redis queue
|
|
|
|
func NewRedisQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error) {
|
|
|
|
configInterface, err := toConfig(RedisQueueConfiguration{}, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config := configInterface.(RedisQueueConfiguration)
|
|
|
|
|
|
|
|
byteFIFO, err := NewRedisByteFIFO(config.RedisByteFIFOConfiguration)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
byteFIFOQueue, err := NewByteFIFOQueue(RedisQueueType, byteFIFO, handle, config.ByteFIFOQueueConfiguration, exemplar)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
queue := &RedisQueue{
|
|
|
|
ByteFIFOQueue: byteFIFOQueue,
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.qid = GetManager().Add(queue, RedisQueueType, config, exemplar)
|
|
|
|
|
|
|
|
return queue, nil
|
|
|
|
}
|
|
|
|
|
2020-01-07 04:23:09 -07:00
|
|
|
type redisClient interface {
|
2021-02-10 14:28:32 -07:00
|
|
|
RPush(ctx context.Context, key string, args ...interface{}) *redis.IntCmd
|
|
|
|
LPop(ctx context.Context, key string) *redis.StringCmd
|
|
|
|
LLen(ctx context.Context, key string) *redis.IntCmd
|
|
|
|
SAdd(ctx context.Context, key string, members ...interface{}) *redis.IntCmd
|
|
|
|
SRem(ctx context.Context, key string, members ...interface{}) *redis.IntCmd
|
|
|
|
SIsMember(ctx context.Context, key string, member interface{}) *redis.BoolCmd
|
|
|
|
Ping(ctx context.Context) *redis.StatusCmd
|
2020-01-07 04:23:09 -07:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2021-04-09 01:40:34 -06:00
|
|
|
var _ ByteFIFO = &RedisByteFIFO{}
|
2020-02-02 16:19:58 -07:00
|
|
|
|
|
|
|
// RedisByteFIFO represents a ByteFIFO formed from a redisClient
|
|
|
|
type RedisByteFIFO struct {
|
2021-05-15 08:22:26 -06:00
|
|
|
client redisClient
|
|
|
|
|
2020-02-02 16:19:58 -07:00
|
|
|
queueName string
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
|
|
|
|
2020-02-02 16:19:58 -07:00
|
|
|
// RedisByteFIFOConfiguration is the configuration for the RedisByteFIFO
|
|
|
|
type RedisByteFIFOConfiguration struct {
|
2020-09-27 15:09:46 -06:00
|
|
|
ConnectionString string
|
|
|
|
QueueName string
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
|
|
|
|
2020-02-02 16:19:58 -07:00
|
|
|
// NewRedisByteFIFO creates a ByteFIFO formed from a redisClient
|
|
|
|
func NewRedisByteFIFO(config RedisByteFIFOConfiguration) (*RedisByteFIFO, error) {
|
|
|
|
fifo := &RedisByteFIFO{
|
|
|
|
queueName: config.QueueName,
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
2020-09-27 15:09:46 -06:00
|
|
|
fifo.client = nosql.GetManager().GetRedisClient(config.ConnectionString)
|
2021-02-10 14:28:32 -07:00
|
|
|
if err := fifo.client.Ping(graceful.GetManager().ShutdownContext()).Err(); err != nil {
|
2020-01-07 04:23:09 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-02 16:19:58 -07:00
|
|
|
return fifo, nil
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
|
|
|
|
2020-02-02 16:19:58 -07:00
|
|
|
// PushFunc pushes data to the end of the fifo and calls the callback if it is added
|
2021-05-15 08:22:26 -06:00
|
|
|
func (fifo *RedisByteFIFO) PushFunc(ctx context.Context, data []byte, fn func() error) error {
|
2020-02-02 16:19:58 -07:00
|
|
|
if fn != nil {
|
|
|
|
if err := fn(); err != nil {
|
|
|
|
return err
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
|
|
|
}
|
2021-05-15 08:22:26 -06:00
|
|
|
return fifo.client.RPush(ctx, fifo.queueName, data).Err()
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
|
|
|
|
2020-02-02 16:19:58 -07:00
|
|
|
// Pop pops data from the start of the fifo
|
2021-05-15 08:22:26 -06:00
|
|
|
func (fifo *RedisByteFIFO) Pop(ctx context.Context) ([]byte, error) {
|
|
|
|
data, err := fifo.client.LPop(ctx, fifo.queueName).Bytes()
|
2020-02-12 03:12:27 -07:00
|
|
|
if err == nil || err == redis.Nil {
|
2020-02-02 16:19:58 -07:00
|
|
|
return data, nil
|
2020-01-28 18:01:06 -07:00
|
|
|
}
|
2020-02-02 16:19:58 -07:00
|
|
|
return data, err
|
2020-01-28 18:01:06 -07:00
|
|
|
}
|
|
|
|
|
2020-02-02 16:19:58 -07:00
|
|
|
// Close this fifo
|
|
|
|
func (fifo *RedisByteFIFO) Close() error {
|
|
|
|
return fifo.client.Close()
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
|
|
|
|
2020-02-02 16:19:58 -07:00
|
|
|
// Len returns the length of the fifo
|
2021-05-15 08:22:26 -06:00
|
|
|
func (fifo *RedisByteFIFO) Len(ctx context.Context) int64 {
|
|
|
|
val, err := fifo.client.LLen(ctx, fifo.queueName).Result()
|
2020-02-02 16:19:58 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error whilst getting length of redis queue %s: Error: %v", fifo.queueName, err)
|
|
|
|
return -1
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
2020-02-02 16:19:58 -07:00
|
|
|
return val
|
2020-01-07 04:23:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
queuesMap[RedisQueueType] = NewRedisQueue
|
|
|
|
}
|