-
Notifications
You must be signed in to change notification settings - Fork 497
/
main.go
107 lines (94 loc) · 2.3 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
package main
import (
"log"
"os"
"os/signal"
"sync"
"syscall"
egapi "github.com/megaease/easegateway/pkg/api"
"github.com/megaease/easegateway/pkg/cluster"
"github.com/megaease/easegateway/pkg/common"
"github.com/megaease/easegateway/pkg/env"
"github.com/megaease/easegateway/pkg/graceupdate"
"github.com/megaease/easegateway/pkg/logger"
"github.com/megaease/easegateway/pkg/option"
"github.com/megaease/easegateway/pkg/pidfile"
"github.com/megaease/easegateway/pkg/profile"
"github.com/megaease/easegateway/pkg/supervisor"
"github.com/megaease/easegateway/pkg/version"
// For register stuff.
_ "github.com/megaease/easegateway/pkg/registry"
)
func main() {
opt := option.New()
msg, err := opt.Parse()
if err != nil {
common.Exit(1, err.Error())
}
if msg != "" {
common.Exit(0, msg)
}
option.Global = opt
err = env.InitServerDir(opt)
if err != nil {
log.Printf("failed to init env: %v", err)
os.Exit(1)
}
logger.Init(opt)
defer logger.Sync()
logger.Infof("%s", version.Long)
// disable force-new-cluster for graceful update
if graceupdate.IsInherit() {
opt.ForceNewCluster = false
} else {
err := pidfile.Write(opt)
if err != nil {
logger.Errorf("write pidfile failed: %v", err)
os.Exit(1)
}
}
profile, err := profile.New(opt)
if err != nil {
logger.Errorf("new profile failed: %v", err)
os.Exit(1)
}
cls, err := cluster.New(opt)
if err != nil {
logger.Errorf("new cluster failed: %v", err)
os.Exit(1)
}
super := supervisor.MustNew(opt, cls)
supervisor.InitGlobalSupervisor(super)
api := egapi.MustNewServer(opt, cls)
if graceupdate.CallOriProcessTerm(super.FirstHandleDone()) {
pidfile.Write(opt)
}
closeCls := func() {
wg := &sync.WaitGroup{}
wg.Add(2)
api.Close(wg)
cls.CloseServer(wg)
wg.Wait()
}
restartCls := func() {
cls.StartServer()
api = egapi.MustNewServer(opt, cls)
}
graceupdate.NotifySigUsr2(closeCls, restartCls)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigChan
go func() {
sig := <-sigChan
logger.Infof("%s signal received, closing easegateway immediately", sig)
os.Exit(255)
}()
logger.Infof("%s signal received, closing easegateway", sig)
wg := &sync.WaitGroup{}
wg.Add(4)
api.Close(wg)
super.Close(wg)
cls.Close(wg)
profile.Close(wg)
wg.Wait()
}