Skip to content

Commit

Permalink
execute commands async
Browse files Browse the repository at this point in the history
  • Loading branch information
fmartingr committed Dec 20, 2024
1 parent 066537d commit ee6c6b0
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions deployment/terraform/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"path/filepath"
"strings"
"sync"

"github.com/mattermost/mattermost-load-test-ng/deployment"
"github.com/mattermost/mattermost-load-test-ng/deployment/terraform/ssh"
Expand Down Expand Up @@ -70,14 +71,8 @@ func (t *Terraform) ClearLicensesData() error {
Clients: appClients,
}

for _, c := range []deployment.Cmd{stopCmd, clearCmd, startCmd} {
mlog.Info(c.Msg)
for _, client := range c.Clients {
mlog.Debug("Running cmd", mlog.String("cmd", c.Value))
if out, err := client.RunCommand(c.Value); err != nil {
return fmt.Errorf("failed to run cmd %q: %w %s", c.Value, err, out)
}
}
if err := t.executeCommands([]deployment.Cmd{stopCmd, clearCmd, startCmd}); err != nil {
return fmt.Errorf("error executing database commands: %w", err)
}

return nil
Expand Down Expand Up @@ -257,12 +252,36 @@ func (t *Terraform) executeDatabaseCommands(extraCommands []deployment.Cmd) erro
func (t *Terraform) executeCommands(commands []deployment.Cmd) error {
for _, c := range commands {
mlog.Info(c.Msg)

errors := make(chan error, len(c.Clients))
wg := sync.WaitGroup{}

for _, client := range c.Clients {
mlog.Debug("Running cmd", mlog.String("cmd", c.Value))
if out, err := client.RunCommand(c.Value); err != nil {
return fmt.Errorf("failed to run cmd %q: %w %s", c.Value, err, out)
}
wg.Add(1)
go func() {
defer wg.Done()
mlog.Debug("Running cmd", mlog.String("cmd", c.Value))
if out, err := client.RunCommand(c.Value); err != nil {
errors <- fmt.Errorf("failed to run cmd %q: %w %s", c.Value, err, out)
}
}()
}

wg.Wait()
go func() {
close(errors)
}()

errorsFound := false
for e := range errors {
errorsFound = true
mlog.Error(e.Error())
}

if errorsFound {
return fmt.Errorf("errors found during command execution")
}

}

return nil
Expand Down

0 comments on commit ee6c6b0

Please sign in to comment.