-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
80 lines (65 loc) · 1.9 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
package main
import (
"io/ioutil"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/Supernomad/nexus/common"
"github.com/Supernomad/nexus/filter"
"github.com/Supernomad/nexus/iface"
"github.com/Supernomad/nexus/worker"
)
func main() {
log := common.NewLogger()
cfg, err := common.NewConfig(log)
if err != nil {
log.Error.Fatalln("[MAIN]", "Configuration issue encountered:", err)
}
backend := iface.New(iface.UDPSocket, log, cfg)
err = backend.Open()
if err != nil {
log.Error.Fatalln("[MAIN]", "Error bringing up the backend networking interface:", err)
}
filters := make([]filter.Filter, 0)
worker := worker.New(log, cfg, backend, filters)
for i := 0; i < cfg.NumWorkers; i++ {
worker.Start(i)
}
signals := make(chan os.Signal, 1)
done := make(chan struct{}, 1)
signal.Notify(signals, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
go func() {
sig := <-signals
switch {
case sig == syscall.SIGHUP:
log.Info.Println("[MAIN]", "Recieved reload signal from user. Reloading process.")
backendFDs := backend.GetFDs()
files := make([]uintptr, 3+cfg.NumWorkers*2)
files[0] = os.Stdin.Fd()
files[1] = os.Stdout.Fd()
files[2] = os.Stderr.Fd()
for i := 0; i < cfg.NumWorkers; i++ {
files[3+i] = uintptr(backendFDs[i])
}
os.Setenv(common.RollingRestart, "restart triggered")
env := os.Environ()
attr := &syscall.ProcAttr{
Env: env,
Files: files,
}
pid, err := syscall.ForkExec(os.Args[0], os.Args, attr)
if err != nil {
log.Error.Fatalln("[MAIN]", "Rolling restart error:", err)
}
ioutil.WriteFile(cfg.PidFile, []byte(strconv.Itoa(pid)), os.ModePerm)
done <- struct{}{}
case sig == syscall.SIGINT || sig == syscall.SIGTERM || sig == syscall.SIGKILL:
log.Info.Println("[MAIN]", "Recieved termination signal from user. Terminating process.")
done <- struct{}{}
}
}()
<-done
worker.Stop()
backend.Close()
}