-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathstatus.go
341 lines (312 loc) · 8.07 KB
/
status.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2018 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Package status provides facilities for reporting statuses from a
// number of tasks working towards a common goal. The toplevel Status
// represents the status for the whole job; it in turn comprises a
// number of groups; each group has 0 or more tasks.
//
// Tasks (and groups) may be updated via their Print[f] functions;
// reporters receive notifications when updates have been made.
//
// Package status also includes a standard console reporter that
// formats nice status screens when the output is a terminal, or else
// issues periodic status updates.
package status
import (
"fmt"
"io"
"sort"
"sync"
"text/tabwriter"
"time"
)
const expiry = 10 * time.Second
// Value is a task or group status at a point in time, it includes a
// title, status, as well as its start and stop times (undefined for
// groups).
type Value struct {
Title, Status string
Begin, End time.Time
LastBegin time.Time
Count int
}
// A Task is a single unit of work. It has a title, a beginning and
// an end time, and may receive zero or more status updates
// throughout its lifetime.
type Task struct {
group *Group
// next is managed by the task's group.
next *Task
mu sync.Mutex
value Value
}
// Print formats a message as fmt.Sprint and updates the task's
// status.
func (t *Task) Print(v ...interface{}) {
if t == nil {
return
}
t.mu.Lock()
t.value.Status = fmt.Sprint(v...)
t.mu.Unlock()
t.group.notify()
}
// Printf formats a message as fmt.Sprintf and updates the task's
// status.
func (t *Task) Printf(format string, args ...interface{}) {
if t == nil {
return
}
t.mu.Lock()
t.value.Status = fmt.Sprintf(format, args...)
t.mu.Unlock()
t.group.notify()
}
// Title formats a title as fmt.Sprint, and updates the task's title.
func (t *Task) Title(v ...interface{}) {
if t == nil {
return
}
t.mu.Lock()
t.value.Title = fmt.Sprint(v...)
t.mu.Unlock()
t.group.notify()
}
// Titlef formats a title as fmt.Sprintf, and updates the task's title.
func (t *Task) Titlef(format string, args ...interface{}) {
if t == nil {
return
}
t.mu.Lock()
t.value.Title = fmt.Sprintf(format, args...)
t.mu.Unlock()
t.group.notify()
}
// Done sets the completion time of the task to the current time.
// Tasks should not be updated after a call to Done; they will be
// discarded by the group after a timeout.
func (t *Task) Done() {
if t == nil {
return
}
t.mu.Lock()
t.value.End = time.Now()
t.mu.Unlock()
t.group.notify()
}
// Value returns this tasks's current value.
func (t *Task) Value() Value {
t.mu.Lock()
v := t.value
t.mu.Unlock()
return v
}
// A Group is a collection of tasks, working toward a common goal.
// Groups are persistent: they have no beginning or end; they have a
// "toplevel" status that can be updated.
type Group struct {
status *Status
mu sync.Mutex
value Value
task *Task
}
// Print formats a status as fmt.Sprint and sets it as the group's status.
func (g *Group) Print(v ...interface{}) {
if g == nil {
return
}
g.mu.Lock()
g.value.Status = fmt.Sprint(v...)
g.mu.Unlock()
g.notify()
}
// Printf formats a status as fmt.Sprintf and sets it as the group's status.
func (g *Group) Printf(format string, args ...interface{}) {
if g == nil {
return
}
g.mu.Lock()
g.value.Status = fmt.Sprintf(format, args...)
g.mu.Unlock()
g.notify()
}
// Start creates a new task associated with this group and returns it.
// The task's initial title is formatted from the provided arguments as
// fmt.Sprint.
func (g *Group) Start(v ...interface{}) *Task {
if g == nil {
return nil
}
task := new(Task)
g.mu.Lock()
task.value.Begin = time.Now()
task.group = g
p := &g.task
for *p != nil {
p = &(*p).next
}
*p = task
g.mu.Unlock()
task.Title(v...) // this will also notify
return task
}
// Startf creates a new task associated with tihs group and returns it.
// The task's initial title is formatted from the provided arguments as
// fmt.Sprintf.
func (g *Group) Startf(format string, args ...interface{}) *Task {
return g.Start(fmt.Sprintf(format, args...))
}
// Tasks returns a snapshot of the group's currently active tasks.
// Expired tasks are garbage collected on calls to Tasks. Tasks are
// returned in the order of creation: the oldest is always first.
func (g *Group) Tasks() []*Task {
now := time.Now()
g.mu.Lock()
defer g.mu.Unlock()
var tasks []*Task
for p := &g.task; *p != nil; {
value := (*p).Value()
if !value.End.IsZero() && now.Sub(value.End) > expiry {
*p = (*p).next
} else {
tasks = append(tasks, *p)
p = &(*p).next
}
}
return tasks
}
// Value returns the group's current value.
func (g *Group) Value() Value {
g.mu.Lock()
v := g.value
g.mu.Unlock()
return v
}
func (g *Group) notify() {
g.status.notify()
}
type waiter struct {
version int
c chan int
}
// Status represents a toplevel status object. A status comprises a
// number of groups which in turn comprise a number of sub-tasks.
type Status struct {
mu sync.Mutex
groups map[string]*Group
version int
order []string
waiters []waiter
}
// Group creates and returns a new group named by the provided
// arguments as formatted by fmt.Sprint. If the group already exists,
// it is returned.
func (s *Status) Group(v ...interface{}) *Group {
name := fmt.Sprint(v...)
s.mu.Lock()
if s.groups == nil {
s.groups = make(map[string]*Group)
}
if s.groups[name] == nil {
s.groups[name] = &Group{status: s, value: Value{Title: name}}
}
g := s.groups[name]
s.mu.Unlock()
s.notify()
return g
}
// Groupf creates and returns a new group named by the provided
// arguments as formatted by fmt.Sprintf. If the group already exists,
// it is returned.
func (s *Status) Groupf(format string, args ...interface{}) *Group {
return s.Group(fmt.Sprintf(format, args...))
}
// Wait returns a channel that is blocked until the version of
// status data is greater than the provided version. When the
// status version exceeds v, it is written to the channel and
// then closed.
//
// This allows status observers to implement a simple loop
// that coalesces updates:
//
// v := -1
// for {
// v = <-status.Wait(v)
// groups := status.Groups()
// // ... process groups
// }
func (s *Status) Wait(v int) <-chan int {
s.mu.Lock()
defer s.mu.Unlock()
c := make(chan int, 1)
if v < s.version {
c <- s.version
return c
}
i := sort.Search(len(s.waiters), func(i int) bool {
return s.waiters[i].version > v
})
s.waiters = append(s.waiters[:i], append([]waiter{{v, c}}, s.waiters[i:]...)...)
return c
}
// Groups returns a snapshot of the status groups. Groups maintains a
// consistent order of returned groups: when a group cohort first
// appears, it is returned in arbitrary order; each cohort is
// appended to the last, and the order of all groups is remembered
// across invocations.
func (s *Status) Groups() []*Group {
s.mu.Lock()
seen := make(map[string]bool)
for _, name := range s.order {
seen[name] = true
}
for name := range s.groups {
if !seen[name] {
s.order = append(s.order, name)
}
}
var groups []*Group
for _, name := range s.order {
if s.groups[name] != nil {
groups = append(groups, s.groups[name])
}
}
s.mu.Unlock()
return groups
}
// Marshal writes s in a human-readable format to w.
func (s *Status) Marshal(w io.Writer) error {
now := time.Now()
for _, group := range s.Groups() {
v := group.Value()
tw := tabwriter.NewWriter(w, 2, 4, 2, ' ', 0)
if _, err := fmt.Fprintf(tw, "%s: %s\n", v.Title, v.Status); err != nil {
return err
}
for _, task := range group.Tasks() {
v := task.Value()
elapsed := now.Sub(v.Begin)
elapsed -= elapsed % time.Second
if _, err := fmt.Fprintf(tw, "\t%s:\t%s\t%s\n", v.Title, v.Status, elapsed); err != nil {
return err
}
}
if err := tw.Flush(); err != nil {
return err
}
}
return nil
}
func (s *Status) notify() {
if s == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.version++
for len(s.waiters) > 0 && s.waiters[0].version < s.version {
s.waiters[0].c <- s.version
s.waiters = s.waiters[1:]
}
}