-
Notifications
You must be signed in to change notification settings - Fork 90
/
task_store.go
254 lines (199 loc) · 5.64 KB
/
task_store.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
package kotsstore
import (
"encoding/json"
"fmt"
"sync"
"time"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/persistence"
"github.com/rqlite/gorqlite"
)
const (
TaskStatusConfigMapName = `kotsadm-tasks`
ConfgConfigMapName = `kotsadm-confg`
taskCacheTTL = 1 * time.Minute
)
var (
taskStatusLock = sync.Mutex{}
)
type TaskStatus struct {
Message string `json:"message"`
Status string `json:"status"`
UpdatedAt time.Time `json:"updatedAt"`
}
func (s *KOTSStore) migrateTasksFromRqlite() error {
db := persistence.MustGetDBSession()
query := `select updated_at, current_message, status from api_task_status`
rows, err := db.QueryOne(query)
if err != nil {
return fmt.Errorf("failed to select tasks for migration: %v: %v", err, rows.Err)
}
taskCm, err := s.getConfigmap(TaskStatusConfigMapName)
if err != nil {
return errors.Wrap(err, "failed to get task status configmap")
}
if taskCm.Data == nil {
taskCm.Data = map[string]string{}
}
for rows.Next() {
var id string
var status gorqlite.NullString
var message gorqlite.NullString
ts := TaskStatus{}
if err := rows.Scan(&id, &ts.UpdatedAt, &message, &status); err != nil {
return errors.Wrap(err, "failed to scan task status")
}
if status.Valid {
ts.Status = status.String
}
if message.Valid {
ts.Message = message.String
}
b, err := json.Marshal(ts)
if err != nil {
return errors.Wrap(err, "failed to marshal task status")
}
taskCm.Data[id] = string(b)
}
if err := s.updateConfigmap(taskCm); err != nil {
return errors.Wrap(err, "failed to update task status configmap")
}
query = `delete from api_task_status`
if wr, err := db.WriteOne(query); err != nil {
return fmt.Errorf("failed to delete tasks from db: %v: %v", err, wr.Err)
}
return nil
}
func (s *KOTSStore) SetTaskStatus(id string, message string, status string) error {
taskStatusLock.Lock()
defer taskStatusLock.Unlock()
cached := s.cachedTaskStatus[id]
if cached == nil {
cached = &cachedTaskStatus{}
s.cachedTaskStatus[id] = cached
}
cached.taskStatus.Message = message
cached.taskStatus.Status = status
cached.taskStatus.UpdatedAt = time.Now()
cached.expirationTime = time.Now().Add(taskCacheTTL)
configmap, err := s.getConfigmap(TaskStatusConfigMapName)
if err != nil {
if canIgnoreEtcdError(err) {
return nil
}
return errors.Wrap(err, "failed to get task status configmap")
}
if configmap.Data == nil {
configmap.Data = map[string]string{}
}
b, err := json.Marshal(cached.taskStatus)
if err != nil {
return errors.Wrap(err, "failed to marshal task status")
}
configmap.Data[id] = string(b)
if err := s.updateConfigmap(configmap); err != nil {
if canIgnoreEtcdError(err) {
return nil
}
return errors.Wrap(err, "failed to update task status configmap")
}
return nil
}
func (s *KOTSStore) UpdateTaskStatusTimestamp(id string) error {
taskStatusLock.Lock()
defer taskStatusLock.Unlock()
cached := s.cachedTaskStatus[id]
if cached != nil {
cached.taskStatus.UpdatedAt = time.Now()
cached.expirationTime = time.Now().Add(taskCacheTTL)
}
configmap, err := s.getConfigmap(TaskStatusConfigMapName)
if err != nil {
if canIgnoreEtcdError(err) && cached != nil {
return nil
}
return errors.Wrap(err, "failed to get task status configmap")
}
if configmap.Data == nil {
configmap.Data = map[string]string{}
}
data, ok := configmap.Data[id]
if !ok {
return nil // copied from s3pgstore
}
ts := TaskStatus{}
if err := json.Unmarshal([]byte(data), &ts); err != nil {
return errors.Wrap(err, "failed to unmarshal task status")
}
ts.UpdatedAt = time.Now()
b, err := json.Marshal(ts)
if err != nil {
return errors.Wrap(err, "failed to marshal task status")
}
configmap.Data[id] = string(b)
if err := s.updateConfigmap(configmap); err != nil {
if canIgnoreEtcdError(err) && cached != nil {
return nil
}
return errors.Wrap(err, "failed to update task status configmap")
}
return nil
}
func (s *KOTSStore) ClearTaskStatus(id string) error {
taskStatusLock.Lock()
defer taskStatusLock.Unlock()
defer delete(s.cachedTaskStatus, id)
configmap, err := s.getConfigmap(TaskStatusConfigMapName)
if err != nil {
return errors.Wrap(err, "failed to get task status configmap")
}
if configmap.Data == nil {
configmap.Data = map[string]string{}
}
_, ok := configmap.Data[id]
if !ok {
return nil // copied from s3pgstore
}
delete(configmap.Data, id)
if err := s.updateConfigmap(configmap); err != nil {
return errors.Wrap(err, "failed to update task status configmap")
}
return nil
}
func (s *KOTSStore) GetTaskStatus(id string) (string, string, error) {
taskStatusLock.Lock()
defer taskStatusLock.Unlock()
cached := s.cachedTaskStatus[id]
if cached != nil && time.Now().Before(cached.expirationTime) {
return cached.taskStatus.Status, cached.taskStatus.Message, nil
}
if cached == nil {
cached = &cachedTaskStatus{
expirationTime: time.Now().Add(taskCacheTTL),
}
s.cachedTaskStatus[id] = cached
}
configmap, err := s.getConfigmap(TaskStatusConfigMapName)
if err != nil {
if canIgnoreEtcdError(err) && cached != nil {
return cached.taskStatus.Status, cached.taskStatus.Message, nil
}
return "", "", errors.Wrap(err, "failed to get task status configmap")
}
if configmap.Data == nil {
return "", "", nil
}
marshalled, ok := configmap.Data[id]
if !ok {
return "", "", nil
}
ts := TaskStatus{}
if err := json.Unmarshal([]byte(marshalled), &ts); err != nil {
return "", "", errors.Wrap(err, "error unmarshalling task status")
}
if ts.UpdatedAt.Before(time.Now().Add(-10 * time.Second)) {
return "", "", nil
}
cached.taskStatus = ts
return ts.Status, ts.Message, nil
}