-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.go
More file actions
348 lines (291 loc) · 8.57 KB
/
Copy pathtask.go
File metadata and controls
348 lines (291 loc) · 8.57 KB
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
342
343
344
345
346
347
348
package progress
import (
"io"
"time"
"github.com/containerd/console"
)
// RootTask is a task that can be used to close the channel of events.
type RootTask struct {
Task
}
// Close closes the channel of events.
func (r *RootTask) Close() error {
close(r.ch)
return nil
}
// NewRootTask creates a new RootTask that sends events to the given channel.
func NewRootTask(ch chan *TaskEvent) *RootTask {
return &RootTask{
Task{
ch: ch,
},
}
}
// DisplayProgress displays progress events to the console or trace. It is
// a convenience function that creates a RootTask and returns a channel that
// is closed when the rendering is complete. The caller has to make sure to
// close the RootTask after all Subtasks are completed. After the RootTask is
// closed, the remaining unprocesses events are rendered and the returned
// channel is closed.
func DisplayProgress(f console.File, name, mode string) (*RootTask, <-chan struct{}, error) {
events := make(chan *TaskEvent)
done, err := ProcessEvents(f, name, mode, events)
if err != nil {
return nil, nil, err
}
return NewRootTask(events), done, nil
}
// TaskEvent carries all the information about tasks. You'll only need this if
// you do not want to use the Task interfaces and provide the events yourself.
type TaskEvent struct {
ID uint64 // unique ID for the task, must be > 0
ParentID uint64 // ID of the parent task, 0 if no parent
Name string // name of the task, this will be displayed in the header line
StartTime, EndTime time.Time // start and end time of the task, used to calculate the duration
IOStartTime time.Time // start time of the IO task, used to calculate the rate and ETA
IsDone bool // true if the task is done, finished tasks will be displayed differently
Cached bool // true if the task is cached, cached tasks will be displayed differently when they are done
// Current and Total are used to display a copy progress if Total is
// unknown leave it as 0 and only Current will be displayed
Current, Total uint64
EnableDisplayRate bool // true if displaying the rate should be enabled, only used for io tasks
DisableDisplayRate bool // true if displaying the rate should be disabled, only used for io tasks
EnableDisplayBar bool // true if displaying the bar should be enabled, only used for io tasks
EnableDisplayETA bool // true if displaying the ETA should be enabled, only used for io tasks
DisableDisplayETA bool // true if displaying the ETA should be disabled, only used for io tasks
DisableDisplayBar bool // true if displaying the bar should be disabled, only used for io tasks
HasErr bool // true if the task has an error
Err error // error of the task, will be displayed in the task body when all tasks are done
Logs []byte // logs of the task, will be displayed in the task body
}
// TaskLogger implements io.Writer and writes logs to the task.
type TaskLogger struct {
ch chan *TaskEvent
id uint64
}
// Write writes logs to the task.
func (l *TaskLogger) Write(p []byte) (int, error) {
pp := make([]byte, len(p))
copy(pp, p)
l.ch <- &TaskEvent{
ID: l.id,
Logs: pp,
}
return len(p), nil
}
// Task is the base type for all tasks. It provides the basic functionality
// for tasks like logging and launching subtasks.
type Task struct {
id uint64
ch chan *TaskEvent
}
// Logger returns a *TaskLogger that can be used to write logs to the task.
func (t *Task) Logger() *TaskLogger {
return &TaskLogger{t.ch, t.id}
}
// Name sets the name of the task.
func (t *Task) Name(name string) {
t.ch <- &TaskEvent{
ID: t.id,
Name: name,
}
}
// Execute launches a new subtask by calling the given function and waits for
// it to complete. If f returns an error, the task will be marked as failed and
// the error will be returned.
func (t *Task) Execute(name string, f func(*Task) error) error {
newID := uint64(time.Now().UnixNano())
now := time.Now()
t.ch <- &TaskEvent{
ID: newID,
ParentID: t.id,
Name: name,
StartTime: now,
IOStartTime: now,
}
err := f(&Task{newID, t.ch})
t.ch <- &TaskEvent{
ID: newID,
EndTime: time.Now(),
IsDone: true,
HasErr: err != nil,
Err: err,
}
return err
}
// Cached marks the task as cached.
func (t *Task) Cached() {
t.ch <- &TaskEvent{
ID: t.id,
Cached: true,
}
}
// IOTask is a task that can be used to display IO progress.
type IOTask struct {
Task
}
// DisplayRate enables or disables the display of the rate.
func (t *IOTask) DisplayRate(b bool) {
EnableDisplayRate := b
DisableDisplayRate := !b
t.ch <- &TaskEvent{
ID: t.id,
EnableDisplayRate: EnableDisplayRate,
DisableDisplayRate: DisableDisplayRate,
}
}
// DisplayETA enables or disables the display of the ETA.
func (t *IOTask) DisplayETA(b bool) {
EnableDisplayETA := b
DisableDisplayETA := !b
t.ch <- &TaskEvent{
ID: t.id,
EnableDisplayETA: EnableDisplayETA,
DisableDisplayETA: DisableDisplayETA,
}
}
// DisplayBar enables or disables the display of a progress bar.
func (t *IOTask) DisplayBar(b bool) {
EnableDisplayBar := b
DisableDisplayBar := !b
t.ch <- &TaskEvent{
ID: t.id,
EnableDisplayBar: EnableDisplayBar,
DisableDisplayBar: DisableDisplayBar,
}
}
// ReaderTask tracks the progress of reading from an underlying io.Reader
type ReaderTask struct {
IOTask
read uint64
r io.Reader
}
// Read reads from the underlying reader and updates the progress.
func (t *ReaderTask) Read(p []byte) (int, error) {
n, err := t.r.Read(p)
t.read += uint64(n)
t.ch <- &TaskEvent{
ID: t.id,
Current: t.read,
}
return n, err
}
// Reader launches a new subtask that reads from the given reader. If total is
// 0, the task will not display a progress bar or ETA.
func (t *Task) Reader(name string, r io.Reader, total uint64, f func(*ReaderTask) error) error {
newID := uint64(time.Now().UnixNano())
now := time.Now()
t.ch <- &TaskEvent{
ID: newID,
ParentID: t.id,
Name: name,
Total: total,
StartTime: now,
IOStartTime: now,
}
rt := &ReaderTask{IOTask{Task{newID, t.ch}}, 0, r}
err := f(rt)
t.ch <- &TaskEvent{
ID: newID,
EndTime: time.Now(),
Current: rt.read,
IsDone: true,
HasErr: err != nil,
Err: err,
}
return err
}
// WriterTask tracks the progress of writing to an underlying io.Writer
type WriterTask struct {
IOTask
written uint64
w io.Writer
}
// Write writes to the underlying writer and updates the progress.
func (t *WriterTask) Write(p []byte) (int, error) {
n, err := t.w.Write(p)
t.written += uint64(n)
t.ch <- &TaskEvent{
ID: t.id,
Current: t.written,
}
return n, err
}
// Writer launches a new subtask that writes to the given writer. If total is
// 0, the task will not display a progress bar or ETA.
func (t *Task) Writer(name string, w io.Writer, total uint64, f func(*WriterTask) error) error {
newID := uint64(time.Now().UnixNano())
now := time.Now()
t.ch <- &TaskEvent{
ID: newID,
ParentID: t.id,
Name: name,
Total: total,
StartTime: now,
IOStartTime: now,
}
wt := &WriterTask{IOTask{Task{newID, t.ch}}, 0, w}
err := f(wt)
t.ch <- &TaskEvent{
ID: newID,
EndTime: time.Now(),
Current: wt.written,
IsDone: true,
HasErr: err != nil,
Err: err,
}
return err
}
// CopyTask tracks the progress of copying from an io.Reader to an io.Writer
type CopyTask struct {
IOTask
written uint64
}
// Copy copies from src to dest and updates the progress.
func (t *CopyTask) Copy(dest io.Writer, src io.Reader) (int64, error) {
r := &countReader{
notify: func(i int64) {
t.ch <- &TaskEvent{
ID: t.id,
Current: uint64(i),
}
},
r: src,
}
return io.Copy(dest, r)
}
// Reset resets the progress of the task, this is useful if you want to reuse
// the same task for multiple copies.
func (t *CopyTask) Reset(total uint64) {
t.ch <- &TaskEvent{
ID: t.id,
Total: total,
Current: 0,
IOStartTime: time.Now(),
}
}
// Copier launches a new subtask that can be used to copy from an io.Reader to
// an io.Writer. If total is 0, the task will not display a progress bar or ETA.
func (t *Task) Copier(name string, total uint64, f func(*CopyTask) error) error {
newID := uint64(time.Now().UnixNano())
now := time.Now()
t.ch <- &TaskEvent{
ID: newID,
ParentID: t.id,
Name: name,
Total: total,
StartTime: now,
IOStartTime: now,
}
ct := &CopyTask{IOTask{Task{newID, t.ch}}, 0}
err := f(ct)
t.ch <- &TaskEvent{
ID: newID,
EndTime: time.Now(),
Current: ct.written,
IsDone: true,
HasErr: err != nil,
Err: err,
}
return err
}