-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (102 loc) · 2.98 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"flag"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"github.com/dan13ram/wpokt-validator/app"
"github.com/dan13ram/wpokt-validator/eth"
"github.com/dan13ram/wpokt-validator/models"
"github.com/dan13ram/wpokt-validator/pokt"
log "github.com/sirupsen/logrus"
)
type ServiceFactory = func(*sync.WaitGroup, models.ServiceHealth) app.Service
var ServiceFactoryMap map[string]ServiceFactory = map[string]ServiceFactory{
pokt.MintMonitorName: pokt.NewMintMonitor,
pokt.BurnSignerName: pokt.NewBurnSigner,
pokt.BurnExecutorName: pokt.NewBurnExecutor,
eth.BurnMonitorName: eth.NewBurnMonitor,
eth.MintSignerName: eth.NewMintSigner,
eth.MintExecutorName: eth.NewMintExecutor,
}
func main() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
logLevel := strings.ToLower(os.Getenv("LOG_LEVEL"))
if logLevel == "debug" {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
var configPath string
var envPath string
flag.StringVar(&configPath, "config", "", "path to config file")
flag.StringVar(&envPath, "env", "", "path to env file")
flag.Parse()
var absConfigPath string = ""
var err error
if configPath != "" {
absConfigPath, err = filepath.Abs(configPath)
if err != nil {
log.Fatal("[MAIN] Error getting absolute path for config file: ", err)
}
}
var absEnvPath string = ""
if envPath != "" {
absEnvPath, err = filepath.Abs(envPath)
if err != nil {
log.Fatal("[MAIN] Error getting absolute path for env file: ", err)
}
}
app.InitConfig(absConfigPath, absEnvPath)
app.InitLogger()
app.InitDB()
pokt.ValidateNetwork()
eth.ValidateNetwork()
healthcheck := app.NewHealthCheck()
serviceHealthMap := make(map[string]models.ServiceHealth)
if app.Config.HealthCheck.ReadLastHealth {
if lastHealth, err := healthcheck.FindLastHealth(); err == nil {
for _, serviceHealth := range lastHealth.ServiceHealths {
serviceHealthMap[serviceHealth.Name] = serviceHealth
}
}
}
services := []app.Service{}
var wg sync.WaitGroup
for serviceName, NewService := range ServiceFactoryMap {
health := models.ServiceHealth{}
if lastHealth, ok := serviceHealthMap[serviceName]; ok {
health = lastHealth
}
services = append(services, NewService(&wg, health))
}
services = append(services, app.NewHealthService(healthcheck, &wg))
healthcheck.SetServices(services)
wg.Add(len(services))
for _, service := range services {
go service.Start()
}
log.Info("[MAIN] Server started")
gracefulStop := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(gracefulStop, syscall.SIGINT, syscall.SIGTERM)
go waitForExitSignals(gracefulStop, done)
<-done
log.Debug("[MAIN] Stopping server gracefully")
for _, service := range services {
service.Stop()
}
wg.Wait()
app.DB.Disconnect()
log.Info("[MAIN] Server stopped")
}
func waitForExitSignals(gracefulStop chan os.Signal, done chan bool) {
sig := <-gracefulStop
log.Debug("[MAIN] Caught signal: ", sig)
done <- true
}