/
main.go
51 lines (40 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"context"
"os"
"os/signal"
"sync"
"syscall"
)
func main() {
Log.Info("Ekko service started")
PrintIntro()
// Set up cancellation context and wait group
ctx, cancelFunc := context.WithCancel(context.Background())
wg := &sync.WaitGroup{}
// create the consumer
consumer := NewConsumer()
// Send the servers to ping as events to worker/s
producer := Producer{callbackFunc: consumer.callbackFunc}
go producer.start(ctx)
// Render table and listen for updates
table := NewStatsTable(Config.Servers, consumer.UIEventChan)
go table.listenForChange(ctx)
// Start consumer with cancellation context passed
go consumer.startConsumer(ctx)
// Start workers and Add [workerPoolSize] to WaitGroup
wg.Add(Config.WorkerPoolSize)
for i := 0; i < Config.WorkerPoolSize; i++ {
go consumer.worker(wg, i)
}
// Handle sigterm and await termChan signal
termChan := make(chan os.Signal)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-termChan // Blocks here until interrupted
// Handle shutdown
Log.Warn("Shutdown signal received")
cancelFunc() // Signal cancellation to context.Context
wg.Wait() // Block here until are workers are done
consumer.closeUIChannel()
Log.Debug("All workers stopped, shutting down")
}