-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (88 loc) · 2.82 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
package main
import (
"context"
"os"
"os/signal"
"strings"
"time"
"gitlab.cern.ch/lb-experts/goermis/alarms"
"gitlab.cern.ch/lb-experts/goermis/api/ermis"
"gitlab.cern.ch/lb-experts/goermis/bootstrap"
"gitlab.cern.ch/lb-experts/goermis/db"
"gitlab.cern.ch/lb-experts/goermis/router"
"gitlab.cern.ch/lb-experts/goermis/views"
)
var (
// Version number
// This should be overwritten with `go build -ldflags "-X main.Version='HELLO_THERE'"`
Version = "head"
// Release number
// It should also be overwritten
Release = "no_release"
log = bootstrap.GetLog()
cfg = bootstrap.GetConf()
)
func main() {
ermis.SetVersion(Version, Release)
bootstrap.ParseFlags()
bootstrap.SetLogLevel()
log.Infof("============Service Started. Ermis version %v-%v =============", Version, Release)
// Echo instance
echo := router.New()
err := db.InitDB()
if err != nil {
log.Error("Error with the database")
return
}
defer db.Close()
//Initiate template views
views.InitViews(echo)
autoMigrateTables()
//Alarms periodic check/update
ticker := time.NewTicker(time.Duration(cfg.Timers.Alarms) * time.Minute)
/*done channel can be used to stop the ticker if needed,
by issuing the command "done<-true". For now, it runs constantly */
done := make(chan bool)
go func() {
for {
select {
case <-done:
ticker.Stop()
return
case <-ticker.C:
log.Debugf("%v minutes passed, preparing to check for active alarms", cfg.Timers.Alarms)
alarms.PeriodicAlarmCheck()
}
}
}()
log.Debug("alarms updated")
/* Start server
Error handling is done a bit differently in this situation. The reason is that
when server is restarted we force it to reuse the same socket. Despite being successfully
restarted, it throws a bind error. This interferes with other important cases where we need
to shut down the service */
go func() {
err := echo.StartTLS(":8080",
cfg.Certs.ErmisCert,
cfg.Certs.ErmisKey)
//Avoiding uneccesary logs and failures when restarting
if !strings.HasSuffix(err.Error(), "bind: address already in use") {
log.Fatalf("Failed to start server: %v", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 10 seconds. It is needed to accomplish socket recycling
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
s := <-quit
log.Infof("received quit signal %v", s)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := echo.Shutdown(ctx); err != nil {
log.Fatal("Fatal error while shutting server down " + err.Error())
}
}
// autoMigrateTables: migrate table columns using GORM. Will not delete/change types for security reasons
func autoMigrateTables() {
db.GetConn().AutoMigrate(&ermis.Alias{}, &ermis.Node{}, &ermis.Cname{}, &ermis.Alarm{}, &ermis.Relation{})
}