Skip to content

Commit

Permalink
network: allow breaking async job loop when Ctrl+C is pressed
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rohityadavcloud committed Feb 9, 2024
1 parent 6323a31 commit d1599ea
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"net/http/cookiejar"
"net/url"
"os"
"os/signal"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d1599ea

Please sign in to comment.