Skip to content

Commit

Permalink
fix: startup commands would never run (#1905)
Browse files Browse the repository at this point in the history
Startup commands were being applied after the errgroup.Wait in `ftl
dev`, which blocked forever.
  • Loading branch information
alecthomas authored Jun 28, 2024
1 parent 078e7ee commit b7040cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
11 changes: 6 additions & 5 deletions cmd/ftl/cmd_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"golang.org/x/sync/errgroup"

"github.com/alecthomas/types/optional"

"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
"github.com/TBD54566975/ftl/buildengine"
"github.com/TBD54566975/ftl/common/projectconfig"
Expand Down Expand Up @@ -53,6 +55,8 @@ func (d *devCmd) Run(ctx context.Context, projConfig projectconfig.Config) error
return nil
}

// cmdServe will notify this channel when startup commands are complete and the controller is ready
controllerReady := make(chan bool, 1)
if !d.NoServe {
if d.ServeCmd.Stop {
err := d.ServeCmd.Run(ctx, projConfig)
Expand All @@ -65,14 +69,11 @@ func (d *devCmd) Run(ctx context.Context, projConfig projectconfig.Config) error
return errors.New(ftlRunningErrorMsg)
}

g.Go(func() error { return d.ServeCmd.Run(ctx, projConfig) })
g.Go(func() error { return d.ServeCmd.run(ctx, projConfig, optional.Some(controllerReady)) })
}

g.Go(func() error {
err := waitForControllerOnline(ctx, d.ServeCmd.StartupTimeout, client)
if err != nil {
return err
}
<-controllerReady

opts := []buildengine.Option{buildengine.Parallelism(d.Parallelism)}
if d.Lsp {
Expand Down
18 changes: 16 additions & 2 deletions cmd/ftl/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"connectrpc.com/connect"
"github.com/alecthomas/kong"
"github.com/alecthomas/types/optional"
"github.com/jackc/pgx/v5/pgxpool"
"golang.org/x/sync/errgroup"

Expand Down Expand Up @@ -48,6 +49,10 @@ const ftlContainerName = "ftl-db-1"
const ftlRunningErrorMsg = "FTL is already running. Use 'ftl serve --stop' to stop it"

func (s *serveCmd) Run(ctx context.Context, projConfig projectconfig.Config) error {
return s.run(ctx, projConfig, optional.None[chan bool]())
}

func (s *serveCmd) run(ctx context.Context, projConfig projectconfig.Config, initialised optional.Option[chan bool]) error {
logger := log.FromContext(ctx)
client := rpc.ClientFromContext[ftlv1connect.ControllerServiceClient](ctx)

Expand Down Expand Up @@ -135,8 +140,9 @@ func (s *serveCmd) Run(ctx context.Context, projConfig projectconfig.Config) err
})
}

if err := wg.Wait(); err != nil {
return fmt.Errorf("serve failed: %w", err)
// Wait for controller to start, then run startup commands.
if err := waitForControllerOnline(ctx, time.Second*10, client); err != nil {
return fmt.Errorf("controller failed to start: %w", err)
}

if len(projConfig.Commands.Startup) > 0 {
Expand All @@ -148,6 +154,14 @@ func (s *serveCmd) Run(ctx context.Context, projConfig projectconfig.Config) err
}
}

if ch, ok := initialised.Get(); ok {
ch <- true
}

if err := wg.Wait(); err != nil {
return fmt.Errorf("serve failed: %w", err)
}

return nil
}

Expand Down

0 comments on commit b7040cb

Please sign in to comment.