Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions pkg/commands/compute/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (c *DeployCommand) Exec(in io.Reader, out io.Writer) (err error) {

serviceURL := fmt.Sprintf("https://%s", domain)

if !c.StatusCheckOff {
if !c.StatusCheckOff && newService {
var status int
if status, err = checkingServiceAvailability(serviceURL+c.StatusCheckPath, spinner, c); err != nil {
if re, ok := err.(fsterr.RemediationError); ok {
Expand Down Expand Up @@ -1153,25 +1153,33 @@ func getServiceDomain(apiClient api.Interface, serviceID string, serviceVersion
return name, nil
}

// checkingServiceAvailability pings the service URL until either there is a 200
// OK or if the configured timeout is exceeded.
// checkingServiceAvailability pings the service URL until either there is a
// non-500 (or whatever status code is configured by the user) or if the
// configured timeout is reached.
func checkingServiceAvailability(
serviceURL string,
spinner text.Spinner,
c *DeployCommand,
) (status int, err error) {
remediation := "The service has been successfully deployed and activated, but the service 'availability' check %s (we were looking for a %s but the last status code response was: %d). If using a custom domain, please be sure to check your DNS settings. Otherwise, your application might be taking longer than usual to deploy across our global network. Please continue to check the service URL and if still unavailable please contact Fastly support."

dur := time.Duration(c.StatusCheckTimeout) * time.Second
end := time.Now().Add(dur)
timeout := time.After(dur)
ticker := time.NewTicker(1 * time.Second)
defer func() { ticker.Stop() }()

err = spinner.Start()
if err != nil {
return 0, err
}
msg := "Checking service availability"
spinner.Message(msg + " (app is being deployed across Fastly's global network)...")

timeout := time.After(time.Duration(c.StatusCheckTimeout) * time.Second)
ticker := time.NewTicker(1 * time.Second)
defer func() { ticker.Stop() }()
spinner.Message(msg + generateTimeout(time.Until(end)))

remediation := "The service has been successfully deployed and activated, but the service 'availability' check %s (last status code response was: %d). If using a custom domain, please be sure to check your DNS settings. Otherwise, your application might be taking longer than usual to deploy across our global network. Please continue to check the service URL and if still unavailable please contact Fastly support."
expected := "non-500 status code"
if c.StatusCheckCode > 0 {
expected = fmt.Sprintf("%d status code", c.StatusCheckCode)
}

// Keep trying until we're timed out, got a result or got an error
for {
Expand All @@ -1185,17 +1193,17 @@ func checkingServiceAvailability(
}
return status, fsterr.RemediationError{
Inner: errors.New("service not yet available"),
Remediation: fmt.Sprintf(remediation, "timed out", status),
Remediation: fmt.Sprintf(remediation, "timed out", expected, status),
}
case <-ticker.C:
case t := <-ticker.C:
var (
ok bool
err error
)
// We overwrite the `status` variable in the parent scope (defined in the
// return arguments list) so it can be used as part of both the timeout
// and success scenarios.
ok, status, err = pingServiceURL(serviceURL, c.Globals.HTTPClient)
ok, status, err = pingServiceURL(serviceURL, c.Globals.HTTPClient, c.StatusCheckCode)
if err != nil {
returnedStatus := fmt.Sprintf(" (status: %d)", status)
spinner.StopFailMessage(msg + returnedStatus)
Expand All @@ -1205,21 +1213,28 @@ func checkingServiceAvailability(
}
return status, fsterr.RemediationError{
Inner: err,
Remediation: fmt.Sprintf(remediation, "failed", status),
Remediation: fmt.Sprintf(remediation, "failed", expected, status),
}
} else if ok {
returnedStatus := fmt.Sprintf(" (status: %d)", status)
spinner.StopMessage(msg + returnedStatus)
return status, spinner.Stop()
}
// Service not available, and no error, so jump back to top of loop
spinner.Message(msg + generateTimeout(end.Sub(t)))
}
}
}

// pingServiceURL indicates if the service returned a non-5xx response, which
// should help signify if the service is generally available.
func pingServiceURL(serviceURL string, httpClient api.HTTPClient) (ok bool, status int, err error) {
func generateTimeout(d time.Duration) string {
remaining := fmt.Sprintf("timeout: %v", d.Round(time.Second))
return fmt.Sprintf(" (app is being deployed across Fastly's global network | %s)...", remaining)
}

// pingServiceURL indicates if the service returned a non-5xx response (or
// whatever the user defined with --status-check-code), which should help
// signify if the service is generally available.
func pingServiceURL(serviceURL string, httpClient api.HTTPClient, expectedStatusCode int) (ok bool, status int, err error) {
req, err := http.NewRequest("GET", serviceURL, nil)
if err != nil {
return false, 0, err
Expand All @@ -1233,7 +1248,12 @@ func pingServiceURL(serviceURL string, httpClient api.HTTPClient) (ok bool, stat
if err != nil {
return false, 0, err
}
if resp.StatusCode < http.StatusInternalServerError {

// We check for the user's defined status code expectation.
// Otherwise we'll default to checking for a non-500.
if expectedStatusCode > 0 && resp.StatusCode == expectedStatusCode {
return true, resp.StatusCode, nil
} else if resp.StatusCode < http.StatusInternalServerError {
return true, resp.StatusCode, nil
}
return false, resp.StatusCode, nil
Expand Down
3 changes: 3 additions & 0 deletions pkg/commands/compute/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
)

// PublishCommand produces and deploys an artifact from files on the local disk.
Expand Down Expand Up @@ -104,6 +105,8 @@ func (c *PublishCommand) Exec(in io.Reader, out io.Writer) (err error) {
return err
}

text.Break(out)

// Reset the fields on the DeployCommand based on PublishCommand values.
if c.pkg.WasSet {
c.deploy.Package = c.pkg.Value
Expand Down