Skip to content

Commit

Permalink
fix: make sure ftl serve --background waits for controller to start
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman committed Feb 23, 2024
1 parent 288a9cd commit 559ad47
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion cmd/ftl/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import (
"syscall"
"time"

"connectrpc.com/connect"
"github.com/alecthomas/kong"
"golang.org/x/sync/errgroup"

"github.com/TBD54566975/ftl/backend/controller"
"github.com/TBD54566975/ftl/backend/controller/scaling/localscaling"
"github.com/TBD54566975/ftl/backend/controller/sql/databasetesting"
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
"github.com/TBD54566975/ftl/internal/bind"
"github.com/TBD54566975/ftl/internal/exec"
"github.com/TBD54566975/ftl/internal/log"
Expand All @@ -38,11 +41,17 @@ type serveCmd struct {

const ftlContainerName = "ftl-db-1"

func (s *serveCmd) Run(ctx context.Context) error {
func (s *serveCmd) Run(ctx context.Context, client ftlv1connect.ControllerServiceClient) error {
logger := log.FromContext(ctx)

if s.Background {
runInBackground(logger)

err := s.pollControllerOnine(ctx, client)
if err != nil {
return err
}

os.Exit(0)
}

Expand Down Expand Up @@ -300,3 +309,36 @@ func pollContainerHealth(ctx context.Context, containerName string, timeout time
}
}
}

func (s *serveCmd) pollControllerOnine(ctx context.Context, client ftlv1connect.ControllerServiceClient) error {
logger := log.FromContext(ctx)

timeoutDuration := 10 * time.Second // should be enough time for the controller to start
ctx, cancel := context.WithTimeout(ctx, timeoutDuration)
defer cancel()

ticker := time.NewTicker(time.Second)
defer ticker.Stop()

for {
select {
case <-ticker.C:
_, err := client.Status(ctx, connect.NewRequest(&ftlv1.StatusRequest{
AllControllers: true,
}))
if err != nil {
logger.Tracef("Error getting status, retrying...: %v", err)
continue // retry
}

return nil

case <-ctx.Done():
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
logger.Errorf(ctx.Err(), "Timeout reached while polling for controller status")
}

return ctx.Err()
}
}
}

0 comments on commit 559ad47

Please sign in to comment.