|
| 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 | + |
| 5 | +package task |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "errors" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "code.gitea.io/gitea/models" |
| 15 | + "code.gitea.io/gitea/modules/log" |
| 16 | + |
| 17 | + "github.com/go-redis/redis" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + _ Queue = &RedisQueue{} |
| 22 | +) |
| 23 | + |
| 24 | +type redisClient interface { |
| 25 | + RPush(key string, args ...interface{}) *redis.IntCmd |
| 26 | + LPop(key string) *redis.StringCmd |
| 27 | + Ping() *redis.StatusCmd |
| 28 | +} |
| 29 | + |
| 30 | +// RedisQueue redis queue |
| 31 | +type RedisQueue struct { |
| 32 | + client redisClient |
| 33 | + queueName string |
| 34 | +} |
| 35 | + |
| 36 | +func parseConnStr(connStr string) (addrs, password string, dbIdx int, err error) { |
| 37 | + fields := strings.Fields(connStr) |
| 38 | + for _, f := range fields { |
| 39 | + items := strings.SplitN(f, "=", 2) |
| 40 | + if len(items) < 2 { |
| 41 | + continue |
| 42 | + } |
| 43 | + switch strings.ToLower(items[0]) { |
| 44 | + case "addrs": |
| 45 | + addrs = items[1] |
| 46 | + case "password": |
| 47 | + password = items[1] |
| 48 | + case "db": |
| 49 | + dbIdx, err = strconv.Atoi(items[1]) |
| 50 | + if err != nil { |
| 51 | + return |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + return |
| 56 | +} |
| 57 | + |
| 58 | +// NewRedisQueue creates single redis or cluster redis queue |
| 59 | +func NewRedisQueue(addrs string, password string, dbIdx int) (*RedisQueue, error) { |
| 60 | + dbs := strings.Split(addrs, ",") |
| 61 | + var queue = RedisQueue{ |
| 62 | + queueName: "task_queue", |
| 63 | + } |
| 64 | + if len(dbs) == 0 { |
| 65 | + return nil, errors.New("no redis host found") |
| 66 | + } else if len(dbs) == 1 { |
| 67 | + queue.client = redis.NewClient(&redis.Options{ |
| 68 | + Addr: strings.TrimSpace(dbs[0]), // use default Addr |
| 69 | + Password: password, // no password set |
| 70 | + DB: dbIdx, // use default DB |
| 71 | + }) |
| 72 | + } else { |
| 73 | + // cluster will ignore db |
| 74 | + queue.client = redis.NewClusterClient(&redis.ClusterOptions{ |
| 75 | + Addrs: dbs, |
| 76 | + Password: password, |
| 77 | + }) |
| 78 | + } |
| 79 | + if err := queue.client.Ping().Err(); err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + return &queue, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (r *RedisQueue) Run() error { |
| 86 | + for { |
| 87 | + bs, err := r.client.LPop(r.queueName).Bytes() |
| 88 | + if err != nil { |
| 89 | + if err != redis.Nil { |
| 90 | + log.Error(4, "LPop failed: %v", err) |
| 91 | + } |
| 92 | + time.Sleep(time.Millisecond * 100) |
| 93 | + continue |
| 94 | + } |
| 95 | + |
| 96 | + var task models.Task |
| 97 | + err = json.Unmarshal(bs, &task) |
| 98 | + if err != nil { |
| 99 | + log.Error(4, "Unmarshal task failed: %s", err.Error()) |
| 100 | + } else { |
| 101 | + err = Run(&task) |
| 102 | + if err != nil { |
| 103 | + log.Error(4, "Run task failed: %s", err.Error()) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + time.Sleep(time.Millisecond * 100) |
| 108 | + } |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +// Push implements Queue |
| 113 | +func (r *RedisQueue) Push(task *models.Task) error { |
| 114 | + bs, err := json.Marshal(task) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + return r.client.RPush(r.queueName, bs).Err() |
| 119 | +} |
0 commit comments