-
Notifications
You must be signed in to change notification settings - Fork 1
/
registry.go
120 lines (99 loc) · 2.23 KB
/
registry.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
package workerpool
import (
"sync"
)
type registry struct {
sync.RWMutex
pool *Workerpool
jobs map[string]*Job
cancelChan chan string
stsEnabled bool
sts map[string]map[string]struct{}
}
func newRegistry(p *Workerpool) *registry {
r := ®istry{
pool: p,
jobs: make(map[string]*Job),
cancelChan: make(chan string, 500),
sts: map[string]map[string]struct{}{
PENDING: make(map[string]struct{}),
RUNNING: make(map[string]struct{}),
COMPLETED: make(map[string]struct{}),
FAILED: make(map[string]struct{}),
CANCELLED: make(map[string]struct{}),
},
}
r.init()
return r
}
func (r *registry) init() {
go func() {
for id := range r.cancelChan {
if job := r.get(id); job != nil {
r.pool.log.Printf("workerpool: Canceling job %s", job.ID())
job.Cancel()
job.setStatus(CANCELLED)
}
}
}()
}
func (r *registry) add(job *Job) string {
change := job.OnStatusChangeFunc
job.OnStatusChangeFunc = func(j *Job) error {
r.pool.log.Printf("workerpool: Job %s is %s", job.ID(), j.Status())
defer r.updateStatus(job.ID(), j.Status())
return change(j)
}
r.Lock()
defer r.Unlock()
r.jobs[job.ID()] = job
return job.ID()
}
func (r *registry) get(id string) *Job {
r.Lock()
defer r.Unlock()
return r.jobs[id]
}
func (r *registry) cancel(id string) {
r.cancelChan <- id
}
func (r *registry) statusRecording(enabled bool) {
r.Lock()
defer r.Unlock()
r.stsEnabled = enabled
}
func (r *registry) statuses() map[string]interface{} {
r.Lock()
defer r.Unlock()
sts := make(map[string]interface{})
for k, v := range r.sts {
sts[k] = len(v)
}
return sts
}
func (r *registry) updateStatus(id, status string) {
r.Lock()
defer r.Unlock()
if !r.stsEnabled {
if status == CANCELLED || status == COMPLETED || status == FAILED {
delete(r.jobs, id)
}
return
}
for _, state := range []string{PENDING, RUNNING, COMPLETED, FAILED} {
delete(r.sts[state], id)
}
switch status {
case PENDING:
fallthrough
case RUNNING:
r.sts[status][id] = struct{}{}
case CANCELLED:
fallthrough
case COMPLETED:
fallthrough
case FAILED:
delete(r.jobs, id)
r.sts[status][id] = struct{}{} // FIXME Should take a lot of memory with a large amount of terminated jobs
}
}