-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathconsumer.go
268 lines (205 loc) · 5.32 KB
/
consumer.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
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
package bokchoy
import (
"context"
"fmt"
"sync"
"github.com/thoas/bokchoy/logging"
"github.com/pkg/errors"
)
type consumer struct {
name string
handler Handler
middlewares []func(Handler) Handler
queue *Queue
serializer Serializer
logger logging.Logger
tracer Tracer
wg *sync.WaitGroup
closed bool
mu *sync.Mutex
}
func (c *consumer) stop(ctx context.Context) {
c.mu.Lock()
defer c.mu.Unlock()
if !c.closed {
c.wg.Done()
c.closed = true
}
c.logger.Debug(ctx, fmt.Sprintf("Stopped %s", c))
}
func (c *consumer) String() string {
return c.name
}
func (c *consumer) handleTask(ctx context.Context, r *Request) error {
var (
err error
task = r.Task
)
timeout, cancel := context.WithTimeout(ctx, task.Timeout)
defer cancel()
type result struct {
err error
}
conn := make(chan result)
// we execute the complete request lifecycle in a goroutine
// to handle timeout and retrieve the worker back if the executing
// time a task is higher than timeout.
go func() {
defer func() {
conn <- result{
err: err,
}
}()
middlewares := c.middlewares
middlewares = append(middlewares, c.handleRequest)
// chain wraps the n middleware with its n-1
// we keep the reference of the final request since it contains
// a modified context by middlewares.
err = chain(middlewares, HandlerFunc(func(req *Request) error {
*r = *req
// execute the underlying handler.
return c.handler.Handle(req)
})).Handle(r)
// retrieve the error in the context added
// by the recoverer middleware.
if err == nil {
err = GetContextError(r.Context())
}
}()
select {
// timeout done, we have to cancel the task.
case <-timeout.Done():
c.logger.Debug(ctx, "Task canceled by timeout", logging.Object("task", task))
err = ErrTaskCanceled
case res := <-conn:
err = res.err
}
if err != nil {
return errors.Wrapf(err, "unable to handle %s", task)
}
return nil
}
// handleError can be called twice since it's called inside Handle
// and in handleRequest.
func (c *consumer) handleError(ctx context.Context, task *Task, err error) error {
// Why do we have to call it twice?
// A panicking task should be marked as failed, when it's panicking
// the first handleError is skipped.
if !task.IsStatusProcessing() {
return nil
}
if err == nil {
task.MarkAsSucceeded()
c.logger.Debug(ctx, "Task marked as succeeded", logging.Object("task", task))
err = c.queue.Save(ctx, task)
if err != nil {
return errors.Wrapf(err, "unable to handle error %s", task)
}
return nil
}
c.tracer.Log(ctx, "Received an error when handling task", errors.Wrapf(err, "unable to handle task %s", task))
if errors.Cause(err) == ErrTaskCanceled {
task.MarkAsCanceled()
c.logger.Debug(ctx, "Task marked as canceled", logging.Object("task", task))
} else {
task.MarkAsFailed(err)
}
if task.MaxRetries == 0 {
c.logger.Debug(ctx, "Task marked as failed: no retry", logging.Object("task", task))
err = c.queue.Save(ctx, task)
if err != nil {
return errors.Wrapf(err, "unable to handle error %s", task)
}
return nil
}
task.MaxRetries -= 1
task.ETA = task.RetryETA()
c.logger.Debug(ctx, fmt.Sprintf("Task marked as failed: retrying in %s...", task.ETADisplay()),
logging.Object("task", task))
err = c.queue.PublishTask(ctx, task)
if err != nil {
return errors.Wrapf(err, "unable to handle error %s", task)
}
return nil
}
func (c *consumer) handleRequest(next Handler) Handler {
return HandlerFunc(func(req *Request) error {
var (
ctx = req.Context()
err = next.Handle(req)
)
return c.handleError(ctx, req.Task, err)
})
}
func (c *consumer) Handle(r *Request) error {
c.mu.Lock()
defer c.mu.Unlock()
var (
task = r.Task
err error
)
ctx := r.Context()
c.logger.Debug(ctx, "Task received", logging.Object("task", task))
if task.IsStatusCanceled() {
c.logger.Debug(ctx, "Task has been previously canceled", logging.Object("task", task))
} else {
task.MarkAsProcessing()
c.logger.Debug(ctx, "Task processing...", logging.Object("task", task))
err = c.queue.Save(ctx, task)
if err != nil {
return err
}
err = c.queue.fireEvents(r)
if err != nil {
return err
}
err = c.handleTask(ctx, r)
err = c.handleError(ctx, task, err)
if err != nil {
return err
}
funcs := GetContextAfterRequestFuncs(r.Context())
for i := range funcs {
funcs[i]()
}
}
return c.queue.fireEvents(r)
}
func (c *consumer) start(ctx context.Context) {
c.wg.Add(1)
c.consume(ctx)
c.logger.Debug(ctx, fmt.Sprintf("Started %s", c))
}
func (c *consumer) isClosed() bool {
c.mu.Lock()
closed := c.closed
defer c.mu.Unlock()
return closed
}
func (c *consumer) consume(ctx context.Context) {
go func() {
for {
ctx := context.Background()
if c.isClosed() {
return
}
tasks, err := c.queue.Consume(ctx)
if err != nil {
c.tracer.Log(ctx, "Receive error from publisher", err)
}
if len(tasks) == 0 {
continue
}
c.logger.Debug(ctx, "Received tasks to consume", logging.Int("tasks_count", len(tasks)))
for i := range tasks {
task := tasks[i]
req := &Request{Task: task}
err = c.Handle(req.WithContext(WithContextTask(req.Context(), task)))
if err != nil {
c.tracer.Log(ctx, "Receive error when handling", err)
}
}
}
}()
}
var _ Handler = (*consumer)(nil)