-
Notifications
You must be signed in to change notification settings - Fork 15
/
task.go
65 lines (58 loc) · 1.38 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
package proxmox
import (
"encoding/json"
"errors"
"strings"
"time"
)
type Task struct {
UPid string `json:"upid"`
Type string `json:"type"`
Status string `json:"status"`
ExitStatus string `json:"exitstatus"`
PID float64 `json:"pid"`
PStart float64 `json:"pstart"`
StartTime float64 `json:"starttime"`
EndTime float64 `json:"endtime"`
ID string `json:"id"`
proxmox ProxMox
}
type TaskList map[string]Task
type TaskResult struct {
Data Task `json:"data"`
}
func (task Task) GetStatus() (string, string, error) {
var target string
var err error
var raw []byte
var data TaskResult
upidParts := strings.Split(task.UPid, ":")
target = "nodes/" + upidParts[1] + "/tasks/" + task.UPid + "/status"
//fmt.Println("target " + target)
raw, err = task.proxmox.GetBytes(target)
if err != nil {
return "", "", err
}
err = json.Unmarshal(raw, &data)
if err != nil {
return "", "", err
}
return data.Data.Status, data.Data.ExitStatus, nil
}
func (task Task) WaitForStatus(status string, timeout int) (string, error) {
var i int
var err error
var actstatus string
var exitstatus string
for i = 0; i < timeout; i++ {
actstatus, exitstatus, err = task.GetStatus()
if err != nil {
return "", err
}
if actstatus == status {
return exitstatus, nil
}
time.Sleep(time.Second * 1)
}
return "", errors.New("Timeout reached")
}