/
run.go
175 lines (153 loc) · 3.47 KB
/
run.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"syscall"
"time"
"github.com/lmittmann/tint"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
"github.com/mycoria/mycoria"
"github.com/mycoria/mycoria/config"
)
func init() {
rootCmd.AddCommand(runCmd)
}
var (
runCmd = &cobra.Command{
Use: "run",
RunE: run,
}
sigUSR1 = syscall.Signal(0xa)
)
func run(cmd *cobra.Command, args []string) error {
c, err := config.LoadConfig(*configFile)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
// Modify config.
c.SetDevMode(*devMode)
// Get log level.
level := slog.LevelInfo
if logLevel != nil && *logLevel != "" {
// TODO: Is this really the best way to do this?
if err := level.UnmarshalText([]byte(*logLevel)); err != nil {
return fmt.Errorf("invalid log level: %w", err)
}
}
// Setup logging.
// Define output.
logOutput := os.Stdout
// Create handler depending on OS.
var logHandler slog.Handler
switch runtime.GOOS {
case "windows":
logHandler = tint.NewHandler(
colorable.NewColorable(logOutput),
&tint.Options{
AddSource: true,
Level: level,
TimeFormat: time.DateTime,
},
)
case "linux":
logHandler = tint.NewHandler(logOutput, &tint.Options{
AddSource: true,
Level: level,
TimeFormat: time.DateTime,
NoColor: !isatty.IsTerminal(logOutput.Fd()),
})
default:
logHandler = tint.NewHandler(os.Stdout, &tint.Options{
AddSource: true,
Level: level,
TimeFormat: time.DateTime,
NoColor: true,
})
}
// Set as default logger.
slog.SetDefault(slog.New(logHandler))
slog.SetLogLoggerLevel(level)
// Setup up everything.
myco, err := mycoria.New(Version, c)
if err != nil {
return fmt.Errorf("failed to initialize mycoria: %w", err)
}
// Log own Router ID on start.
slog.Info(
"starting mycoria",
"version", Version,
"id", c.Router.Address.IP,
)
// Finalize and start all workers.
err = myco.Start()
if err != nil {
return fmt.Errorf("failed to start mycoria: %w", err)
}
// Wait for signal.
signalCh := make(chan os.Signal, 1)
signal.Notify(
signalCh,
os.Interrupt,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
sigUSR1,
)
signalLoop:
for {
select {
case sig := <-signalCh:
// Only print and continue to wait if SIGUSR1
if sig == sigUSR1 {
printStackTo(os.Stderr, "PRINTING STACK ON REQUEST")
continue signalLoop
}
fmt.Println(" <INTERRUPT>") // CLI output.
slog.Warn("program was interrupted, stopping")
// catch signals during shutdown
go func() {
forceCnt := 5
for {
<-signalCh
forceCnt--
if forceCnt > 0 {
fmt.Printf(" <INTERRUPT> again, but already shutting down - %d more to force\n", forceCnt)
} else {
printStackTo(os.Stderr, "PRINTING STACK ON FORCED EXIT")
os.Exit(1)
}
}
}()
go func() {
time.Sleep(3 * time.Minute)
printStackTo(os.Stderr, "PRINTING STACK - TAKING TOO LONG FOR SHUTDOWN")
os.Exit(1)
}()
if !myco.Stop() {
slog.Error("failed to stop mycoria")
os.Exit(1)
}
break signalLoop
case <-myco.Done():
break signalLoop
}
}
return nil
}
func printStackTo(writer io.Writer, msg string) {
_, err := fmt.Fprintf(writer, "===== %s =====\n", msg)
if err == nil {
err = pprof.Lookup("goroutine").WriteTo(writer, 1)
}
if err != nil {
slog.Error("failed to write stack trace", "err", err)
}
}