This is an automated email from the ASF dual-hosted git repository.
rohit pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack-cloudmonkey.git
The following commit(s) were added to refs/heads/main by this push:
new d1599ea network: allow breaking async job loop when Ctrl+C is pressed
d1599ea is described below
commit d1599eaa7ce19b856f3d471fc6241ccf4b6d5dc5
Author: Rohit Yadav <[email protected]>
AuthorDate: Fri Feb 9 12:54:45 2024 +0530
network: allow breaking async job loop when Ctrl+C is pressed
This break the async job loop when Ctrl+C is pressed during async job
polling. May address #126
Signed-off-by: Rohit Yadav <[email protected]>
---
cmd/network.go | 22 +++++++++++++++++-----
1 file changed, 17 insertions(+), 5 deletions(-)
diff --git a/cmd/network.go b/cmd/network.go
index 61653c9..6588668 100644
--- a/cmd/network.go
+++ b/cmd/network.go
@@ -30,6 +30,7 @@ import (
"net/http/cookiejar"
"net/url"
"os"
+ "os/signal"
"sort"
"strings"
"time"
@@ -148,7 +149,18 @@ func pollAsyncJob(r *Request, jobID string)
(map[string]interface{}, error) {
spinner := r.Config.StartSpinner("polling for async API result")
defer r.Config.StopSpinner(spinner)
+ interrupted := false
+ c := make(chan os.Signal, 1)
+ signal.Notify(c, os.Interrupt)
+ go func() {
+ <-c
+ interrupted = true
+ }()
+
for {
+ if interrupted {
+ return nil, errors.New("API job query interrupted")
+ }
select {
case <-timeout.C:
return nil, errors.New("async API job query timed out")
@@ -234,8 +246,8 @@ func NewAPIRequest(r *Request, api string, args []string,
isAsync bool) (map[str
config.Debug("NewAPIRequest API request URL:", requestURL)
var response *http.Response
- response,err = executeRequest(r, requestURL, params)
- if (err != nil) {
+ response, err = executeRequest(r, requestURL, params)
+ if err != nil {
return nil, err
}
config.Debug("NewAPIRequest response status code:", response.StatusCode)
@@ -251,8 +263,8 @@ func NewAPIRequest(r *Request, api string, args []string,
isAsync bool) (map[str
requestURL = fmt.Sprintf("%s?%s", r.Config.ActiveProfile.URL,
encodeRequestParams(params))
config.Debug("NewAPIRequest API request URL:", requestURL)
- response,err = executeRequest(r, requestURL, params)
- if (err != nil) {
+ response, err = executeRequest(r, requestURL, params)
+ if err != nil {
return nil, err
}
}
@@ -281,7 +293,7 @@ func NewAPIRequest(r *Request, api string, args []string,
isAsync bool) (map[str
}
// we can implement further conditions to do POST or GET (or other http
commands) here
-func executeRequest(r *Request, requestURL string, params url.Values)
(*http.Response, error){
+func executeRequest(r *Request, requestURL string, params url.Values)
(*http.Response, error) {
if params.Has("password") || params.Has("userdata") {
requestURL = fmt.Sprintf("%s", r.Config.ActiveProfile.URL)
return r.Client().PostForm(requestURL, params)