-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
60 lines (48 loc) · 1 KB
/
worker.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
package jobworker
import "context"
type WorkerFunc func(job *Job) error
type Worker interface {
Work(*Job) error
}
type defaultWorker struct {
workFunc func(*Job) error
}
func (w *defaultWorker) Work(job *Job) error {
if w.workFunc != nil {
return w.workFunc(job)
}
return nil
}
type subWorker struct {
id string
*JobWorker
}
func (sw *subWorker) work(jobs <-chan *Job) {
for job := range jobs {
sw.trackJob(job, true)
sw.WorkOnceSafely(context.Background(), job)
sw.trackJob(job, false)
}
}
type workerWithOption struct {
worker Worker
opt *Option
}
type Option struct {
SubscribeMetadata map[string]string
}
type OptionFunc func(*Option)
func (o *Option) ApplyOptions(opts ...OptionFunc) {
for _, opt := range opts {
opt(o)
}
}
// SubscribeMetadata is metadata of subscribe func
func SubscribeMetadata(k, v string) OptionFunc {
return func(opt *Option) {
if opt.SubscribeMetadata == nil {
opt.SubscribeMetadata = make(map[string]string)
}
opt.SubscribeMetadata[k] = v
}
}