2019-03-04 09:19:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"github.com/Shopify/sarama"
|
|
|
|
"encoding/json"
|
2019-07-10 12:40:43 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"time"
|
2019-04-29 09:05:15 +00:00
|
|
|
)
|
2019-03-04 09:19:55 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
)
|
|
|
|
|
2019-10-24 12:25:59 +00:00
|
|
|
func start(pool *ResourcePool, config Configuration) {
|
|
|
|
consumer, err := sarama.NewConsumer(config.KafkaBrokers, nil)
|
2019-07-10 12:40:43 +00:00
|
|
|
for {
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Warn(err)
|
|
|
|
time.Sleep(time.Second * 5)
|
2019-10-24 12:25:59 +00:00
|
|
|
consumer, err = sarama.NewConsumer(config.KafkaBrokers, nil)
|
2019-03-04 09:19:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 12:25:59 +00:00
|
|
|
partitionList, err := consumer.Partitions(config.KafkaTopic)
|
2019-03-04 09:19:55 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for partition := range partitionList {
|
2019-10-24 12:25:59 +00:00
|
|
|
pc, err := consumer.ConsumePartition(config.KafkaTopic, int32(partition), sarama.OffsetNewest)
|
2019-03-04 09:19:55 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer pc.AsyncClose()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func(sarama.PartitionConsumer) {
|
|
|
|
defer wg.Done()
|
|
|
|
for msg := range pc.Messages() {
|
2019-04-16 08:59:19 +00:00
|
|
|
var nodeStatus NodeStatus
|
|
|
|
err = json.Unmarshal([]byte(string(msg.Value)), &nodeStatus)
|
2019-03-04 09:19:55 +00:00
|
|
|
if err != nil {
|
2019-10-24 12:25:59 +00:00
|
|
|
log.Warn(err)
|
2019-03-04 09:19:55 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-04-16 08:59:19 +00:00
|
|
|
pool.update(nodeStatus)
|
2019-03-04 09:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}(pc)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
consumer.Close()
|
|
|
|
}
|