From 923e2b3bf0062d853370dd38fb31ae9d55b2ba12 Mon Sep 17 00:00:00 2001 From: Safeer Jiwan Date: Tue, 18 Jun 2024 14:27:29 -0700 Subject: [PATCH] fix: move startup command exec from Dev to cmd_serve (#1825) Fixes #1343 --- buildengine/engine.go | 14 +------------- cmd/ftl-controller/main.go | 2 +- cmd/ftl/cmd_dev.go | 6 +++--- cmd/ftl/cmd_serve.go | 13 ++++++++++++- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/buildengine/engine.go b/buildengine/engine.go index 258219d390..aaee445878 100644 --- a/buildengine/engine.go +++ b/buildengine/engine.go @@ -21,8 +21,6 @@ import ( 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/backend/schema" - "github.com/TBD54566975/ftl/common/projectconfig" - "github.com/TBD54566975/ftl/internal/exec" "github.com/TBD54566975/ftl/internal/log" "github.com/TBD54566975/ftl/internal/rpc" ) @@ -225,17 +223,7 @@ func (e *Engine) Deploy(ctx context.Context, replicas int32, waitForDeployOnline } // Dev builds and deploys all local modules and watches for changes, redeploying as necessary. -func (e *Engine) Dev(ctx context.Context, period time.Duration, commands projectconfig.Commands) error { - logger := log.FromContext(ctx) - if len(commands.Startup) > 0 { - for _, cmd := range commands.Startup { - logger.Debugf("Executing startup command: %s", cmd) - if err := exec.Command(ctx, log.Info, ".", "bash", "-c", cmd).Run(); err != nil { - return fmt.Errorf("startup command failed: %w", err) - } - } - } - +func (e *Engine) Dev(ctx context.Context, period time.Duration) error { return e.watchForModuleChanges(ctx, period) } diff --git a/cmd/ftl-controller/main.go b/cmd/ftl-controller/main.go index 1766553f7f..4181a6fe7d 100644 --- a/cmd/ftl-controller/main.go +++ b/cmd/ftl-controller/main.go @@ -27,7 +27,7 @@ var cli struct { ObservabilityConfig observability.Config `embed:"" prefix:"o11y-"` LogConfig log.Config `embed:"" prefix:"log-"` ControllerConfig controller.Config `embed:""` - ConfigFlag []string `name:"config" short:"C" help:"Paths to FTL project configuration files." env:"FTL_CONFIG" placeholder:"FILE[,FILE,...]"` + ConfigFlag string `name:"config" short:"C" help:"Path to FTL project configuration file." env:"FTL_CONFIG" placeholder:"FILE"` } func main() { diff --git a/cmd/ftl/cmd_dev.go b/cmd/ftl/cmd_dev.go index 929a71c13b..90f7cb52b7 100644 --- a/cmd/ftl/cmd_dev.go +++ b/cmd/ftl/cmd_dev.go @@ -55,7 +55,7 @@ func (d *devCmd) Run(ctx context.Context, projConfig projectconfig.Config) error if !d.NoServe { if d.ServeCmd.Stop { - err := d.ServeCmd.Run(ctx) + err := d.ServeCmd.Run(ctx, projConfig) if err != nil { return err } @@ -65,7 +65,7 @@ func (d *devCmd) Run(ctx context.Context, projConfig projectconfig.Config) error return errors.New(ftlRunningErrorMsg) } - g.Go(func() error { return d.ServeCmd.Run(ctx) }) + g.Go(func() error { return d.ServeCmd.Run(ctx, projConfig) }) } g.Go(func() error { @@ -88,7 +88,7 @@ func (d *devCmd) Run(ctx context.Context, projConfig projectconfig.Config) error if err != nil { return err } - return engine.Dev(ctx, d.Watch, projConfig.Commands) + return engine.Dev(ctx, d.Watch) }) return g.Wait() diff --git a/cmd/ftl/cmd_serve.go b/cmd/ftl/cmd_serve.go index ee54c9272f..723cef021e 100644 --- a/cmd/ftl/cmd_serve.go +++ b/cmd/ftl/cmd_serve.go @@ -22,8 +22,10 @@ import ( "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/common/projectconfig" "github.com/TBD54566975/ftl/internal/bind" "github.com/TBD54566975/ftl/internal/container" + "github.com/TBD54566975/ftl/internal/exec" "github.com/TBD54566975/ftl/internal/log" "github.com/TBD54566975/ftl/internal/model" "github.com/TBD54566975/ftl/internal/rpc" @@ -44,7 +46,7 @@ type serveCmd struct { 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) error { +func (s *serveCmd) Run(ctx context.Context, projConfig projectconfig.Config) error { logger := log.FromContext(ctx) client := rpc.ClientFromContext[ftlv1connect.ControllerServiceClient](ctx) @@ -77,6 +79,15 @@ func (s *serveCmd) Run(ctx context.Context) error { logger.Infof("Starting FTL with %d controller(s)", s.Controllers) + if len(projConfig.Commands.Startup) > 0 { + for _, cmd := range projConfig.Commands.Startup { + logger.Debugf("Executing startup command: %s", cmd) + if err := exec.Command(ctx, log.Info, ".", "bash", "-c", cmd).Run(); err != nil { + return fmt.Errorf("startup command failed: %w", err) + } + } + } + dsn, err := s.setupDB(ctx) if err != nil { return err