-
Notifications
You must be signed in to change notification settings - Fork 35
/
server.go
86 lines (74 loc) · 1.5 KB
/
server.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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/codegangsta/cli"
"github.com/ehazlett/stellar/server"
"github.com/sirupsen/logrus"
)
const (
localhost = "127.0.0.1"
)
var serverCommand = cli.Command{
Name: "server",
Usage: "run the stellar daemon",
Action: serverAction,
Flags: []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "path to config file",
Value: "",
},
},
}
func serverAction(ctx *cli.Context) error {
p := ctx.String("config")
if p == "" {
return fmt.Errorf("config file not specified")
}
cfg, err := loadConfigFromFile(p)
if err != nil {
return err
}
srv, err := server.NewServer(cfg)
if err != nil {
return err
}
if err := srv.Run(); err != nil {
return err
}
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)
doneCh := make(chan bool, 1)
go func() {
for {
select {
case sig := <-signals:
switch sig {
case syscall.SIGUSR1:
logrus.Debug("generating debug profile")
profilePath, err := srv.GenerateProfile()
if err != nil {
logrus.Error(err)
continue
}
logrus.WithFields(logrus.Fields{
"profile": profilePath,
}).Info("generated memory profile")
case syscall.SIGTERM, syscall.SIGINT:
logrus.Info("shutting down")
if err := srv.Stop(); err != nil {
logrus.Error(err)
}
doneCh <- true
default:
logrus.Warnf("unhandled signal %s", sig)
}
}
}
}()
<-doneCh
return nil
}