Skip to content

Commit

Permalink
feat: timeout for deploy command
Browse files Browse the repository at this point in the history
Also limit test deploy to 1m to help diagnose issues
  • Loading branch information
stuartwdouglas committed Nov 7, 2024
1 parent 7e8abfa commit ca07e3a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
25 changes: 19 additions & 6 deletions frontend/cli/cmd_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package main

import (
"context"
"time"

"github.com/TBD54566975/golang-tools/go/ssa/testdata/src/fmt"

Check failure on line 7 in frontend/cli/cmd_deploy.go

View workflow job for this annotation

GitHub Actions / Lint

could not import github.com/TBD54566975/golang-tools/go/ssa/testdata/src/fmt (-: # github.com/TBD54566975/golang-tools/go/ssa/testdata/src/fmt
"github.com/alecthomas/types/optional"

"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1beta1/provisioner/provisionerconnect"
Expand All @@ -11,10 +15,11 @@ import (
)

type deployCmd struct {
Replicas int32 `short:"n" help:"Number of replicas to deploy." default:"1"`
NoWait bool `help:"Do not wait for deployment to complete." default:"false"`
UseProvisioner bool `help:"Use the ftl-provisioner to deploy the application." default:"false" hidden:"true"`
Build buildCmd `embed:""`
Replicas int32 `short:"n" help:"Number of replicas to deploy." default:"1"`
NoWait bool `help:"Do not wait for deployment to complete." default:"false"`
UseProvisioner bool `help:"Use the ftl-provisioner to deploy the application." default:"false" hidden:"true"`
Build buildCmd `embed:""`
Timeout time.Duration `short:"t" help:"Timeout for the deployment."`
}

func (d *deployCmd) Run(ctx context.Context, projConfig projectconfig.Config) error {
Expand All @@ -24,7 +29,6 @@ func (d *deployCmd) Run(ctx context.Context, projConfig projectconfig.Config) er
} else {
client = rpc.ClientFromContext[ftlv1connect.ControllerServiceClient](ctx)
}

bindAllocator, err := bindAllocatorWithoutController()
if err != nil {
return err
Expand All @@ -37,5 +41,14 @@ func (d *deployCmd) Run(ctx context.Context, projConfig projectconfig.Config) er
if err != nil {
return err
}
return engine.BuildAndDeploy(ctx, d.Replicas, !d.NoWait)
tm := optional.None[time.Duration]()
if d.Timeout.Milliseconds() > 0 {
tm = optional.Some(d.Timeout)
}

err = engine.BuildAndDeploy(ctx, d.Replicas, !d.NoWait, tm)
if err != nil {
return fmt.Errorf("failed to deploy: %w", err)
}
return nil
}
17 changes: 11 additions & 6 deletions internal/buildengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func (e *Engine) watchForModuleChanges(ctx context.Context, period time.Duration
}()

// Build and deploy all modules first.
err = e.BuildAndDeploy(ctx, 1, true)
err = e.BuildAndDeploy(ctx, 1, true, optional.None[time.Duration]())
if err != nil {
logger.Errorf(err, "initial deploy failed")
}
Expand Down Expand Up @@ -540,7 +540,7 @@ func (e *Engine) watchForModuleChanges(ctx context.Context, period time.Duration
}
e.moduleMetas.Store(config.Module, meta)
e.rawEngineUpdates <- ModuleAdded{Module: config.Module}
_ = e.BuildAndDeploy(ctx, 1, true, config.Module) //nolint:errcheck
_ = e.BuildAndDeploy(ctx, 1, true, optional.None[time.Duration](), config.Module) //nolint:errcheck
}
case watch.WatchEventModuleRemoved:
err := terminateModuleDeployment(ctx, e.client, event.Config.Module)
Expand Down Expand Up @@ -577,7 +577,7 @@ func (e *Engine) watchForModuleChanges(ctx context.Context, period time.Duration
meta.module.Config = validConfig
e.moduleMetas.Store(event.Config.Module, meta)

_ = e.BuildAndDeploy(ctx, 1, true, event.Config.Module) //nolint:errcheck
_ = e.BuildAndDeploy(ctx, 1, true, optional.None[time.Duration](), event.Config.Module) //nolint:errcheck
}
case change := <-schemaChanges:
if change.ChangeType == ftlv1.DeploymentChangeType_DEPLOYMENT_REMOVED {
Expand All @@ -604,7 +604,7 @@ func (e *Engine) watchForModuleChanges(ctx context.Context, period time.Duration
dependentModuleNames := e.getDependentModuleNames(change.Name)
if len(dependentModuleNames) > 0 {
logger.Infof("%s's schema changed; processing %s", change.Name, strings.Join(dependentModuleNames, ", "))
_ = e.BuildAndDeploy(ctx, 1, true, dependentModuleNames...) //nolint:errcheck
_ = e.BuildAndDeploy(ctx, 1, true, optional.None[time.Duration](), dependentModuleNames...) //nolint:errcheck
}

case event := <-e.rebuildRequests:
Expand All @@ -619,7 +619,7 @@ func (e *Engine) watchForModuleChanges(ctx context.Context, period time.Duration
break readLoop
}
}
_ = e.BuildAndDeploy(ctx, 1, true, modules...) //nolint:errcheck
_ = e.BuildAndDeploy(ctx, 1, true, optional.None[time.Duration](), modules...) //nolint:errcheck
}
}
}
Expand Down Expand Up @@ -755,7 +755,7 @@ func (e *Engine) getDependentModuleNames(moduleName string) []string {
}

// BuildAndDeploy attempts to build and deploy all local modules.
func (e *Engine) BuildAndDeploy(ctx context.Context, replicas int32, waitForDeployOnline bool, moduleNames ...string) error {
func (e *Engine) BuildAndDeploy(ctx context.Context, replicas int32, waitForDeployOnline bool, timeout optional.Option[time.Duration], moduleNames ...string) error {
logger := log.FromContext(ctx)
if len(moduleNames) == 0 {
moduleNames = e.Modules()
Expand All @@ -768,6 +768,11 @@ func (e *Engine) BuildAndDeploy(ctx context.Context, replicas int32, waitForDepl
buildGroup.Go(func() error {
e.modulesToBuild.Store(module.Config.Module, false)
e.rawEngineUpdates <- ModuleDeployStarted{Module: module.Config.Module}
var cancel context.CancelFunc
if tm, ok := timeout.Get(); ok {
buildCtx, cancel = context.WithTimeout(buildCtx, tm)
defer cancel()
}
err := Deploy(buildCtx, e.projectConfig, module, module.Deploy, replicas, waitForDeployOnline, e.client)
if err != nil {
e.rawEngineUpdates <- ModuleDeployFailed{Module: module.Config.Module, Error: err}
Expand Down
2 changes: 1 addition & 1 deletion internal/integration/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func ExpectError(action Action, expectedErrorMsg ...string) Action {
func Deploy(module string) Action {
return Chain(
func(t testing.TB, ic TestContext) {
args := []string{"deploy"}
args := []string{"deploy", "-t", "1m"}
if ic.Provisioner != nil {
args = append(args, "--use-provisioner", "--provisioner-endpoint=http://localhost:8893")
}
Expand Down

0 comments on commit ca07e3a

Please sign in to comment.