-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.go
More file actions
485 lines (455 loc) · 12.1 KB
/
admin.go
File metadata and controls
485 lines (455 loc) · 12.1 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package queue
import (
"context"
"errors"
"fmt"
"time"
)
// JobState identifies queue job state used by queue admin APIs.
// @group Admin
type JobState string
const (
JobStatePending JobState = "pending"
JobStateActive JobState = "active"
JobStateScheduled JobState = "scheduled"
JobStateRetry JobState = "retry"
JobStateArchived JobState = "archived"
JobStateCompleted JobState = "completed"
)
// QueueHistoryWindow identifies queue history horizon.
// @group Admin
type QueueHistoryWindow string
const (
QueueHistoryHour QueueHistoryWindow = "hour"
QueueHistoryDay QueueHistoryWindow = "day"
QueueHistoryWeek QueueHistoryWindow = "week"
)
// ListJobsOptions configures queue admin list jobs calls.
// @group Admin
type ListJobsOptions struct {
Queue string
State JobState
Page int
PageSize int
}
// Normalize returns a safe options payload with defaults applied.
// @group Admin
//
// Example: normalize list options
//
// opts := queue.ListJobsOptions{Queue: "", State: "", Page: 0, PageSize: 1000}
// normalized := opts.Normalize()
// fmt.Println(normalized.Queue, normalized.State, normalized.Page, normalized.PageSize)
// // Output: default pending 1 500
func (o ListJobsOptions) Normalize() ListJobsOptions {
if o.Queue == "" {
o.Queue = "default"
}
if o.State == "" {
o.State = JobStatePending
}
if o.Page <= 0 {
o.Page = 1
}
if o.PageSize <= 0 {
o.PageSize = 50
}
if o.PageSize > 500 {
o.PageSize = 500
}
return o
}
// JobSnapshot describes an admin-facing queue job record.
// @group Admin
type JobSnapshot struct {
ID string
Queue string
State JobState
Type string
Payload string
Attempt int
MaxRetry int
LastError string
NextProcessAt *time.Time
CompletedAt *time.Time
}
// ListJobsResult contains queue admin job listing output.
// @group Admin
type ListJobsResult struct {
Jobs []JobSnapshot
Total int64
}
// QueueHistoryPoint represents processed/failed totals at a point in time.
// @group Admin
type QueueHistoryPoint struct {
At time.Time
Processed int64
Failed int64
}
// QueueAdmin exposes optional queue admin capabilities.
// @group Admin
type QueueAdmin interface {
ListJobs(ctx context.Context, opts ListJobsOptions) (ListJobsResult, error)
RetryJob(ctx context.Context, queueName, jobID string) error
CancelJob(ctx context.Context, jobID string) error
DeleteJob(ctx context.Context, queueName, jobID string) error
ClearQueue(ctx context.Context, queueName string) error
History(ctx context.Context, queueName string, window QueueHistoryWindow) ([]QueueHistoryPoint, error)
}
// QueueHistoryProvider exposes queue history capability independently from full queue admin support.
// Drivers like sync/workerpool can provide history without supporting per-job admin operations.
// @group Admin
type QueueHistoryProvider interface {
History(ctx context.Context, queueName string, window QueueHistoryWindow) ([]QueueHistoryPoint, error)
}
// ErrQueueAdminUnsupported indicates queue admin operations are unsupported by a driver.
// @group Admin
var ErrQueueAdminUnsupported = errors.New("queue admin is not supported by this driver")
// SupportsQueueAdmin reports whether queue admin operations are available.
// @group Admin
//
// Example: detect admin support
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// fmt.Println(queue.SupportsQueueAdmin(q))
// // Output: true
func SupportsQueueAdmin(q any) bool {
return resolveQueueAdmin(q) != nil
}
// ListJobs lists jobs for a queue and state when supported.
// @group Admin
//
// Example: list jobs via helper
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// _, err = queue.ListJobs(context.Background(), q, queue.ListJobsOptions{
// Queue: "default",
// State: queue.JobStatePending,
// })
// _ = err
func ListJobs(ctx context.Context, q any, opts ListJobsOptions) (ListJobsResult, error) {
admin := resolveQueueAdmin(q)
if admin == nil {
return ListJobsResult{}, ErrQueueAdminUnsupported
}
return admin.ListJobs(ctx, opts)
}
// RetryJob retries (runs now) a job when supported.
// @group Admin
//
// Example: retry a job via helper
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// err = queue.RetryJob(context.Background(), q, "default", "job-id")
// _ = err
func RetryJob(ctx context.Context, q any, queueName, jobID string) error {
admin := resolveQueueAdmin(q)
if admin == nil {
return ErrQueueAdminUnsupported
}
return admin.RetryJob(ctx, queueName, jobID)
}
// CancelJob cancels a job when supported.
// @group Admin
//
// Example: cancel a job via helper
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// err = queue.CancelJob(context.Background(), q, "job-id")
// _ = err
func CancelJob(ctx context.Context, q any, jobID string) error {
admin := resolveQueueAdmin(q)
if admin == nil {
return ErrQueueAdminUnsupported
}
return admin.CancelJob(ctx, jobID)
}
// DeleteJob deletes a job when supported.
// @group Admin
//
// Example: delete a job via helper
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// err = queue.DeleteJob(context.Background(), q, "default", "job-id")
// _ = err
func DeleteJob(ctx context.Context, q any, queueName, jobID string) error {
admin := resolveQueueAdmin(q)
if admin == nil {
return ErrQueueAdminUnsupported
}
return admin.DeleteJob(ctx, queueName, jobID)
}
// ClearQueue clears queue jobs when supported.
// @group Admin
//
// Example: clear queue via helper
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// err = queue.ClearQueue(context.Background(), q, "default")
// _ = err
func ClearQueue(ctx context.Context, q any, queueName string) error {
admin := resolveQueueAdmin(q)
if admin == nil {
return ErrQueueAdminUnsupported
}
return admin.ClearQueue(ctx, queueName)
}
// QueueHistory returns queue history points when supported.
// @group Admin
//
// Example: history via helper
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// _, err = queue.QueueHistory(context.Background(), q, "default", queue.QueueHistoryHour)
// _ = err
func QueueHistory(ctx context.Context, q any, queueName string, window QueueHistoryWindow) ([]QueueHistoryPoint, error) {
history := resolveQueueHistory(q)
if history == nil {
return nil, ErrQueueAdminUnsupported
}
return history.History(ctx, queueName, window)
}
func resolveQueueAdmin(v any) QueueAdmin {
raw := resolveQueueRuntime(v)
if raw == nil {
return nil
}
if admin, ok := raw.(QueueAdmin); ok {
return admin
}
switch rt := raw.(type) {
case *nativeQueueRuntime:
if rt == nil || rt.common == nil {
return nil
}
if admin, ok := rt.common.inner.(QueueAdmin); ok {
return admin
}
case *externalQueueRuntime:
if rt == nil || rt.common == nil {
return nil
}
if admin, ok := rt.common.inner.(QueueAdmin); ok {
return admin
}
}
return nil
}
func resolveQueueHistory(v any) QueueHistoryProvider {
raw := resolveQueueRuntime(v)
if raw == nil {
return nil
}
if history, ok := raw.(QueueHistoryProvider); ok {
return history
}
switch rt := raw.(type) {
case *nativeQueueRuntime:
if rt == nil || rt.common == nil {
return nil
}
if history, ok := rt.common.inner.(QueueHistoryProvider); ok {
return history
}
case *externalQueueRuntime:
if rt == nil || rt.common == nil {
return nil
}
if history, ok := rt.common.inner.(QueueHistoryProvider); ok {
return history
}
}
return nil
}
// ListJobs lists jobs via queue admin capability when supported.
// @group Admin
//
// Example: queue method list jobs
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// _, err = q.ListJobs(context.Background(), queue.ListJobsOptions{
// Queue: "default",
// State: queue.JobStatePending,
// })
// _ = err
func (r *Queue) ListJobs(ctx context.Context, opts ListJobsOptions) (ListJobsResult, error) {
if r == nil || r.q == nil {
return ListJobsResult{}, fmt.Errorf("runtime is nil")
}
admin := resolveQueueAdmin(r)
if admin == nil {
return ListJobsResult{}, ErrQueueAdminUnsupported
}
return admin.ListJobs(ctx, opts)
}
// RetryJob retries (runs now) a job via queue admin capability when supported.
// @group Admin
//
// Example: queue method retry job
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// if !queue.SupportsQueueAdmin(q) {
// return
// }
// err = q.RetryJob(context.Background(), "default", "job-id")
// _ = err
func (r *Queue) RetryJob(ctx context.Context, queueName, jobID string) error {
if r == nil || r.q == nil {
return fmt.Errorf("runtime is nil")
}
admin := resolveQueueAdmin(r)
if admin == nil {
return ErrQueueAdminUnsupported
}
return admin.RetryJob(ctx, queueName, jobID)
}
// CancelJob cancels a job via queue admin capability when supported.
// @group Admin
//
// Example: queue method cancel job
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// if !queue.SupportsQueueAdmin(q) {
// return
// }
// err = q.CancelJob(context.Background(), "job-id")
// _ = err
func (r *Queue) CancelJob(ctx context.Context, jobID string) error {
if r == nil || r.q == nil {
return fmt.Errorf("runtime is nil")
}
admin := resolveQueueAdmin(r)
if admin == nil {
return ErrQueueAdminUnsupported
}
return admin.CancelJob(ctx, jobID)
}
// DeleteJob deletes a job via queue admin capability when supported.
// @group Admin
//
// Example: queue method delete job
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// if !queue.SupportsQueueAdmin(q) {
// return
// }
// err = q.DeleteJob(context.Background(), "default", "job-id")
// _ = err
func (r *Queue) DeleteJob(ctx context.Context, queueName, jobID string) error {
if r == nil || r.q == nil {
return fmt.Errorf("runtime is nil")
}
admin := resolveQueueAdmin(r)
if admin == nil {
return ErrQueueAdminUnsupported
}
return admin.DeleteJob(ctx, queueName, jobID)
}
// ClearQueue clears queue jobs via queue admin capability when supported.
// @group Admin
//
// Example: queue method clear queue
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// if !queue.SupportsQueueAdmin(q) {
// return
// }
// err = q.ClearQueue(context.Background(), "default")
// _ = err
func (r *Queue) ClearQueue(ctx context.Context, queueName string) error {
if r == nil || r.q == nil {
return fmt.Errorf("runtime is nil")
}
admin := resolveQueueAdmin(r)
if admin == nil {
return ErrQueueAdminUnsupported
}
return admin.ClearQueue(ctx, queueName)
}
// History returns queue history points via queue admin capability when supported.
// @group Admin
//
// Example: queue method history
//
// q, err := redisqueue.New("127.0.0.1:6379")
// if err != nil {
// return
// }
// points, err := q.History(context.Background(), "default", queue.QueueHistoryHour)
// _ = points
// _ = err
func (r *Queue) History(ctx context.Context, queueName string, window QueueHistoryWindow) ([]QueueHistoryPoint, error) {
if r == nil || r.q == nil {
return nil, fmt.Errorf("runtime is nil")
}
history := resolveQueueHistory(r)
if history == nil {
return nil, ErrQueueAdminUnsupported
}
return history.History(ctx, queueName, window)
}
func singlePointHistory(snapshot StatsSnapshot, queueName string) []QueueHistoryPoint {
queueName = normalizeQueueName(queueName)
counters, ok := snapshot.Queue(queueName)
if !ok {
return nil
}
return []QueueHistoryPoint{{
At: time.Now(),
Processed: counters.Processed,
Failed: counters.Failed,
}}
}
// SinglePointHistory converts a snapshot into a single current-history point.
// This helper is intended for driver modules that do not expose historical buckets.
// @group Admin
//
// Example: single-point history
//
// snapshot := queue.StatsSnapshot{
// ByQueue: map[string]queue.QueueCounters{
// "default": {Processed: 12, Failed: 1},
// },
// }
// points := queue.SinglePointHistory(snapshot, "default")
// fmt.Println(len(points), points[0].Processed, points[0].Failed)
// // Output: 1 12 1
func SinglePointHistory(snapshot StatsSnapshot, queueName string) []QueueHistoryPoint {
return singlePointHistory(snapshot, queueName)
}