Skip to content

Commit

Permalink
refactor: Implement concurrent service deployment with improved error…
Browse files Browse the repository at this point in the history
… handling
  • Loading branch information
yarlson committed Nov 20, 2024
1 parent b511a77 commit cc42aa6
Showing 1 changed file with 50 additions and 17 deletions.
67 changes: 50 additions & 17 deletions pkg/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,27 +180,60 @@ func (d *Deployment) deployDependencies(ctx context.Context, project string, dep
return nil
}

// deployServices deploys all services concurrently.
func (d *Deployment) deployServices(ctx context.Context, project string, services []config.Service, events chan<- console.Event) error {
var wg sync.WaitGroup
errChan := make(chan error, len(services))

for _, service := range services {
select {
case <-ctx.Done():
return ctx.Err()
default:
events <- console.Event{
Type: console.EventTypeStart,
Message: fmt.Sprintf("Deploying service %s", service.Name),
Name: "services",
}
if err := d.deployService(project, &service); err != nil {
return fmt.Errorf("failed to deploy service %s: %w", service.Name, err)
}
events <- console.Event{
Type: console.EventTypeFinish,
Message: fmt.Sprintf("Service %s deployed", service.Name),
Name: "services",
wg.Add(1)
go func(service config.Service) {
defer wg.Done()

select {
case <-ctx.Done():
errChan <- ctx.Err()
return
default:
serviceName := service.Name

events <- console.Event{
Type: console.EventTypeStart,
Message: fmt.Sprintf("Deploying service %s", serviceName),
Name: serviceName,
}

if err := d.deployService(project, &service); err != nil {
events <- console.Event{
Type: console.EventTypeError,
Message: fmt.Sprintf("Failed to deploy service %s: %v", serviceName, err),
Name: serviceName,
}
errChan <- fmt.Errorf("failed to deploy service %s: %w", serviceName, err)
return
}

events <- console.Event{
Type: console.EventTypeFinish,
Message: fmt.Sprintf("Service %s deployed", serviceName),
Name: serviceName,
}
}
}
}(service)
}

wg.Wait()
close(errChan)

var errs []error
for err := range errChan {
errs = append(errs, err)
}

if len(errs) > 0 {
return fmt.Errorf("errors occurred during service deployment: %v", errs)
}

return nil
}

Expand Down

0 comments on commit cc42aa6

Please sign in to comment.