This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
/
main.go
134 lines (117 loc) · 3.26 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
129
130
131
132
133
134
package main
import (
"fmt"
"github.com/blablacar/go-synapse/synapse"
"github.com/ghodss/yaml"
"github.com/n0rad/go-erlog/data"
"github.com/n0rad/go-erlog/errs"
"github.com/n0rad/go-erlog/logs"
_ "github.com/n0rad/go-erlog/register"
"github.com/spf13/cobra"
"io/ioutil"
"math/rand"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"
)
var Version = "No Version Defined"
var BuildTime = "1970-01-01_00:00:00_UTC"
func LoadConfig(configPath string) (*synapse.Synapse, error) {
file, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, errs.WithEF(err, data.WithField("file", configPath), "Failed to read configuration file")
}
conf := &synapse.Synapse{}
err = yaml.Unmarshal(file, conf)
if err != nil {
return nil, errs.WithEF(err, data.WithField("file", configPath), "Invalid configuration format")
}
return conf, nil
}
func waitForSignal() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
<-sigs
logs.Debug("Stop signal received")
}
//func trace() {
// // We don't know how big the traces are, so grow a few times if they don't fit. Start large, though.
// n := 10000
// if all {
// n = 100000
// }
// var trace []byte
// for i := 0; i < 5; i++ {
// trace = make([]byte, n)
// nbytes := runtime.Stack(trace, all)
// if nbytes < len(trace) {
// return trace[:nbytes]
// }
// n *= 2
// }
// return trace
//
//}
func sigQuitThreadDump() {
sigChan := make(chan os.Signal)
go func() {
for range sigChan {
stacktrace := make([]byte, 2<<20)
length := runtime.Stack(stacktrace, true)
fmt.Println(string(stacktrace[:length]))
ioutil.WriteFile("/tmp/"+strconv.Itoa(os.Getpid())+".dump", stacktrace[:length], 0644)
}
}()
signal.Notify(sigChan, syscall.SIGQUIT)
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
sigQuitThreadDump()
var logLevel string
var version bool
var oneshot bool
rootCmd := &cobra.Command{
Use: "synapse config.yml",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if version {
fmt.Println("Synapse")
fmt.Println("Version :", Version)
fmt.Println("Build Time :", BuildTime)
os.Exit(0)
}
if logLevel != "" {
level, err := logs.ParseLevel(logLevel)
if err != nil {
logs.WithField("value", logLevel).Fatal("Unknown log level")
}
logs.SetLevel(level)
}
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
logs.Fatal("Synapse require a configuration file as argument")
}
synapse, err := LoadConfig(args[0])
if err != nil {
logs.WithE(err).Fatal("Cannot start, failed to load configuration")
}
if err := synapse.Init(Version, BuildTime, logLevel != ""); err != nil {
logs.WithE(err).Fatal("Failed to init synapse")
}
if err := synapse.Start(oneshot); err != nil {
logs.WithE(err).Fatal("Failed to start synapse")
}
waitForSignal()
synapse.Stop()
},
}
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "L", "", "Set log level")
rootCmd.PersistentFlags().BoolVarP(&version, "version", "V", false, "Display version")
//rootCmd.PersistentFlags().BoolVarP(&oneshot, "oneshot", "O", false, "run watchers/router only once and exit")
if err := rootCmd.Execute(); err != nil {
logs.WithE(err).Fatal("Failed to process args")
}
}