-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworker.go
110 lines (91 loc) · 2.18 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
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
package worker
import (
"fmt"
"time"
"golang.org/x/net/context"
"github.com/golang/protobuf/proto"
"github.com/guilhermehubner/worker/broker"
"github.com/guilhermehubner/worker/log"
"github.com/jpillora/backoff"
)
const (
minBackoffTime = 100 * time.Millisecond
maxBackoffTime = 10 * time.Second
)
type getJobHandle func() (*broker.Message, *JobType, error)
type worker struct {
middlewares []Middleware
getJob getJobHandle
cancel chan struct{}
ended chan struct{}
backoff *backoff.Backoff
getMessageBackoff *backoff.Backoff
}
func newWorker(middlewares []Middleware, getJob getJobHandle) *worker {
return &worker{
middlewares: middlewares,
getJob: getJob,
cancel: make(chan struct{}),
ended: make(chan struct{}),
backoff: &backoff.Backoff{
Min: minBackoffTime,
Max: maxBackoffTime,
},
getMessageBackoff: &backoff.Backoff{
Min: minBackoffTime,
Max: maxBackoffTime,
},
}
}
func (w *worker) start() chan struct{} {
go func() {
for {
select {
case <-w.cancel:
w.ended <- struct{}{}
return
default:
w.executeJob()
}
}
}()
return w.ended
}
func (w *worker) executeJob() {
message, job, err := w.getJob()
if err != nil {
log.Get().Error(fmt.Sprintf("worker: fail to get job: %v", err))
time.Sleep(w.getMessageBackoff.Duration())
return
}
w.getMessageBackoff.Reset()
if message == nil {
return
}
gen := func(msg proto.Message) error {
return proto.Unmarshal(message.Body(), msg)
}
wrappedHandle := func(ctx context.Context) error {
return job.Handle(ctx, gen)
}
for i := len(w.middlewares) - 1; i >= 0; i-- {
index := i
oldWrapped := wrappedHandle
wrappedHandle = func(index int) func(context.Context) error {
return func(ctx context.Context) error {
return w.middlewares[index](injectJobInfo(ctx, *job, message),
oldWrapped)
}
}(index)
}
ctx, cancelFn := context.WithCancel(context.Background())
defer cancelFn()
if err := wrappedHandle(ctx); err != nil {
if job != nil && message.Retries() < job.Retry {
for err := message.Requeue(); err != nil; {
time.Sleep(w.backoff.Duration())
}
w.backoff.Reset()
}
}
}