-
Notifications
You must be signed in to change notification settings - Fork 2
/
group.go
39 lines (31 loc) · 1012 Bytes
/
group.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
package queue
import (
"sync"
)
// Group represents a collection of handlers with specific middleware applied.
type Group struct {
name string
worker *Worker
middlewares []MiddlewareFunc
mu sync.Mutex
}
// Use adds a middleware to the group.
func (g *Group) Use(middlewares ...MiddlewareFunc) {
g.mu.Lock()
defer g.mu.Unlock()
g.middlewares = append(g.middlewares, middlewares...)
}
// Register configures and registers a handler for a specific job type within this group.
func (g *Group) Register(jobType string, handle HandlerFunc, opts ...HandlerOption) error {
if len(g.middlewares) > 0 {
opts = append([]HandlerOption{WithMiddleware(g.middlewares...)}, opts...)
}
return g.worker.Register(jobType, handle, opts...)
}
// RegisterHandler registers a handler for a specific job type within this group.
func (g *Group) RegisterHandler(handler *Handler) error {
if len(g.middlewares) > 0 {
handler.Use(g.middlewares...)
}
return g.worker.RegisterHandler(handler)
}