Store checked instances in map to preserve order.

This commit is contained in:
Kavin 2023-07-26 12:46:28 +01:00
parent e8d2b2b267
commit 20e5350736
No known key found for this signature in database
GPG Key ID: 6E4598CA5C92C41F

19
main.go
View File

@ -238,11 +238,12 @@ func monitorInstances() {
lines := strings.Split(buf.String(), "\n")
var instances []Instance
instancesMap := make(map[int]Instance)
wg := sync.WaitGroup{}
skipped := 0
checking := 0
for _, line := range lines {
split := strings.Split(line, "|")
@ -257,18 +258,28 @@ func monitorInstances() {
}
wg.Add(1)
go func(split []string) {
go func(i int, split []string) {
defer wg.Done()
instance, err := getInstanceDetails(split, latest)
if err == nil {
instances = append(instances, instance)
instancesMap[i] = instance
} else {
log.Print(err)
}
}(split)
}(checking, split)
checking++
}
wg.Wait()
// Map to ordered array
var instances []Instance
for i := 0; i < checking; i++ {
instance, ok := instancesMap[i]
if ok {
instances = append(instances, instance)
}
}
// update the global instances variable
monitored_instances = instances
}