-
Notifications
You must be signed in to change notification settings - Fork 53
/
task.go
133 lines (122 loc) · 2.77 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
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
package task
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
"github.com/rancher/opni/pkg/storage"
"github.com/rancher/opni/pkg/util"
"github.com/samber/lo"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/protobuf/types/known/timestamppb"
)
type Task struct {
id string
cctx context.Context // controller context
status storage.ValueStoreLocker[*corev1.TaskStatus]
logger *zap.SugaredLogger
}
func (t *Task) TaskId() string {
return t.id
}
func (t *Task) LoadTaskMetadata(output any) {
t.status.Lock()
defer t.status.Unlock()
status := t.getStatus()
md := status.GetMetadata()
if strings.TrimSpace(md) == "" {
return
}
if err := json.Unmarshal([]byte(md), output); err != nil {
panic(fmt.Sprintf("bug: failed to unmarshal task metadata into type %T: %v", output, err))
}
}
func (t *Task) SetProgress(progress *corev1.Progress) {
t.status.Lock()
defer t.status.Unlock()
status := t.getStatus()
if status == nil {
return
}
status.Progress = util.ProtoClone(progress)
t.putStatus(status)
}
func (t *Task) GetProgress() *corev1.Progress {
t.status.Lock()
defer t.status.Unlock()
return t.getStatus().GetProgress()
}
func (t *Task) AddLogEntry(level zapcore.Level, msg string) {
t.status.Lock()
defer t.status.Unlock()
status := t.getStatus()
if status == nil {
return
}
switch level {
case zapcore.DebugLevel:
t.logger.Debug(msg)
case zapcore.InfoLevel:
t.logger.Info(msg)
case zapcore.WarnLevel:
t.logger.Warn(msg)
case zapcore.ErrorLevel:
t.logger.Error(msg)
default:
t.logger.Info(msg)
}
status.Logs = append(status.Logs, &corev1.LogEntry{
Msg: msg,
Level: int32(level),
Timestamp: timestamppb.Now(),
})
t.putStatus(status)
}
func (t *Task) logTransition(transition *corev1.StateTransition) {
t.status.Lock()
defer t.status.Unlock()
status := t.getStatus()
if status == nil {
return
}
status.Transitions = append(status.Transitions, transition)
t.putStatus(status)
}
// requires status lock held
func (t *Task) getStatus() *corev1.TaskStatus {
for {
select {
case result := <-lo.Async2(util.BindContext2(t.status.Get, t.cctx)):
status, err := lo.Unpack2(result)
if err == nil {
return status
}
t.logger.With(
zap.Error(err),
).Warn("failed to get task status (will retry)")
time.Sleep(time.Second)
case <-t.cctx.Done():
return nil
}
}
}
// requires status lock held
func (t *Task) putStatus(status *corev1.TaskStatus) {
for {
select {
case err := <-lo.Async(func() error { return t.status.Put(t.cctx, status) }):
if err == nil {
return
}
t.logger.With(
zap.Error(err),
).Warn("failed to set task status (will retry)")
time.Sleep(time.Second)
case <-t.cctx.Done():
return
}
}
}