Skip to content

Commit

Permalink
Addition of waits and retries to have the instance vnc URL ready
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandroargentieri committed Jan 9, 2025
1 parent 7dc05b6 commit 321b1f9
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion cmd/instance/instance_vnc.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package instance

import (
"fmt"
"net/http"
"os"
"time"

"github.com/civo/cli/common"
"github.com/civo/cli/config"
"github.com/civo/cli/pkg/browser"
"github.com/civo/cli/utility"
"github.com/spf13/cobra"
"os"
)

// maxAttempts represents the max number of attempts (each one every 7s) to connect to the vnc URL
const maxAttempts = 5

var instanceVncCmd = &cobra.Command{
Use: "vnc",
Example: "civo instance vnc INSTANCE-ID/NAME",
Expand Down Expand Up @@ -46,7 +53,16 @@ var instanceVncCmd = &cobra.Command{
// Display VNC details
utility.Info("VNC has been successfully enabled for instance: %s", instance.Hostname)
utility.Info("VNC URL: %s", vnc.URI)
utility.Info("We're preparing the VNC Console Access. This may take a while...")

err = waitEndpointReady(vnc.URI)
if err != nil {
utility.Error("VNC Console URL is not reachable: %s", err)
os.Exit(1)
}

utility.Info("Opening VNC in your default browser...")
time.Sleep(3 * time.Second)

// Open VNC in the browser
err = browser.OpenInBrowser(vnc.URI)
Expand All @@ -57,3 +73,32 @@ var instanceVncCmd = &cobra.Command{
}
},
}

// endpointReady checks if the given URL endpoint is ready by sending a GET request
// and returning true if the HTTP status code is not 503 or 40x
func endpointReady(url string) bool {
utility.Info("New attempt to reach the VNC Console URI...")
resp, err := http.Get(url)
if err != nil {
return false
}
defer resp.Body.Close()

return resp.StatusCode != http.StatusServiceUnavailable
}

// waitEndpointReady continuously checks the given URL endpoint every 5 seconds
// until it becomes ready (i.e., it does not return an HTTP 503 status).
func waitEndpointReady(url string) error {
var attempt int
for {
attempt++
if endpointReady(url) {
return nil
}
if attempt == maxAttempts {
return fmt.Errorf("max num of attempts reached: VNC endpoint not ready - please contact the support")
}
time.Sleep(7 * time.Second) // Wait for 7 seconds before the next attempt
}
}

0 comments on commit 321b1f9

Please sign in to comment.