-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
104 lines (87 loc) · 2.09 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
package main
import (
"flag"
"io"
"log"
"log/slog"
"math/rand"
"os"
"os/signal"
"runtime"
"syscall"
"time"
_ "github.com/joho/godotenv/autoload"
)
const (
eventQuit = iota
)
type sysEventMessage struct {
event int
idata int
}
var sysEventChannel = make(chan sysEventMessage, 5)
var logOutput io.Writer
var startTime time.Time
var logFileName = flag.String("log", "/tmp/ch_registry_server.log", "Log file ('-' for only stderr)")
func main() {
os.Setenv("TZ", "UTC")
startTime = time.Now()
rand.Seed(startTime.UnixNano())
if runtime.GOOS == "windows" {
*logFileName = "c:\\temp\\ch_registry_server.log"
}
flag.Parse()
if *logFileName != "-" {
f, err := os.OpenFile(*logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
if err != nil {
log.Panic("Cannot open log file " + *logFileName)
}
defer f.Close()
logOutput = io.MultiWriter(os.Stderr, f)
} else {
logOutput = os.Stderr
}
log.SetOutput(logOutput)
slog.Info("Starting up...")
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel, syscall.SIGINT)
err := initDb()
if err != nil {
slog.Error("Cannot open database", "err", err)
return
}
//go webServer()
//go infraWebServer()
var m runtime.MemStats
runtime.ReadMemStats(&m)
oldAlloc := int64(m.Alloc)
printMemStats(&m)
for {
select {
case msg := <-sysEventChannel:
switch msg.event {
case eventQuit:
slog.Info("Exiting")
os.Exit(msg.idata)
}
case sig := <-sigChannel:
switch sig {
case syscall.SIGINT:
sysEventChannel <- sysEventMessage{event: eventQuit, idata: 0}
log.Println("^C detected")
}
case <-time.After(60 * time.Second):
runtime.ReadMemStats(&m)
if abs(int64(m.Alloc)-oldAlloc) > 1024*1024 {
printMemStats(&m)
oldAlloc = int64(m.Alloc)
}
case <-time.After(15 * time.Minute):
//cleanupDb()
}
}
}
func printMemStats(m *runtime.MemStats) {
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
slog.Info("Stats:", "alloc", bToMB(m.Alloc), "total_alloc", bToMB(m.TotalAlloc), "sys", bToMB(m.Sys), "num_gc", m.NumGC, "uptime_hrs", time.Since(startTime).Hours())
}