-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
247 lines (222 loc) · 5.57 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
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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"time"
"github.com/fatih/color"
"github.com/rogpeppe/go-internal/lockedfile"
)
type Task struct {
ID int64 `json:"id"`
UUID string `json:"uuid"`
Type string `json:"type"`
Tenant struct {
Name string `json:"Name"`
ID string `json:"id"`
} `json:"tenant"`
Policy struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
} `json:"policy"`
Context struct {
MachineName string `json:"MachineName"`
ProtectionPlanID string `json:"ProtectionPlanID"`
} `json:"context"`
Updated time.Time `json:"updatedAt"`
State string `json:"state"`
StartedByUser string `json:"startedByUser"`
CancelRequested bool `json:"cancelRequested"`
Kind int64 `json:"kind"`
Result struct {
Code string `json:"code"`
Error struct {
Reason string `json:"reason"`
Context struct {
Cause string `json:"cause_str"`
Effect string `json:"effect_str"`
} `json:"context"`
} `json:"error"`
} `json:"result"`
}
// see https://github.com/golang/go/issues/33974 to convert this to `x/sync/lockedfile.OpenFile` when possible
func writeTask(t Task, cfg cacheConfig) error {
filename := cfg.taskPath(t)
if filepath.Base(filename) == ".json" {
return nil
}
// color.Cyan("logging to disk - %s", filename)
f, err := lockedfile.OpenFile(
filename,
os.O_CREATE|os.O_TRUNC|os.O_WRONLY,
0644,
)
if err != nil {
return err
}
defer f.Close()
return json.NewEncoder(f).Encode(t)
}
// readTask reads a given Task from disk.
// During operation, the contents of T are overwritten completely.
// see https://github.com/golang/go/issues/33974 to convert this to `x/sync/lockedfile.OpenFile` when possible
func readTask(path string) (Task, error) {
var t Task
f, err := lockedfile.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
return Task{}, err
}
defer f.Close()
err = json.NewDecoder(f).Decode(&t)
return t, err
}
func strInSlice(target string, list []string) bool {
for _, s := range list {
if s == target {
return true
}
}
return false
}
func filterTaskState(states []string, in <-chan Task, out chan<- Task) {
defer close(out)
for t := range in {
if strInSlice(t.State, states) {
out <- t
}
}
}
type taskPipelineFunc func(Task) error
func writeTaskPipeline(cfg cacheConfig) taskPipelineFunc {
return func(task Task) error {
return writeTask(task, cfg)
}
}
func splitByPolicySet(policy, noPolicy taskPipelineFunc) taskPipelineFunc {
return func(t Task) error {
if t.Policy.ID == "" {
return noPolicy(t)
}
return policy(t)
}
}
func multiTaskPipelineFunc(children ...taskPipelineFunc) taskPipelineFunc {
return func(t Task) error {
for _, next := range children {
err := next(t)
if err != nil {
return err
}
}
return nil
}
}
func filterUpdatesOnly(
cfg cacheConfig,
next taskPipelineFunc) taskPipelineFunc {
return func(t Task) error {
path := cfg.taskPath(t)
loaded, err := readTask(path)
if err != nil {
if os.IsNotExist(err) {
// if there was an error reading the other cause it's not there
return next(t)
}
// otherwise if there was an error reading it, just disregard
return err
}
if t.Updated.Before(loaded.Updated) {
// if its after what's on disk, just skip
return nil
}
return next(t)
}
}
func (a *AcronisAPI) walkTasks(query url.Values, limit int, next taskPipelineFunc) error {
var limitStr string
if query == nil {
query = url.Values{}
}
if limit > 0 {
limitStr = strconv.Itoa(limit)
} else {
limitStr = "100"
}
query.Set("lod", "full")
query.Set("limit", limitStr)
query.Del("after")
var prev time.Time
ts := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()
newTasks, after, err := a.getTasks(ctx, query)
if err != nil {
return err
}
// FIXME: improve debugging logging?
afterAbrev := after
if len(afterAbrev) > 16 {
afterAbrev = afterAbrev[:16]
}
fmt.Fprintln(os.Stderr, color.CyanString(
"secs [%d] records [%d] after [%s] first ts [%s]\n",
int(ts.Sub(prev).Seconds()), len(newTasks), afterAbrev,
newTasks[0].Updated.Format(time.RFC3339)))
for _, task := range newTasks {
if err = next(task); err != nil {
return err
}
}
for after != "" {
prev = ts
ts = time.Now()
fmt.Fprintln(os.Stderr, color.CyanString(
"secs [%d] records [%d] after [%s] first ts [%s]\n",
int(ts.Sub(prev).Seconds()), len(newTasks), after[:16],
newTasks[0].Updated.Format(time.RFC3339)))
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()
query = url.Values{}
query.Set("after", after)
query.Set("limit", limitStr)
newTasks, after, err = a.getTasks(ctx, query)
if err != nil {
return err
}
for _, task := range newTasks {
if err = next(task); err != nil {
return err
}
}
}
return nil
}
func (a *AcronisAPI) getTasks(ctx context.Context, query url.Values) ([]Task, string, error) {
var respData struct {
Tasks []Task `json:"items"`
Paging struct {
Cursors struct {
After string `json:"after"`
} `json:"cursors"`
} `json:"paging"`
Error struct {
Message string `json:"message"`
} `json:"error"`
}
statusCode, err := a.Call(ctx, http.MethodGet, "api/task_manager/v2/tasks",
nil, query, nil, &respData)
if err != nil {
return []Task{}, "", err
}
if statusCode != http.StatusOK {
return []Task{}, "", fmt.Errorf("error status %d : %s",
statusCode, respData.Error.Message)
}
return respData.Tasks, respData.Paging.Cursors.After, nil
}