-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
134 lines (107 loc) · 2.64 KB
/
client.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package conveyor
import (
"context"
"fmt"
"log/slog"
"time"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/jpoz/conveyor/config"
"github.com/jpoz/conveyor/storage"
"github.com/jpoz/conveyor/wire"
)
type Client struct {
handler storage.Handler
cfg config.ClientConfig
}
type Result struct {
Uuid string
}
func NewClient(cfg config.ClientConfig) (*Client, error) {
cfg.SetLogger(cfg.GetLogger().With(slog.String("client", "conveyor")))
handler, err := storage.NewRedisHandler(cfg)
if err != nil {
return nil, fmt.Errorf("failed to create redis handler: %w", err)
}
err = handler.Ping(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to ping redis: %w", err)
}
return &Client{
handler: handler,
cfg: cfg,
}, nil
}
func (c *Client) Close() error {
return c.handler.Close()
}
type JobOption func(job *wire.Job) error
func Delay(delay time.Duration) JobOption {
return func(job *wire.Job) error {
runAt := time.Now().Add(delay)
job.RunAt = timestamppb.New(runAt)
return nil
}
}
func RunAt(t time.Time) JobOption {
return func(job *wire.Job) error {
job.RunAt = timestamppb.New(t)
return nil
}
}
func (c *Client) Enqueue(ctx context.Context, msg proto.Message, opts ...JobOption) (*Result, error) {
payload, err := proto.Marshal(msg)
if err != nil {
return nil, err
}
msgName := proto.MessageName(msg)
job := &wire.Job{
Type: string(msgName),
Queue: string(msgName),
Payload: payload,
}
for _, opt := range opts {
err := opt(job)
if err != nil {
return nil, fmt.Errorf("failed to apply option: %#v %w", opt, err)
}
}
return c.add(ctx, job)
}
func (c *Client) EnqueueChild(ctx context.Context, msg proto.Message) (*Result, error) {
parent := CurrentJob(ctx)
payload, err := proto.Marshal(msg)
if err != nil {
return nil, err
}
msgName := proto.MessageName(msg)
job := &wire.Job{
Type: string(msgName),
Queue: string(msgName),
Payload: payload,
ParentUuid: parent.Uuid,
}
return c.add(ctx, job)
}
func (c *Client) EnqueueHeir(ctx context.Context, msg proto.Message) (*Result, error) {
predecessor := CurrentJob(ctx)
payload, err := proto.Marshal(msg)
if err != nil {
return nil, err
}
msgName := proto.MessageName(msg)
job := &wire.Job{
Type: string(msgName),
Queue: string(msgName),
Payload: payload,
PredecessorUuid: predecessor.Uuid,
}
return c.add(ctx, job)
}
func (c *Client) add(ctx context.Context, job *wire.Job) (*Result, error) {
err := c.handler.AddJob(ctx, job)
if err != nil {
return nil, err
}
return &Result{Uuid: job.Uuid}, nil
}