Skip to content

Commit

Permalink
fix: parallelize deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman committed Mar 7, 2024
1 parent e8b0a36 commit 723b6ba
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions buildengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ func (e *Engine) Dev(ctx context.Context, period time.Duration) error {
logger := log.FromContext(ctx)

// Build and deploy all modules first.
err := e.buildWithCallback(ctx, func(ctx context.Context, module Module) error {
return Deploy(ctx, module, 1, true, e.client)
})
err := e.buildAndDeploy(ctx, 1, true)
if err != nil {
logger.Errorf(err, "initial deploy failed")
}
Expand Down Expand Up @@ -223,13 +221,41 @@ func (e *Engine) Dev(ctx context.Context, period time.Duration) error {
}

func (e *Engine) buildAndDeploy(ctx context.Context, replicas int32, waitForDeployOnline bool, modules ...string) error {
err := e.buildWithCallback(ctx, func(ctx context.Context, module Module) error {
return Deploy(ctx, module, replicas, waitForDeployOnline, e.client)
}, modules...)
if err != nil {
return err
if len(modules) == 0 {
modules = maps.Keys(e.modules)
}
return nil

deployQueue := make(chan Module, len(modules))
wg, ctx := errgroup.WithContext(ctx)
wg.SetLimit(runtime.NumCPU())

wg.Go(func() error {
defer close(deployQueue)

return e.buildWithCallback(ctx, func(ctx context.Context, module Module) error {
select {
case deployQueue <- module:
return nil
case <-ctx.Done():
return ctx.Err()
}
}, modules...)
})

for i := 0; i < len(modules); i++ {
wg.Go(func() error {
for module := range deployQueue {
module := module
// Deploy each module in its goroutine.
if err := Deploy(ctx, module, replicas, waitForDeployOnline, e.client); err != nil {
return err
}
}
return nil
})
}

return wg.Wait()
}

type buildCallback func(ctx context.Context, module Module) error
Expand Down

0 comments on commit 723b6ba

Please sign in to comment.