Skip to content

Commit

Permalink
restructure WaitForCompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
lnxbil committed Nov 26, 2019
1 parent 8d39c29 commit c45b357
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
43 changes: 24 additions & 19 deletions proxmox.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,37 +569,42 @@ func (p ProxmoxVE) GetStorageType(node string, storagename string) (string, erro

// TaskStatusReturn represents a status return message from the API
type TaskStatusReturn struct {
Data []struct {
UPID string `json:"upid"`
Node string `json:"node"`
User string `json:"user"`
PID int `json:"pid"`
ID string `json:"id"`
StartTime int `json:"starttime"`
Type string `json:"type"`
PStart int `json:"pstart"`
Status string `json:"status"`
}
UPID string `json:"upid"`
Node string `json:"node"`
User string `json:"user"`
PID int `json:"pid"`
ID string `json:"id"`
StartTime int `json:"starttime"`
Exitstatus string `json:"exitstatus"`
Type string `json:"type"`
PStart int `json:"pstart"`
Status string `json:"status"`
}

// WaitForTaskToComplete waits until the given task in taskid is finished (exited or otherwise)
func (p ProxmoxVE) WaitForTaskToComplete(node string, taskid string) {
func (p ProxmoxVE) WaitForTaskToComplete(node string, taskid string) error {
path := fmt.Sprintf("/nodes/%s/tasks/%s/status", node, taskid)

tsr := TaskStatusReturn{}

for true {
log.Infof("Waiting for task %s to finish", taskid)
p.get(nil, &tsr, path)
if len(tsr.Data) == 0 {
log.Info("Empty result, so it's finished or wrong taskid")
return
err := p.get(nil, &tsr, path)
if err != nil {
log.Infof("Got on read: %s", err.Error())
return err
}
if tsr.Data[0].Status != "running" {
log.Infof("Status is %s, exiting", tsr.Data[0].Status)
return
log.Infof("Status is %s", tsr.Status)
if tsr.Status != "running" {
if tsr.Exitstatus != "OK" {
return fmt.Errorf("%s -> %s: %s", tsr.Type, tsr.Status, tsr.Exitstatus)
}
log.Infof("exiting with %s", tsr.Exitstatus)
return nil
}
log.Info("still running, waiting 500ms")
time.Sleep(500 * time.Millisecond)
}
// unreachable code
return nil
}
17 changes: 14 additions & 3 deletions proxmoxdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,16 @@ func (d *Driver) Create() error {
return err
}

d.driver.WaitForTaskToComplete(d.Node, taskid)
err = d.driver.WaitForTaskToComplete(d.Node, taskid)
if err != nil {
return err
}

err = d.Start()
if err != nil {
return err
}

return d.waitAndPrepareSSH()
}
func (d *Driver) waitAndPrepareSSH() error {
Expand Down Expand Up @@ -471,7 +475,11 @@ func (d *Driver) Start() error {
}
taskid, err := d.driver.NodesNodeQemuVMIDStatusStartPost(d.Node, d.VMID)

d.driver.WaitForTaskToComplete(d.Node, taskid)
if err != nil {
return err
}

err = d.driver.WaitForTaskToComplete(d.Node, taskid)

return err
}
Expand Down Expand Up @@ -501,8 +509,11 @@ func (d *Driver) Remove() error {
}
taskid, err := d.driver.NodesNodeQemuVMIDDelete(d.Node, d.VMID)

d.driver.WaitForTaskToComplete(d.Node, taskid)
if err != nil {
return err
}

err = d.driver.WaitForTaskToComplete(d.Node, taskid)
return err
}

Expand Down

0 comments on commit c45b357

Please sign in to comment.