Skip to content

Commit 5d5e61a

Browse files
authored
Revert "Exit when CD task is done (#1056)" (#1073)
This reverts commit c22d63d.
1 parent c22d63d commit 5d5e61a

File tree

9 files changed

+23
-160
lines changed

9 files changed

+23
-160
lines changed

src/pkg/cli/client/byoc/aws/byoc.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,6 @@ func (b *ByocAws) setUpCD(ctx context.Context) error {
186186
return nil
187187
}
188188

189-
func (b *ByocAws) GetDeploymentStatus(ctx context.Context) error {
190-
return ecs.GetTaskStatus(ctx, b.cdTaskArn)
191-
}
192-
193189
func (b *ByocAws) Deploy(ctx context.Context, req *defangv1.DeployRequest) (*defangv1.DeployResponse, error) {
194190
return b.deploy(ctx, req, "up")
195191
}
@@ -709,8 +705,8 @@ func (b *ByocAws) QueryLogs(ctx context.Context, req *defangv1.TailRequest) (cli
709705
var err error
710706
var taskArn ecs.TaskArn
711707
var tailStream ecs.LiveTailStream
712-
var stopWhenCDTaskDone bool
713-
708+
stopWhenCDTaskDone := false
709+
logType := logs.LogType(req.LogType)
714710
if etag != "" && !pkg.IsValidRandomID(etag) { // Assume invalid "etag" is a task ID
715711
tailStream, err = b.driver.TailTaskID(ctx, etag)
716712
taskArn, _ = b.driver.GetTaskArn(etag)
@@ -729,23 +725,20 @@ func (b *ByocAws) QueryLogs(ctx context.Context, req *defangv1.TailRequest) (cli
729725
if req.Until.IsValid() {
730726
end = req.Until.AsTime()
731727
}
732-
tailStream, err = ecs.QueryAndTailLogGroups(ctx, start, end, b.getLogGroupInputs(etag, req.Project, service, req.Pattern, logs.LogType(req.LogType))...)
728+
tailStream, err = ecs.QueryAndTailLogGroups(ctx, start, end, b.getLogGroupInputs(etag, req.Project, service, req.Pattern, logType)...)
733729
taskArn = b.cdTaskArn
734-
stopWhenCDTaskDone = false
735730
}
736731
if err != nil {
737732
return nil, AnnotateAwsError(err)
738733
}
739734
if taskArn != nil {
740-
// When we have a CD task, override the context so we can cancel the tailing when the CD task is done
741735
var cancel context.CancelCauseFunc
742736
ctx, cancel = context.WithCancelCause(ctx)
743737
go func() {
744738
if err := ecs.WaitForTask(ctx, taskArn, 2*time.Second); err != nil {
745-
isTaskFailure := errors.As(err, &ecs.TaskFailure{})
746-
if stopWhenCDTaskDone || isTaskFailure {
739+
if stopWhenCDTaskDone || errors.As(err, &ecs.TaskFailure{}) {
747740
time.Sleep(2 * time.Second) // make sure we got all the logs from the task/ecs before cancelling
748-
if isTaskFailure {
741+
if !errors.Is(err, io.EOF) {
749742
err = pkg.ErrDeploymentFailed{Message: err.Error()}
750743
}
751744
cancel(err)

src/pkg/cli/client/byoc/do/byoc.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -211,22 +211,6 @@ func (b *ByocDo) deploy(ctx context.Context, req *defangv1.DeployRequest, cmd st
211211
}, nil
212212
}
213213

214-
func (b *ByocDo) GetDeploymentStatus(ctx context.Context) error {
215-
deploymentInfo, _, err := b.client.Apps.GetDeployment(ctx, b.cdAppID, b.cdDeploymentID)
216-
if err != nil {
217-
return err
218-
}
219-
220-
switch deploymentInfo.GetPhase() {
221-
default:
222-
return nil
223-
case godo.DeploymentPhase_Active:
224-
return io.EOF
225-
case godo.DeploymentPhase_Error, godo.DeploymentPhase_Canceled:
226-
return pkg.ErrDeploymentFailed{}
227-
}
228-
}
229-
230214
func (b *ByocDo) BootstrapCommand(ctx context.Context, req client.BootstrapCommandRequest) (string, error) {
231215
if err := b.setUp(ctx); err != nil {
232216
return "", err
@@ -597,11 +581,7 @@ func (s *subscribeStream) Receive() bool {
597581
State: phaseToState(deployment.Phase),
598582
}
599583
}
600-
select {
601-
case s.msg = <-s.queue:
602-
default:
603-
s.msg = nil
604-
}
584+
s.msg = <-s.queue
605585
return err == nil
606586
}
607587

src/pkg/cli/client/byoc/gcp/byoc.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ import (
77
"errors"
88
"fmt"
99
"io"
10-
"log"
1110
"os"
1211
"path"
1312
"strings"
1413
"time"
1514

1615
"cloud.google.com/go/logging/apiv2/loggingpb"
17-
run "cloud.google.com/go/run/apiv2"
18-
"cloud.google.com/go/run/apiv2/runpb"
1916
"cloud.google.com/go/storage"
2017
"github.com/DefangLabs/defang/src/pkg"
2118
"github.com/DefangLabs/defang/src/pkg/cli/client"
@@ -399,37 +396,6 @@ func (b *ByocGcp) Preview(ctx context.Context, req *defangv1.DeployRequest) (*de
399396
return b.deploy(ctx, req, "preview")
400397
}
401398

402-
func (b *ByocGcp) GetDeploymentStatus(ctx context.Context) error {
403-
client, err := run.NewExecutionsClient(ctx)
404-
if err != nil {
405-
return err
406-
}
407-
defer client.Close()
408-
409-
execution, err := client.GetExecution(ctx, &runpb.GetExecutionRequest{Name: b.cdExecution})
410-
if err != nil {
411-
log.Fatal("Failed to get execution:", err)
412-
}
413-
414-
isCompleted := false
415-
for _, condition := range execution.GetConditions() {
416-
if condition.GetType() == "Completed" {
417-
isCompleted = condition.GetState() == runpb.Condition_CONDITION_SUCCEEDED ||
418-
condition.GetState() == runpb.Condition_CONDITION_FAILED
419-
break
420-
}
421-
}
422-
423-
if isCompleted {
424-
if execution.GetSucceededCount() > 0 {
425-
return io.EOF
426-
}
427-
return pkg.ErrDeploymentFailed{}
428-
} else {
429-
return nil // no completed yet
430-
}
431-
}
432-
433399
func (b *ByocGcp) deploy(ctx context.Context, req *defangv1.DeployRequest, command string) (*defangv1.DeployResponse, error) {
434400
// If multiple Compose files were provided, req.Compose is the merged representation of all the files
435401
project, err := compose.LoadFromContent(ctx, req.Compose, "")

src/pkg/cli/client/playground.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66

7-
"github.com/DefangLabs/defang/src/pkg"
87
"github.com/DefangLabs/defang/src/pkg/term"
98
"github.com/DefangLabs/defang/src/pkg/types"
109
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
@@ -22,10 +21,6 @@ func (g *PlaygroundProvider) Deploy(ctx context.Context, req *defangv1.DeployReq
2221
return getMsg(g.GetController().Deploy(ctx, connect.NewRequest(req)))
2322
}
2423

25-
func (g *PlaygroundProvider) GetDeploymentStatus(ctx context.Context) error {
26-
return pkg.ErrDeploymentCompleted{} // TODO: implement on fabric, for now assume service is deployed
27-
}
28-
2924
func (g *PlaygroundProvider) Preview(ctx context.Context, req *defangv1.DeployRequest) (*defangv1.DeployResponse, error) {
3025
return nil, errors.New("the preview command is not valid for the Defang playground; did you forget --provider?")
3126
}

src/pkg/cli/client/provider.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ type Provider interface {
144144
ListConfig(context.Context, *defangv1.ListConfigsRequest) (*defangv1.Secrets, error)
145145
QueryForDebug(context.Context, *defangv1.DebugRequest) error
146146
Preview(context.Context, *defangv1.DeployRequest) (*defangv1.DeployResponse, error)
147-
GetDeploymentStatus(context.Context) error // nil means deployment is pending/running; io.EOF means deployment is done
148147
PutConfig(context.Context, *defangv1.PutConfigRequest) error
149148
RemoteProjectName(context.Context) (string, error)
150149
ServiceDNS(string) string

src/pkg/cli/composeUp.go

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"strings"
8-
"sync"
97
"time"
108

119
"github.com/DefangLabs/defang/src/pkg"
@@ -188,11 +186,6 @@ func TailUp(ctx context.Context, provider client.Provider, project *compose.Proj
188186
}
189187

190188
func WaitAndTail(ctx context.Context, project *compose.Project, client client.FabricClient, provider client.Provider, deploy *defangv1.DeployResponse, waitTimeout time.Duration, since time.Time, verbose bool) error {
191-
if DoDryRun {
192-
// If we are in dry-run mode, we don't need to wait for the deployment to complete
193-
return ErrDryRun
194-
}
195-
196189
ctx, cancelTail := context.WithCancelCause(ctx)
197190
defer cancelTail(nil) // to cancel WaitServiceState and clean-up context
198191

@@ -202,39 +195,14 @@ func WaitAndTail(ctx context.Context, project *compose.Project, client client.Fa
202195
defer cancelTimeout()
203196
}
204197

205-
var deploymentStatusCh = make(chan error, 2)
206-
wg := waitForDeploymentCompleted(ctx, provider, project, deploy, cancelTail, deploymentStatusCh)
207-
208-
tailOptions := NewTailOptionsForDeploy(deploy, since, verbose)
209-
// blocking call to tail
210-
TailUp(ctx, provider, project, deploy, tailOptions)
211-
wg.Wait()
212-
213-
close(deploymentStatusCh)
214-
for errDeployment := range deploymentStatusCh {
215-
var errDeploymentCompleted pkg.ErrDeploymentCompleted
216-
if !errors.As(errDeployment, &errDeploymentCompleted) && !(strings.Contains(errDeployment.Error(), "EOF")) {
217-
return errDeployment
218-
}
219-
}
220-
221-
return nil
222-
}
198+
errCompleted := errors.New("deployment succeeded") // tail canceled because of deployment completion
223199

224-
func waitForDeploymentCompleted(ctx context.Context, provider client.Provider, project *compose.Project, deploy *defangv1.DeployResponse, cancelTail context.CancelCauseFunc, deploymentStatusCh chan error) *sync.WaitGroup {
225200
const targetState = defangv1.ServiceState_DEPLOYMENT_COMPLETED
226201
_, unmanagedServices := SplitManagedAndUnmanagedServices(project.Services)
227-
228-
var wg sync.WaitGroup
229-
wg.Add(2)
230202
go func() {
231-
defer wg.Done()
232-
233-
// block on waiting for services to reach target state
234203
if err := WaitServiceState(ctx, provider, targetState, project.Name, deploy.Etag, unmanagedServices); err != nil {
235204
var errDeploymentFailed pkg.ErrDeploymentFailed
236205
if errors.As(err, &errDeploymentFailed) {
237-
deploymentStatusCh <- err
238206
cancelTail(err)
239207
} else if !(errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)) {
240208
term.Warnf("error waiting for deployment completion: %v", err) // TODO: don't print in Go-routine
@@ -244,25 +212,22 @@ func waitForDeploymentCompleted(ctx context.Context, provider client.Provider, p
244212
service.State = targetState
245213
}
246214

247-
var errDeploymentCompleted = pkg.ErrDeploymentCompleted{}
248-
deploymentStatusCh <- errDeploymentCompleted
249-
cancelTail(errDeploymentCompleted)
215+
cancelTail(errCompleted)
250216
}
251217
}()
252218

253-
go func() {
254-
defer wg.Done()
255-
256-
// block on waiting for cdTask to complete
257-
err := WaitCdTaskState(ctx, provider)
258-
deploymentStatusCh <- err
259-
var errDeploymentFailed pkg.ErrDeploymentFailed
260-
if errors.As(err, &errDeploymentFailed) {
261-
cancelTail(err)
262-
}
263-
}()
219+
tailOptions := NewTailOptionsForDeploy(deploy, since, verbose)
220+
// blocking call to tail
221+
err := TailUp(ctx, provider, project, deploy, tailOptions)
222+
var errDeploymentFailed pkg.ErrDeploymentFailed
223+
if errors.As(context.Cause(ctx), &errDeploymentFailed) {
224+
return errDeploymentFailed
225+
}
226+
if !errors.Is(context.Cause(ctx), errCompleted) {
227+
return err
228+
}
264229

265-
return &wg
230+
return nil
266231
}
267232

268233
func NewTailOptionsForDeploy(deploy *defangv1.DeployResponse, since time.Time, verbose bool) TailOptions {

src/pkg/cli/getDeploymentStatus.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/pkg/cli/subscribe.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func WaitServiceState(
2222
) error {
2323
term.Debugf("waiting for services %v to reach state %s\n", services, targetState) // TODO: don't print in Go-routine
2424

25+
if DoDryRun {
26+
return ErrDryRun
27+
}
28+
2529
if len(services) == 0 {
2630
return ErrNothingToMonitor
2731
}

src/pkg/errors.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,3 @@ func (e ErrDeploymentFailed) Error() string {
1414
}
1515
return fmt.Sprintf("deployment failed%s: %s", service, e.Message)
1616
}
17-
18-
type ErrDeploymentCompleted struct{}
19-
20-
func (e ErrDeploymentCompleted) Error() string {
21-
return "deployment COMPLETED"
22-
}

0 commit comments

Comments
 (0)