-
Notifications
You must be signed in to change notification settings - Fork 14
/
worker.go
89 lines (74 loc) · 1.41 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
package gitcollector
import (
"context"
"gopkg.in/src-d/go-errors.v1"
)
type worker struct {
id string
jobs chan Job
cancel chan bool
stopped bool
metrics MetricsCollector
}
func newWorker(jobs chan Job, metrics MetricsCollector) *worker {
return &worker{
jobs: jobs,
cancel: make(chan bool),
metrics: metrics,
}
}
var (
errJobsClosed = errors.NewKind("jobs channel was closed")
errWorkerStopped = errors.NewKind("worker was stopped")
)
func (w *worker) start() {
// It shouldn't be restarted after a call to stop.
if w.stopped {
return
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for {
if err := w.consumeJob(ctx); err != nil {
if errJobsClosed.Is(err) || errWorkerStopped.Is(err) {
close(w.cancel)
}
return
}
}
}
func (w *worker) consumeJob(ctx context.Context) error {
select {
case <-w.cancel:
return errWorkerStopped.New()
case job, ok := <-w.jobs:
if !ok {
return errJobsClosed.New()
}
var done = make(chan struct{})
go func() {
defer close(done)
if err := job.Process(ctx); err != nil {
w.metrics.Fail(job)
return
}
w.metrics.Success(job)
}()
select {
case now := <-w.cancel:
if !now {
<-done
}
return errWorkerStopped.New()
case <-done:
return nil
}
}
}
func (w *worker) stop(immediate bool) {
if w.stopped {
return
}
w.cancel <- immediate
w.stopped = true
}