-
Notifications
You must be signed in to change notification settings - Fork 0
/
redishandler.go
69 lines (55 loc) · 1.35 KB
/
redishandler.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package storage
import (
context "context"
"fmt"
"log/slog"
"github.com/redis/go-redis/v9"
"google.golang.org/protobuf/proto"
"github.com/jpoz/conveyor/pkg/config"
"github.com/jpoz/conveyor/wire"
)
const DefaultMaxRetries int32 = 3
type RedisHandler struct {
Namespace string
rdb *redis.Client
log *slog.Logger
}
func NewRedisHandler(cfg config.RedisConfig) (Handler, error) {
log := cfg.GetLogger()
opt, err := redis.ParseURL(cfg.GetRedisURL())
if err != nil {
log.Error("failed to parse redis url", slog.Any("error", err))
return nil, fmt.Errorf("RedisHandler failed to parse redis url: %w", err)
}
rdb := redis.NewClient(opt)
return &RedisHandler{
rdb: rdb,
log: log.With(slog.String("handler", "redis")),
Namespace: cfg.GetNamespace(),
}, nil
}
func (s *RedisHandler) setJob(ctx context.Context, uuid string, jobBytes []byte) error {
err := s.rdb.Set(ctx, s.JobKey(uuid), jobBytes, 0).Err()
if err != nil {
return err
}
return nil
}
func (s *RedisHandler) Ping(ctx context.Context) error {
return s.rdb.Ping(ctx).Err()
}
func (s *RedisHandler) Close() error {
return s.rdb.Close()
}
func marshalJob(job *wire.Job) ([]byte, error) {
if job == nil {
return nil, ErrNoJob
}
if job.Queue == "" {
return nil, ErrNoQueue
}
if job.Type == "" {
return nil, ErrNoType
}
return proto.Marshal(job)
}