-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob.go
More file actions
362 lines (332 loc) · 8.12 KB
/
job.go
File metadata and controls
362 lines (332 loc) · 8.12 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package queue
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"time"
)
// Job is a pure queue payload value plus enqueue metadata.
// @group Job
//
// Example: job
//
// job := queue.NewJob("emails:send").
// PayloadJSON(map[string]string{"to": "user@example.com"}).
// OnQueue("critical")
// _ = job
type Job struct {
Type string
payload []byte
options jobOptions
buildErr error
}
type jobOptions struct {
queueName string
timeout *time.Duration
maxRetry *int
attempt int
backoff *time.Duration
delay time.Duration
uniqueTTL time.Duration
}
// DriverJobOptions exposes parsed job enqueue metadata for driver-module implementations.
//
// This is an advanced type intended for optional driver integrations.
// @group Driver Integration
type DriverJobOptions struct {
QueueName string
Timeout *time.Duration
MaxRetry *int
Attempt int
Backoff *time.Duration
Delay time.Duration
UniqueTTL time.Duration
}
// NewJob creates a job value with a required job type.
// @group Job
//
// Example: new job
//
// job := queue.NewJob("emails:send")
// _ = job
func NewJob(jobType string) Job {
return Job{Type: jobType}
}
// Payload sets job payload from common value types.
// @group Job
//
// Example: payload bytes
//
// jobBytes := queue.NewJob("emails:send").Payload([]byte(`{"id":1}`))
// _ = jobBytes
//
// Example: payload struct
//
// type Meta struct {
// Nested bool `json:"nested"`
// }
// type EmailPayload struct {
// ID int `json:"id"`
// To string `json:"to"`
// Meta Meta `json:"meta"`
// }
// jobStruct := queue.NewJob("emails:send").Payload(EmailPayload{
// ID: 1,
// To: "user@example.com",
// Meta: Meta{Nested: true},
// })
// _ = jobStruct
//
// Example: payload map
//
// jobMap := queue.NewJob("emails:send").Payload(map[string]any{
// "id": 1,
// "to": "user@example.com",
// "meta": map[string]any{"nested": true},
// })
// _ = jobMap
func (t Job) Payload(payload any) Job {
encoded, err := encodePayload(payload)
if err != nil {
t.buildErr = err
return t
}
t.payload = encoded
return t
}
// PayloadJSON marshals payload as JSON.
// @group Job
//
// Example: payload json
//
// job := queue.NewJob("emails:send").PayloadJSON(map[string]int{"id": 1})
// _ = job
func (t Job) PayloadJSON(v any) Job {
payload, err := encodePayload(v)
if err != nil {
t.buildErr = err
return t
}
t.payload = payload
return t
}
// OnQueue sets the target queue name.
// @group Job
//
// Example: on queue
//
// job := queue.NewJob("emails:send").OnQueue("critical")
// _ = job
func (t Job) OnQueue(name string) Job {
t.options.queueName = name
return t
}
// Timeout sets per-job execution timeout.
// @group Job
//
// Example: timeout
//
// job := queue.NewJob("emails:send").Timeout(10 * time.Second)
// _ = job
func (t Job) Timeout(timeout time.Duration) Job {
if timeout < 0 {
return t.withBuildErr(fmt.Errorf("timeout must be >= 0"))
}
t.options.timeout = &timeout
return t
}
// Retry sets max retry attempts.
// @group Job
//
// Example: retry
//
// job := queue.NewJob("emails:send").Retry(4)
// _ = job
func (t Job) Retry(maxRetry int) Job {
if maxRetry < 0 {
return t.withBuildErr(fmt.Errorf("retry must be >= 0"))
}
t.options.maxRetry = &maxRetry
return t
}
// Backoff sets delay between retries.
// @group Job
//
// Example: backoff
//
// job := queue.NewJob("emails:send").Backoff(500 * time.Millisecond)
// _ = job
func (t Job) Backoff(backoff time.Duration) Job {
if backoff < 0 {
return t.withBuildErr(fmt.Errorf("backoff must be >= 0"))
}
t.options.backoff = &backoff
return t
}
// Delay defers execution by duration.
// @group Job
//
// Example: delay
//
// job := queue.NewJob("emails:send").Delay(300 * time.Millisecond)
// _ = job
func (t Job) Delay(delay time.Duration) Job {
if delay < 0 {
return t.withBuildErr(fmt.Errorf("delay must be >= 0"))
}
t.options.delay = delay
return t
}
// UniqueFor enables uniqueness dedupe within the given TTL.
// @group Job
//
// Example: unique for
//
// job := queue.NewJob("emails:send").UniqueFor(45 * time.Second)
// _ = job
func (t Job) UniqueFor(ttl time.Duration) Job {
if ttl < 0 {
return t.withBuildErr(fmt.Errorf("unique ttl must be >= 0"))
}
t.options.uniqueTTL = ttl
return t
}
// PayloadBytes returns a copy of job payload bytes.
// @group Job
//
// Example: payload bytes read
//
// job := queue.NewJob("emails:send").Payload([]byte(`{"id":1}`))
// payload := job.PayloadBytes()
// _ = payload
func (t Job) PayloadBytes() []byte {
return append([]byte(nil), t.payload...)
}
// Bind unmarshals job payload JSON into dst.
// @group Job
//
// Example: bind payload
//
// type EmailPayload struct {
// ID int `json:"id"`
// To string `json:"to"`
// }
// job := queue.NewJob("emails:send").Payload(EmailPayload{
// ID: 1,
// To: "user@example.com",
// })
// var payload EmailPayload
// if err := job.Bind(&payload); err != nil {
// return
// }
// _ = payload.To
func (t Job) Bind(dst any) error {
if dst == nil {
return fmt.Errorf("bind destination is required")
}
v := reflect.ValueOf(dst)
if v.Kind() != reflect.Pointer || v.IsNil() {
return fmt.Errorf("bind destination must be a non-nil pointer")
}
if err := json.Unmarshal(t.PayloadBytes(), dst); err != nil {
return fmt.Errorf("bind payload: %w", err)
}
return nil
}
func encodePayload(payload any) ([]byte, error) {
if payload == nil {
return nil, nil
}
switch v := payload.(type) {
case []byte:
return append([]byte(nil), v...), nil
case string:
return []byte(v), nil
case json.RawMessage:
return append([]byte(nil), v...), nil
}
if marshaler, ok := payload.(interface{ MarshalJSON() ([]byte, error) }); ok {
encoded, err := marshaler.MarshalJSON()
if err != nil {
return nil, fmt.Errorf("marshal payload json: %w", err)
}
return encoded, nil
}
encoded, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("marshal payload json: %w", err)
}
return encoded, nil
}
func (t Job) validate() error {
if t.buildErr != nil {
return t.buildErr
}
if t.Type == "" {
return fmt.Errorf("job type is required")
}
return nil
}
func (t Job) jobOptions() jobOptions {
return t.options
}
// ValidateDriverJob validates a job value for backend dispatch.
//
// This is an advanced helper intended for driver-module implementations.
// @group Driver Integration
func ValidateDriverJob(job Job) error {
return job.validate()
}
// DriverOptions returns parsed enqueue metadata for backend dispatch.
//
// This is an advanced helper intended for driver-module implementations.
// @group Driver Integration
func DriverOptions(job Job) DriverJobOptions {
opts := job.jobOptions()
return DriverJobOptions{
QueueName: opts.queueName,
Timeout: opts.timeout,
MaxRetry: opts.maxRetry,
Attempt: opts.attempt,
Backoff: opts.backoff,
Delay: opts.delay,
UniqueTTL: opts.uniqueTTL,
}
}
func (t Job) withBuildErr(err error) Job {
if t.buildErr == nil {
t.buildErr = err
}
return t
}
func (t Job) withAttempt(attempt int) Job {
t.options.attempt = attempt
return t
}
// DriverWithAttempt returns a copy of the job with the attempt number set.
//
// This is an advanced helper intended for driver-module implementations.
// @group Driver Integration
func DriverWithAttempt(job Job, attempt int) Job {
return job.withAttempt(attempt)
}
// Handler processes a job.
// @group Job
//
// Example: handler
//
// handler := func(ctx context.Context, job queue.Job) error { return nil }
// _ = handler
type Handler func(ctx context.Context, job Job) error
// ErrDuplicate indicates a duplicate unique job enqueue.
var ErrDuplicate = errors.New("duplicate job")
// ErrQueuerShuttingDown indicates enqueue was rejected during shutdown.
var ErrQueuerShuttingDown = errors.New("queue is shutting down")
// ErrWorkerpoolQueueNotInitialized indicates workerpool queue is unavailable.
var ErrWorkerpoolQueueNotInitialized = errors.New("workerpool queue not initialized")
// ErrBackoffUnsupported indicates requested backoff is unsupported by a driver.
var ErrBackoffUnsupported = errors.New("backoff option is not supported by this driver")
// ErrQueuePaused indicates enqueue was rejected because queue is paused.
var ErrQueuePaused = errors.New("queue is paused")