Skip to content

Commit

Permalink
Fix detailed status retrieval in PollCommandExecution
Browse files Browse the repository at this point in the history
  • Loading branch information
royroyee committed Feb 28, 2024
1 parent a89d50f commit 729f85b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
21 changes: 14 additions & 7 deletions api/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,19 @@ func RunCommand(ac *client.AlpaconClient, serverName, command string, username,
return "", err
}

return PollCommandExecution(ac, cmdResponse.Id)
result, err := PollCommandExecution(ac, cmdResponse.Id)
if err != nil {
return "", err
}

if result.Status["text"] == "Stuck" || result.Status["text"] == "Error" {
return result.Status["message"].(string), nil
}

return result.Result, nil
}

func PollCommandExecution(ac *client.AlpaconClient, cmdId string) (string, error) {
func PollCommandExecution(ac *client.AlpaconClient, cmdId string) (EventDetails, error) {
var response EventDetails

timer := time.NewTimer(5 * time.Minute)
Expand All @@ -96,24 +105,22 @@ func PollCommandExecution(ac *client.AlpaconClient, cmdId string) (string, error
for {
select {
case <-timer.C:
return "", errors.New("command execution timed out")
return response, errors.New("command execution timed out")
case <-ticker.C:
responseBody, err := ac.SendGetRequest(getEventURL + cmdId)
if err != nil {
continue
}
if err = json.Unmarshal(responseBody, &response); err != nil {
return "", err
return response, err
}

switch response.Status["text"] {
case "Acked":
timer.Reset(5 * time.Minute)
continue
case "Stuck", "Error":
return response.Status["message"].(string), nil
default:
return response.Result, nil
return response, nil
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions api/ftp/ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ func UploadFile(ac *client.AlpaconClient, src []string, dest, username, groupnam
if err != nil {
return nil, err
}
result = append(result, status)
if status.Status["text"] == "Stuck" || status.Status["text"] == "Error" {
result = append(result, status.Status["message"].(string))
} else {
result = append(result, status.Result)
}
}

return result, nil
Expand Down Expand Up @@ -124,7 +128,13 @@ func DownloadFile(ac *client.AlpaconClient, src, dest, username, groupname strin
return err
}

utils.CliWarning(fmt.Sprintf("File Transfer Status: '%s'. Attempting to transfer '%s' from the Alpacon server. Note: Transfer may timeout after 100 seconds.", status, path))
if status.Status["text"] == "Stuck" || status.Status["text"] == "Error" {
utils.CliError(status.Status["message"].(string))
}
if status.Status["text"] == "Failed" {
utils.CliError(status.Result)
}
utils.CliWarning(fmt.Sprintf("File Transfer Status: '%s'. Attempting to transfer '%s' from the Alpacon server. Note: Transfer may timeout after 100 seconds.", status.Result, path))

for count := 0; count < maxAttempts; count++ {
resp, err = ac.SendGetRequestForDownload(utils.RemovePrefixBeforeAPI(downloadResponse.DownloadURL))
Expand Down

0 comments on commit 729f85b

Please sign in to comment.