feat: enhance logging for parallel commands

This logging change makes it easier to figure out what is breaking where when running commands in parallel.
This commit is contained in:
Jose Diaz-Gonzalez committed 2020-12-20 04:15:42 -05:00
1 parent 3ba775d4b3
commit 57b884e7c7
1 file changed
+8 -11
+8 -11
View File
@@ -10,16 +10,11 @@ import (
// the ps plugins can execute in parallel // the ps plugins can execute in parallel
type ParallelCommand func(string) error type ParallelCommand func(string) error
// ParallelCommandRun contains all the arguments
// necessary for a parallel command run
type ParallelCommandRun struct {
Name string
}
// ParallelCommandResult is the result of a parallel // ParallelCommandResult is the result of a parallel
// command run // command run
type ParallelCommandResult struct { type ParallelCommandResult struct {
Name string Name string
CommandName string
Error error Error error
} }
@@ -63,7 +58,7 @@ func RunCommandAgainstAllAppsInParallel(command ParallelCommand, commandName str
go allocateJobs(apps, jobs) go allocateJobs(apps, jobs)
done := make(chan error) done := make(chan error)
go aggregateResults(results, done) go aggregateResults(results, done)
createParallelWorkerPool(jobs, results, command, parallelCount) createParallelWorkerPool(jobs, results, command, commandName, parallelCount)
err = <-done err = <-done
return err return err
@@ -81,6 +76,7 @@ func RunCommandAgainstAllAppsSerially(command ParallelCommand, commandName strin
for _, appName := range apps { for _, appName := range apps {
LogInfo1(fmt.Sprintf("Running %s against app %s", commandName, appName)) LogInfo1(fmt.Sprintf("Running %s against app %s", commandName, appName))
if err = command(appName); err != nil { if err = command(appName); err != nil {
LogWarn(fmt.Sprintf("Error running %s against app %s: %s", commandName, appName, err.Error()))
errorCount++ errorCount++
} }
} }
@@ -104,7 +100,7 @@ func aggregateResults(results chan ParallelCommandResult, done chan error) {
errorCount := 0 errorCount := 0
for result := range results { for result := range results {
if result.Error != nil { if result.Error != nil {
LogWarn(fmt.Sprintf("Error running command against %s", result.Name)) LogWarn(fmt.Sprintf("Error running %s against %s: %s", result.CommandName, result.Name, result.Error.Error()))
errorCount++ errorCount++
} }
} }
@@ -114,11 +110,12 @@ func aggregateResults(results chan ParallelCommandResult, done chan error) {
done <- parallelError done <- parallelError
} }
func createParallelWorker(jobs chan string, results chan ParallelCommandResult, command ParallelCommand, wg *sync.WaitGroup, workerID int) { func createParallelWorker(jobs chan string, results chan ParallelCommandResult, command ParallelCommand, commandName string, wg *sync.WaitGroup, workerID int) {
for job := range jobs { for job := range jobs {
LogInfo1(fmt.Sprintf("Running command against %s", job)) LogInfo1(fmt.Sprintf("Running command against %s", job))
output := ParallelCommandResult{ output := ParallelCommandResult{
Name: job, Name: job,
CommandName: commandName,
Error: command(job), Error: command(job),
} }
results <- output results <- output
@@ -126,11 +123,11 @@ func createParallelWorker(jobs chan string, results chan ParallelCommandResult,
wg.Done() wg.Done()
} }
func createParallelWorkerPool(jobs chan string, results chan ParallelCommandResult, command ParallelCommand, numberOfWorkers int) { func createParallelWorkerPool(jobs chan string, results chan ParallelCommandResult, command ParallelCommand, commandName string, numberOfWorkers int) {
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < numberOfWorkers; i++ { for i := 0; i < numberOfWorkers; i++ {
wg.Add(1) wg.Add(1)
go createParallelWorker(jobs, results, command, &wg, i) go createParallelWorker(jobs, results, command, commandName, &wg, i)
} }
wg.Wait() wg.Wait()
close(results) close(results)