-
Notifications
You must be signed in to change notification settings - Fork 56
/
application.go
275 lines (230 loc) · 6.65 KB
/
application.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package cherry
import (
cconst "github.com/cherry-game/cherry/const"
ctime "github.com/cherry-game/cherry/extend/time"
cutils "github.com/cherry-game/cherry/extend/utils"
cfacade "github.com/cherry-game/cherry/facade"
clog "github.com/cherry-game/cherry/logger"
cpacket "github.com/cherry-game/cherry/net/packet"
cserializer "github.com/cherry-game/cherry/net/serializer"
cprofile "github.com/cherry-game/cherry/profile"
"os"
"os/signal"
"reflect"
"sync/atomic"
"syscall"
)
const (
Cluster NodeMode = 1 // 集群模式
Standalone NodeMode = 2 // 单机模式
)
type (
NodeMode byte
Application struct {
cfacade.INode
cfacade.ISerializer
cfacade.IPacketCodec
isFrontend bool
nodeMode NodeMode
startTime ctime.CherryTime // application start time
running int32 // is running
dieChan chan bool // wait for end application
components []cfacade.IComponent // all components
onShutdownFn []func() // on shutdown execute functions
}
)
// NewApp create new application instance
func NewApp(profilePath, profileName, nodeId string) *Application {
node, err := cprofile.Init(profilePath, profileName, nodeId)
if err != nil {
panic(err)
}
// set logger
clog.SetNodeLogger(node)
// print version info
clog.Info(cconst.GetLOGO())
app := &Application{
INode: node,
ISerializer: cserializer.NewProtobuf(),
IPacketCodec: cpacket.NewPomeloCodec(),
isFrontend: true,
nodeMode: Standalone,
startTime: ctime.Now(),
running: 0,
dieChan: make(chan bool),
}
return app
}
func (a *Application) IsFrontend() bool {
return a.isFrontend
}
func (a *Application) NodeMode() NodeMode {
return a.nodeMode
}
func (a *Application) Running() bool {
return a.running > 0
}
func (a *Application) DieChan() chan bool {
return a.dieChan
}
func (a *Application) Register(components ...cfacade.IComponent) {
if a.Running() {
return
}
for _, c := range components {
if c == nil || c.Name() == "" {
clog.Errorf("[component = %T] name is nil", c)
return
}
result := a.Find(c.Name())
if result != nil {
clog.Errorf("[component name = %s] is duplicate.", c.Name())
return
}
a.components = append(a.components, c)
}
}
func (a *Application) Find(name string) cfacade.IComponent {
if name == "" {
return nil
}
for _, component := range a.components {
if component.Name() == name {
return component
}
}
return nil
}
// Remove remove component by name
func (a *Application) Remove(name string) cfacade.IComponent {
if name == "" {
return nil
}
var removeComponent cfacade.IComponent
for i := 0; i < len(a.components); i++ {
if a.components[i].Name() == name {
removeComponent = a.components[i]
a.components = append(a.components[:i], a.components[i+1:]...)
i--
}
}
return removeComponent
}
func (a *Application) All() []cfacade.IComponent {
return a.components
}
func (a *Application) StartTime() string {
return a.startTime.ToDateTimeFormat()
}
// Startup load components before startup
func (a *Application) Startup(components ...cfacade.IComponent) {
defer func() {
if r := recover(); r != nil {
clog.Error(r)
}
}()
if a.Running() {
clog.Error("application has running.")
return
}
defer func() {
clog.Flush()
}()
clog.Info("-------------------------------------------------")
clog.Infof("[nodeId = %s] application is starting...", a.NodeId())
clog.Infof("[nodeType = %s]", a.NodeType())
clog.Infof("[pid = %d]", os.Getpid())
clog.Infof("[startTime = %s]", a.StartTime())
clog.Infof("[profile = %s]", cprofile.Name())
clog.Infof("[profilePath = %s]", cprofile.Dir())
clog.Infof("[profileFile = %s]", cprofile.FileName())
clog.Infof("[printLevel = %s]", cprofile.PrintLevel())
clog.Infof("[isDebug = %v]", cprofile.Debug())
clog.Infof("[logLevel = %s]", clog.DefaultLogger.Level)
clog.Infof("[stackLevel = %s]", clog.DefaultLogger.StackLevel)
clog.Infof("[writeFile = %v]", clog.DefaultLogger.EnableWriteFile)
clog.Infof("[codec = %v]", reflect.TypeOf(a.IPacketCodec))
clog.Infof("[serializer = %s]", a.ISerializer.Name())
clog.Info("-------------------------------------------------")
// add components
a.Register(components...)
// component list
for _, c := range a.components {
c.Set(a)
clog.Infof("[component = %s] is added.", c.Name())
}
clog.Info("-------------------------------------------------")
// execute Init()
for _, c := range a.components {
clog.Infof("[component = %s] -> OnInit().", c.Name())
c.Init()
}
clog.Info("-------------------------------------------------")
//execute OnAfterInit()
for _, c := range a.components {
clog.Infof("[component = %s] -> OnAfterInit().", c.Name())
c.OnAfterInit()
}
clog.Info("-------------------------------------------------")
spendTime := a.startTime.DiffInMillisecond(ctime.Now())
clog.Infof("[spend time = %dms] application is running.", spendTime)
clog.Info("-------------------------------------------------")
// set application is running
atomic.AddInt32(&a.running, 1)
sg := make(chan os.Signal)
signal.Notify(sg, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGTERM)
select {
case <-a.dieChan:
clog.Info("invoke shutdown().")
case s := <-sg:
clog.Infof("receive shutdown signal = %v.", s)
}
// stop status
atomic.StoreInt32(&a.running, 0)
clog.Info("------- application will shutdown -------")
cutils.Try(func() {
if a.onShutdownFn != nil {
for _, f := range a.onShutdownFn {
f()
}
}
}, func(errString string) {
clog.Warnf("[onShutdownFn] error = %s", errString)
})
//all components in reverse order
for i := len(a.components) - 1; i >= 0; i-- {
cutils.Try(func() {
clog.Infof("[component = %s] -> OnBeforeStop().", a.components[i].Name())
a.components[i].OnBeforeStop()
}, func(errString string) {
clog.Warnf("[component = %s] -> OnBeforeStop(). error = %s", a.components[i].Name(), errString)
})
}
for i := len(a.components) - 1; i >= 0; i-- {
cutils.Try(func() {
clog.Infof("[component = %s] -> OnStop().", a.components[i].Name())
a.components[i].OnStop()
}, func(errString string) {
clog.Warnf("[component = %s] -> OnStop(). error = %s", a.components[i].Name(), errString)
})
}
clog.Info("------- application has been shutdown... -------")
}
func (a *Application) OnShutdown(fn ...func()) {
a.onShutdownFn = append(a.onShutdownFn, fn...)
}
func (a *Application) Shutdown() {
a.dieChan <- true
}
func (a *Application) SetSerializer(serializer cfacade.ISerializer) {
if a.Running() {
return
}
a.ISerializer = serializer
}
func (a *Application) SetPacketCodec(codec cfacade.IPacketCodec) {
if a.Running() {
return
}
a.IPacketCodec = codec
}