-
Notifications
You must be signed in to change notification settings - Fork 36
/
producer.go
147 lines (122 loc) · 4.07 KB
/
producer.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
135
136
137
138
139
140
141
142
143
144
145
146
147
package workers
import (
"context"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"time"
"github.com/go-redis/redis/v8"
)
const (
// NanoSecondPrecision is a constant for the number of nanoseconds in a second
NanoSecondPrecision = 1000000000.0
)
// Producer is used to enqueue new work
type Producer struct {
opts Options
}
// EnqueueData stores data and configuration for new work
type EnqueueData struct {
Queue string `json:"queue,omitempty"`
Class string `json:"class"`
Args interface{} `json:"args"`
Jid string `json:"jid"`
EnqueuedAt float64 `json:"enqueued_at"`
EnqueueOptions
}
// EnqueueOptions stores configuration for new work
type EnqueueOptions struct {
RetryCount int `json:"retry_count,omitempty"`
RetryMax int `json:"retry_max,omitempty"`
Retry bool `json:"retry,omitempty"`
At float64 `json:"at,omitempty"`
}
// NewProducer creates a new producer with the given options
func NewProducer(options Options) (*Producer, error) {
options, err := processOptions(options)
if err != nil {
return nil, err
}
return &Producer{
opts: options,
}, nil
}
// NewProducerWithRedisClient creates a new producer with the given options and Redis client
func NewProducerWithRedisClient(options Options, client *redis.Client) (*Producer, error) {
options, err := processOptionsWithRedisClient(options, client)
if err != nil {
return nil, err
}
return &Producer{
opts: options,
}, nil
}
// GetRedisClient returns the Redis client used by the producer
// Deprecated: the Redis client is an internal implementation and access will be removed
func (p *Producer) GetRedisClient() *redis.Client {
return p.opts.client
}
// Enqueue enqueues new work for immediate processing
func (p *Producer) Enqueue(queue, class string, args interface{}) (string, error) {
return p.EnqueueWithOptions(queue, class, args, EnqueueOptions{At: nowToSecondsWithNanoPrecision()})
}
// EnqueueIn enqueues new work for delayed processing
func (p *Producer) EnqueueIn(queue, class string, in float64, args interface{}) (string, error) {
return p.EnqueueWithOptions(queue, class, args, EnqueueOptions{At: nowToSecondsWithNanoPrecision() + in})
}
// EnqueueAt enqueues new work for processing at a specific time
func (p *Producer) EnqueueAt(queue, class string, at time.Time, args interface{}) (string, error) {
return p.EnqueueWithOptions(queue, class, args, EnqueueOptions{At: timeToSecondsWithNanoPrecision(at)})
}
// EnqueueWithOptions enqueues new work for processing with the given options
func (p *Producer) EnqueueWithOptions(queue, class string, args interface{}, opts EnqueueOptions) (string, error) {
return p.EnqueueWithContext(context.Background(), queue, class, args, opts)
}
// EnqueueWithContext enqueues new work for processing with the given options and context
func (p *Producer) EnqueueWithContext(ctx context.Context, queue, class string, args interface{}, opts EnqueueOptions) (string, error) {
now := nowToSecondsWithNanoPrecision()
data := EnqueueData{
Queue: queue,
Class: class,
Args: args,
Jid: generateJid(),
EnqueuedAt: now,
EnqueueOptions: opts,
}
bytes, err := json.Marshal(data)
if err != nil {
return "", err
}
if now < opts.At {
err = p.opts.store.EnqueueScheduledMessage(ctx, data.At, string(bytes))
return data.Jid, err
}
err = p.opts.store.CreateQueue(ctx, queue)
if err != nil {
return "", err
}
err = p.opts.store.EnqueueMessageNow(ctx, queue, string(bytes))
if err != nil {
return "", err
}
return data.Jid, nil
}
func timeToSecondsWithNanoPrecision(t time.Time) float64 {
return float64(t.UnixNano()) / NanoSecondPrecision
}
func durationToSecondsWithNanoPrecision(d time.Duration) float64 {
return float64(d.Nanoseconds()) / NanoSecondPrecision
}
func nowToSecondsWithNanoPrecision() float64 {
return timeToSecondsWithNanoPrecision(time.Now())
}
func generateJid() string {
// Return 12 random bytes as 24 character hex
b := make([]byte, 12)
_, err := io.ReadFull(rand.Reader, b)
if err != nil {
return ""
}
return fmt.Sprintf("%x", b)
}