-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
197 lines (170 loc) · 3.25 KB
/
hub.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
// TODO check pool load on add
// TODO ignore to frequent progress updates
// TODO context
// TODO gracefully stop pool
// TODO give get status a timeout
package tasks
import (
"errors"
"math"
"sync"
"time"
"github.com/alitto/pond"
)
type State int
const (
Waiting State = iota
Running
Done
Failed
)
type Progress struct {
Numerator, Denominator int
}
type Status struct {
State State
Progress Progress
Error error
StartTime time.Time
EndTime time.Time
}
type Task struct {
id string
hub *Hub
}
type taskCmd struct {
id string
fn func(Task) error
}
type stateCmd struct {
id string
state State
err error
start time.Time
end time.Time
}
type progressCmd struct {
id string
num, denom int
}
type statusCmd struct {
id string
ch chan Status
}
type Hub struct {
statuses map[string]*Status
pool *pond.WorkerPool
mu sync.RWMutex
minRetentionTime time.Duration
}
func NewHub() *Hub {
return &Hub{
statuses: make(map[string]*Status),
pool: pond.New(250, 5000),
minRetentionTime: 5 * time.Minute,
}
}
func (h *Hub) Add(id string, fn func(Task) error) {
if h.addTask(id) {
h.pool.Submit(func() {
h.setRunning(id)
defer func() {
if r := recover(); r != nil {
var err error
switch rt := r.(type) {
case string:
err = errors.New(rt)
case error:
err = rt
default:
err = errors.New("unknown panic")
}
h.setFailed(id, err)
}
}()
if err := fn(Task{hub: h, id: id}); err != nil {
h.setFailed(id, err)
return
}
h.setDone(id)
})
}
}
func (h *Hub) Status(id string) Status {
h.mu.RLock()
defer h.mu.RUnlock()
if s, ok := h.statuses[id]; ok {
return *s
}
return Status{}
}
func (h *Hub) addTask(id string) bool {
h.cleanup()
h.mu.Lock()
defer h.mu.Unlock()
if _, ok := h.statuses[id]; ok {
return false
}
h.statuses[id] = &Status{}
return true
}
func (h *Hub) setRunning(id string) {
h.mu.Lock()
defer h.mu.Unlock()
s := h.statuses[id]
s.State = Running
s.StartTime = time.Now()
}
func (h *Hub) setDone(id string) {
h.mu.Lock()
defer h.mu.Unlock()
s := h.statuses[id]
s.State = Done
s.EndTime = time.Now()
}
func (h *Hub) setFailed(id string, err error) {
h.mu.Lock()
defer h.mu.Unlock()
s := h.statuses[id]
s.State = Failed
s.EndTime = time.Now()
s.Error = err
}
func (h *Hub) setProgress(id string, num, denom int) {
h.mu.Lock()
defer h.mu.Unlock()
s := h.statuses[id]
s.Progress.Numerator = num
s.Progress.Denominator = denom
}
func (h *Hub) cleanup() {
h.mu.Lock()
defer h.mu.Unlock()
now := time.Now()
for id, status := range h.statuses {
if (status.State == Done || status.State == Failed) && now.Sub(status.EndTime) > h.minRetentionTime {
delete(h.statuses, id)
}
}
}
func (s Status) Waiting() bool {
return s.State == Waiting
}
func (s Status) Running() bool {
return s.State == Running
}
func (s Status) Done() bool {
return s.State == Done
}
func (s Status) Failed() bool {
return s.State == Failed
}
func (p Progress) Percent() int {
if p.Denominator == 0 {
return 0
}
return int(math.Round((float64(p.Numerator) * float64(100)) / float64(p.Denominator)))
}
func (t Task) Progress(num int, denom int) {
t.hub.setProgress(t.id, num, denom)
}