Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update for handle failed deployment case #501

Merged
merged 13 commits into from
Jul 5, 2024
15 changes: 12 additions & 3 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import (
const DEFANG_PORTAL_HOST = "portal.defang.dev"
const SERVICE_PORTAL_URL = "https://" + DEFANG_PORTAL_HOST + "/service"

var ErrorFailedToReachRunningState = errors.New("failed to reach running state")
nullfunc marked this conversation as resolved.
Show resolved Hide resolved
var ErrorDeploymentFailed = errors.New("deployment failed")

const authNeeded = "auth-needed" // annotation to indicate that a command needs authorization
var authNeededAnnotation = map[string]string{authNeeded: ""}

Expand Down Expand Up @@ -828,6 +831,7 @@ var composeUpCmd = &cobra.Command{

tailCtx, cancelTail := context.WithCancel(ctx)

deploymentFailed := false
var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand All @@ -841,15 +845,20 @@ var composeUpCmd = &cobra.Command{
}
}()
if err := waitServiceStatus(ctx, cli.ServiceStarted, serviceInfos); err != nil && !errors.Is(err, context.Canceled) {
if !errors.Is(err, cli.ErrDryRun) && !errors.As(err, new(cliClient.ErrNotImplemented)) {
if errors.Is(err, ErrorDeploymentFailed) {
term.Warn("Deployment FAILED. Service(s) not running.")
nullfunc marked this conversation as resolved.
Show resolved Hide resolved
deploymentFailed = true
} else {
term.Warnf("failed to wait for service status: %v", err)
}
wg.Wait() // Wait until ctrl+c is pressed
lionello marked this conversation as resolved.
Show resolved Hide resolved
}
cancelTail()
wg.Wait() // Wait for tail to finish

printEndpoints(serviceInfos)
if !deploymentFailed {
printEndpoints(serviceInfos)
}

term.Info("Done.")
return nil
},
Expand Down
8 changes: 6 additions & 2 deletions src/cmd/cli/command/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package command

import (
"context"
"fmt"

"github.com/DefangLabs/defang/src/pkg/cli"
"github.com/DefangLabs/defang/src/pkg/term"
Expand Down Expand Up @@ -34,6 +33,11 @@ func waitServiceStatus(ctx context.Context, targetStatus cli.ServiceStatus, serv
continue
}

// exit on detecting a FAILED state
if newStatus.Status == string(cli.ServiceFailed) {
return ErrorDeploymentFailed
}

serviceStatus[newStatus.Name] = newStatus.Status

if allInStatus(targetStatus, serviceStatus) {
Expand All @@ -44,7 +48,7 @@ func waitServiceStatus(ctx context.Context, targetStatus cli.ServiceStatus, serv
}
}

return fmt.Errorf("service state monitoring terminated without all services reaching desired state: %s", targetStatus)
return ErrorFailedToReachRunningState
}

func allInStatus(targetStatus cli.ServiceStatus, serviceStatuses map[string]string) bool {
Expand Down