-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
47 lines (40 loc) · 779 Bytes
/
pool.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
package archer
import (
"context"
"time"
"github.com/dyaksa/archer/job"
)
type pool struct {
queue Queue
handler Handler
sleepInterval time.Duration
}
func newPool(q *Queue, m mutate, w Worker, sleepInterval time.Duration) *pool {
return &pool{
queue: *q,
handler: newHandler(w, m),
sleepInterval: sleepInterval,
}
}
func (p *pool) Run(ctx context.Context, errChan chan<- error) {
for {
select {
case <-ctx.Done():
return
default:
j, err := p.queue.Poll(ctx)
if err == job.ErrorJobNotFound {
time.Sleep(p.sleepInterval)
continue
}
if err != nil {
errChan <- err
continue
}
if err := p.handler.Handle(context.Background(), *j); err != nil {
errChan <- err
continue
}
}
}
}