-
Notifications
You must be signed in to change notification settings - Fork 7
/
task.go
61 lines (47 loc) · 1.08 KB
/
task.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
package progrock
import timestamppb "google.golang.org/protobuf/types/known/timestamppb"
type TaskRecorder struct {
*VertexRecorder
Task *VertexTask
}
func (recorder *TaskRecorder) Wrap(f func() error) error {
recorder.Start()
err := f()
recorder.Done(err)
return err
}
func (recorder *TaskRecorder) Done(err error) {
if err != nil {
recorder.Error(err)
}
recorder.Complete()
}
func (recorder *TaskRecorder) Start() {
now := Clock.Now()
recorder.Task.Started = timestamppb.New(now)
recorder.sync()
}
func (recorder *TaskRecorder) Complete() {
now := Clock.Now()
if recorder.Task.Started == nil {
recorder.Task.Started = timestamppb.New(now)
}
recorder.Task.Completed = timestamppb.New(now)
recorder.sync()
}
func (recorder *TaskRecorder) Progress(cur, total int64) {
recorder.Task.Current = cur
recorder.Task.Total = total
recorder.sync()
}
func (recorder *TaskRecorder) Current(cur int64) {
recorder.Task.Current = cur
recorder.sync()
}
func (recorder *TaskRecorder) sync() {
recorder.Recorder.Record(&StatusUpdate{
Tasks: []*VertexTask{
recorder.Task,
},
})
}