Skip to content

Commit

Permalink
fix: move startup command exec from Dev to cmd_serve (#1825)
Browse files Browse the repository at this point in the history
Fixes #1343
  • Loading branch information
safeer authored Jun 18, 2024
1 parent 40b95a5 commit 923e2b3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
14 changes: 1 addition & 13 deletions buildengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/ftl-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions cmd/ftl/cmd_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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()
Expand Down
13 changes: 12 additions & 1 deletion cmd/ftl/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 923e2b3

Please sign in to comment.