-
Notifications
You must be signed in to change notification settings - Fork 2
/
nexus.go
140 lines (121 loc) · 2.78 KB
/
nexus.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
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
. "github.com/jaracil/nexus/log"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
var (
nodeId string
mainContext context.Context
mainCancel context.CancelFunc
sesNotify *Notifier = NewNotifier()
sigChan chan os.Signal = make(chan os.Signal, 1)
listenContext context.Context
listenCancel context.CancelFunc
)
func signalManager() {
for s := range sigChan {
switch s {
case syscall.SIGINT:
if listenContext.Err() == nil {
Log.Warnln("Stopping new connections")
listenCancel()
go func() {
for numconn > 0 {
time.Sleep(time.Second)
}
exit("there is no connection left")
}()
} else {
exit("system INT signal")
}
case syscall.SIGTERM:
exit("system TERM signal")
case syscall.SIGKILL:
exit("system KILL signal")
case syscall.SIGUSR1:
listenCancel()
case syscall.SIGUSR2:
if listenContext.Err() != nil {
listen()
}
default:
}
}
}
func exit(cause string) {
if mainContext.Err() == nil {
Log.WithFields(logrus.Fields{
"cause": cause,
}).Error("Daemon exit")
mainCancel()
}
}
func main() {
parseOptions()
if opts.Version {
fmt.Println(Version.String())
os.Exit(0)
}
nodeId = safeId(4)
if len(opts.Verbose) > 0 {
SetLogLevel(DebugLevel)
} else {
SetLogLevel(InfoLevel)
}
if opts.IsProduction {
customFormatter := new(logrus.JSONFormatter)
customFormatter.TimestampFormat = TimestampFormat
Logger.Formatter = customFormatter
}
Log = GetLogger(nodeId, opts.Logs.AddSystemInfo)
if opts.Logs.Path != "" {
// Test if file will be accessible
fd, err := os.OpenFile(opts.Logs.Path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
Log.WithFields(logrus.Fields{
"error": err,
}).Fatalln("Error opening logfile")
}
fd.Close()
logfileFormatter := lfshook.NewHook(lfshook.PathMap{
logrus.DebugLevel: opts.Logs.Path,
logrus.InfoLevel: opts.Logs.Path,
logrus.WarnLevel: opts.Logs.Path,
logrus.ErrorLevel: opts.Logs.Path,
logrus.FatalLevel: opts.Logs.Path,
logrus.PanicLevel: opts.Logs.Path,
}, Logger.Formatter)
Logger.Hooks.Add(logfileFormatter)
}
signal.Notify(sigChan)
go signalManager()
mainContext, mainCancel = context.WithCancel(context.Background())
err := dbOpen()
if err != nil {
Log.WithFields(logrus.Fields{
"error": err,
}).Fatal("Error opening RethinkDB connection")
}
defer db.Close()
go nodeTrack()
go taskTrack()
go pipeTrack()
go sessionTrack()
go taskPurge()
go hooksTrack()
listen()
Log.Infoln("Nexus", Version.String(), "node started")
<-mainContext.Done()
cleanNode(nodeId)
for numconn > 0 {
time.Sleep(time.Second)
}
Log.Warnln("Nexus", Version.String(), "node stopped")
}