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