Skip to content

Commit

Permalink
feat: execute commands async to speed up tasks (#863)
Browse files Browse the repository at this point in the history
* feat: execute commands async to speed up tasks

* show host ip on log and errors

* removed goroutine
  • Loading branch information
fmartingr authored Dec 5, 2024
1 parent 841b786 commit a48d4e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
41 changes: 29 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,34 @@ 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), mlog.String("ip", client.IP))
if out, err := client.RunCommand(c.Value); err != nil {
errors <- fmt.Errorf("failed to run cmd %q on %s: %w %s", c.Value, client.IP, err, out)
}
}()
}

wg.Wait()
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
3 changes: 2 additions & 1 deletion deployment/terraform/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type ExtAgent struct {
// commands in a single method.
type Client struct {
client *ssh.Client
IP string
}

// NewAgent connects to the local ssh agent and validates
Expand Down Expand Up @@ -68,7 +69,7 @@ func (ea *ExtAgent) NewClientWithPort(ip string, port string) (*Client, error) {
if err != nil {
return nil, err
}
return &Client{client: sshc}, nil
return &Client{client: sshc, IP: ip}, nil
}

// NewClient returns a Client object by dialing
Expand Down

0 comments on commit a48d4e8

Please sign in to comment.