-
Notifications
You must be signed in to change notification settings - Fork 701
/
application.go
522 lines (462 loc) · 13.5 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright 2021 rex lv
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package application
import (
"context"
"fmt"
"sync"
"time"
"github.com/douyu/jupiter/pkg/component"
job "github.com/douyu/jupiter/pkg/worker/xjob"
"github.com/BurntSushi/toml"
"github.com/douyu/jupiter/pkg/conf"
//go-lint
_ "github.com/douyu/jupiter/pkg/conf/datasource/file"
_ "github.com/douyu/jupiter/pkg/conf/datasource/http"
_ "github.com/douyu/jupiter/pkg/registry/etcdv3"
"github.com/douyu/jupiter/internal/hooks"
"github.com/douyu/jupiter/pkg/ecode"
"github.com/douyu/jupiter/pkg/flag"
"github.com/douyu/jupiter/pkg/registry"
"github.com/douyu/jupiter/pkg/server"
"github.com/douyu/jupiter/pkg/signals"
"github.com/douyu/jupiter/pkg/util/xcolor"
"github.com/douyu/jupiter/pkg/util/xcycle"
"github.com/douyu/jupiter/pkg/util/xdebug"
"github.com/douyu/jupiter/pkg/util/xdefer"
"github.com/douyu/jupiter/pkg/util/xgo"
"github.com/douyu/jupiter/pkg/worker"
"github.com/douyu/jupiter/pkg/xlog"
"golang.org/x/sync/errgroup"
)
// Application is the framework's instance, it contains the servers, workers, client and configuration settings.
// Create an instance of Application, by using &Application{}
type Application struct {
cycle *xcycle.Cycle
smu *sync.RWMutex
initOnce sync.Once
startupOnce sync.Once
stopOnce sync.Once
servers []server.Server
workers []worker.Worker
jobs map[string]job.Runner
logger *xlog.Logger
hooks map[uint32]*xdefer.DeferStack
configParser conf.Unmarshaller
disableMap map[Disable]bool
HideBanner bool
stopped chan struct{}
components []component.Component
}
// New create a new Application instance
func New(fns ...func() error) (*Application, error) {
app := &Application{}
if err := app.Startup(fns...); err != nil {
return nil, err
}
return app, nil
}
func DefaultApp() *Application {
app := &Application{}
app.initialize()
return app
}
//run hooks
func (app *Application) runHooks(stage hooks.Stage) {
hooks.Do(stage)
}
//RegisterHooks register a stage Hook
func (app *Application) RegisterHooks(stage hooks.Stage, fns ...func()) {
hooks.Register(stage, fns...)
}
// initialize application
func (app *Application) initialize() {
app.initOnce.Do(func() {
//assign
app.cycle = xcycle.NewCycle()
app.smu = &sync.RWMutex{}
app.servers = make([]server.Server, 0)
app.workers = make([]worker.Worker, 0)
app.jobs = make(map[string]job.Runner)
app.logger = xlog.JupiterLogger
app.configParser = toml.Unmarshal
app.disableMap = make(map[Disable]bool)
app.stopped = make(chan struct{})
app.components = make([]component.Component, 0)
//private method
app.parseFlags()
app.printBanner()
})
}
// // start up application
// // By default the startup composition is:
// // - parse config, watch, version flags
// // - load config
// // - init default biz logger, jupiter frame logger
// // - init procs
// func (app *Application) startup() (err error) {
// app.startupOnce.Do(func() {
// err = xgo.SerialUntilError(
// app.parseFlags,
// // app.printBanner,
// // app.loadConfig,
// // app.initLogger,
// // app.initMaxProcs,
// // app.initTracer,
// // app.initSentinel,
// // app.initGovernor,
// )()
// })
// return
// }
//Startup ..
func (app *Application) Startup(fns ...func() error) error {
app.initialize()
// if err := app.startup(); err != nil {
// return err
// }
return xgo.SerialUntilError(fns...)()
}
// Defer ..
// Deprecated: use AfterStop instead
// func (app *Application) Defer(fns ...func() error) {
// app.AfterStop(fns...)
// }
// BeforeStop hook
// Deprecated: use RegisterHooks instead
// func (app *Application) BeforeStop(fns ...func() error) {
// app.RegisterHooks(StageBeforeStop, fns...)
// }
// AfterStop hook
// Deprecated: use RegisterHooks instead
// func (app *Application) AfterStop(fns ...func() error) {
// app.RegisterHooks(StageAfterStop, fns...)
// }
// Serve start server
func (app *Application) Serve(s ...server.Server) error {
app.smu.Lock()
defer app.smu.Unlock()
app.servers = append(app.servers, s...)
return nil
}
// Schedule ..
func (app *Application) Schedule(w worker.Worker) error {
app.workers = append(app.workers, w)
return nil
}
// Job ..
func (app *Application) Job(runner job.Runner) error {
namedJob, ok := runner.(interface{ GetJobName() string })
// job runner must implement GetJobName
if !ok {
return nil
}
jobName := namedJob.GetJobName()
if flag.Bool("disable-job") {
app.logger.Info("jupiter disable job", xlog.FieldName(jobName))
return nil
}
// start job by name
jobFlag := flag.String("job")
if jobFlag == "" {
app.logger.Error("jupiter jobs flag name empty", xlog.FieldName(jobName))
return nil
}
if jobName != jobFlag {
app.logger.Info("jupiter disable jobs", xlog.FieldName(jobName))
return nil
}
app.logger.Info("jupiter register job", xlog.FieldName(jobName))
app.jobs[jobName] = runner
return nil
}
// SetRegistry set customize registry
// Deprecated, please use registry.DefaultRegisterer instead.
func (app *Application) SetRegistry(reg registry.Registry) {
registry.DefaultRegisterer = reg
}
// SetGovernor set governor addr (default 127.0.0.1:0)
// Deprecated
//func (app *Application) SetGovernor(addr string) {
// app.governorAddr = addr
//}
// Run run application
func (app *Application) Run(servers ...server.Server) error {
app.smu.Lock()
app.servers = append(app.servers, servers...)
app.smu.Unlock()
app.waitSignals() //start signal listen task in goroutine
defer app.clean()
// todo jobs not graceful
app.startJobs()
// start servers and govern server
app.cycle.Run(app.startServers)
// start workers
app.cycle.Run(app.startWorkers)
//blocking and wait quit
if err := <-app.cycle.Wait(); err != nil {
app.logger.Error("jupiter shutdown with error", xlog.FieldMod(ecode.ModApp), xlog.FieldErr(err))
return err
}
app.logger.Info("shutdown jupiter, bye!", xlog.FieldMod(ecode.ModApp))
return nil
}
//clean after app quit
func (app *Application) clean() {
_ = xlog.DefaultLogger.Flush()
_ = xlog.JupiterLogger.Flush()
}
// Stop application immediately after necessary cleanup
func (app *Application) Stop() (err error) {
app.stopOnce.Do(func() {
app.stopped <- struct{}{}
app.runHooks(hooks.Stage_BeforeStop)
//stop servers
app.smu.RLock()
for _, s := range app.servers {
func(s server.Server) {
app.cycle.Run(s.Stop)
}(s)
}
app.smu.RUnlock()
//stop workers
for _, w := range app.workers {
func(w worker.Worker) {
app.cycle.Run(w.Stop)
}(w)
}
<-app.cycle.Done()
app.runHooks(hooks.Stage_AfterStop)
app.cycle.Close()
})
return
}
// GracefulStop application after necessary cleanup
func (app *Application) GracefulStop(ctx context.Context) (err error) {
app.stopOnce.Do(func() {
app.stopped <- struct{}{}
app.runHooks(hooks.Stage_BeforeStop)
//stop servers
app.smu.RLock()
for _, s := range app.servers {
func(s server.Server) {
app.cycle.Run(func() error {
return s.GracefulStop(ctx)
})
}(s)
}
app.smu.RUnlock()
//stop workers
for _, w := range app.workers {
func(w worker.Worker) {
app.cycle.Run(w.Stop)
}(w)
}
<-app.cycle.Done()
app.runHooks(hooks.Stage_AfterStop)
app.cycle.Close()
})
return err
}
// waitSignals wait signal
func (app *Application) waitSignals() {
app.logger.Info("init listen signal", xlog.FieldMod(ecode.ModApp), xlog.FieldEvent("init"))
signals.Shutdown(func(grace bool) { //when get shutdown signal
//todo: support timeout
if grace {
app.GracefulStop(context.TODO())
} else {
app.Stop()
}
})
}
// func (app *Application) initGovernor() error {
// if app.isDisable(DisableDefaultGovernor) {
// app.logger.Info("defualt governor disable", xlog.FieldMod(ecode.ModApp))
// return nil
// }
// config := governor.StdConfig("governor")
// if !config.Enable {
// return nil
// }
// return app.Serve(config.Build())
// }
func (app *Application) startServers() error {
var eg errgroup.Group
var ctx, cancel = context.WithTimeout(context.Background(), time.Second*10)
go func() {
<-app.stopped
cancel()
}()
// start multi servers
for _, s := range app.servers {
s := s
eg.Go(func() (err error) {
registry.DefaultRegisterer.RegisterService(ctx, s.Info())
defer registry.DefaultRegisterer.UnregisterService(ctx, s.Info())
app.logger.Info("start server", xlog.FieldMod(ecode.ModApp), xlog.FieldEvent("init"), xlog.FieldName(s.Info().Name), xlog.FieldAddr(s.Info().Label()), xlog.Any("scheme", s.Info().Scheme))
defer app.logger.Info("exit server", xlog.FieldMod(ecode.ModApp), xlog.FieldEvent("exit"), xlog.FieldName(s.Info().Name), xlog.FieldErr(err), xlog.FieldAddr(s.Info().Label()))
err = s.Serve()
return
})
}
return eg.Wait()
}
func (app *Application) startWorkers() error {
var eg errgroup.Group
// start multi workers
for _, w := range app.workers {
w := w
eg.Go(func() error {
return w.Run()
})
}
return eg.Wait()
}
// todo handle error
func (app *Application) startJobs() error {
if len(app.jobs) == 0 {
return nil
}
var jobs = make([]func(), 0)
//warp jobs
for name, runner := range app.jobs {
jobs = append(jobs, func() {
app.logger.Info("job run begin", xlog.FieldName(name))
defer app.logger.Info("job run end", xlog.FieldName(name))
// runner.Run panic 错误在更上层抛出
runner.Run()
})
}
xgo.Parallel(jobs...)()
return nil
}
//parseFlags init
func (app *Application) parseFlags() error {
if app.isDisable(DisableParserFlag) {
app.logger.Info("parseFlags disable", xlog.FieldMod(ecode.ModApp))
return nil
}
// flag.Register(&flag.StringFlag{
// Name: "config",
// Usage: "--config",
// EnvVar: "JUPITER_CONFIG",
// Default: "",
// Action: func(name string, fs *flag.FlagSet) {},
// })
// flag.Register(&flag.BoolFlag{
// Name: "version",
// Usage: "--version, print version",
// Default: false,
// Action: func(string, *flag.FlagSet) {
// pkg.PrintVersion()
// os.Exit(0)
// },
// })
// flag.Register(&flag.StringFlag{
// Name: "host",
// Usage: "--host, print host",
// Default: "127.0.0.1",
// Action: func(string, *flag.FlagSet) {},
// })
return flag.Parse()
}
//loadConfig init
// func (app *Application) loadConfig() error {
// if app.isDisable(DisableLoadConfig) {
// app.logger.Info("load config disable", xlog.FieldMod(ecode.ModConfig))
// return nil
// }
// var configAddr = flag.String("config")
// provider, err := manager.NewDataSource(configAddr)
// if err != manager.ErrConfigAddr {
// if err != nil {
// app.logger.Panic("data source: provider error", xlog.FieldMod(ecode.ModConfig), xlog.FieldErr(err))
// }
// if err := conf.LoadFromDataSource(provider, app.configParser); err != nil {
// app.logger.Panic("data source: load config", xlog.FieldMod(ecode.ModConfig), xlog.FieldErrKind(ecode.ErrKindUnmarshalConfigErr), xlog.FieldErr(err))
// }
// } else {
// app.logger.Info("no config... ", xlog.FieldMod(ecode.ModConfig))
// }
// return nil
// }
//initLogger init
// func (app *Application) initLogger() error {
// if conf.Get(xlog.ConfigEntry("default")) != nil {
// xlog.DefaultLogger = xlog.RawConfig(constant.ConfigPrefix + ".logger.default").Build()
// }
// xlog.DefaultLogger.AutoLevel(constant.ConfigPrefix + ".logger.default")
// if conf.Get(constant.ConfigPrefix+".logger.jupiter") != nil {
// xlog.JupiterLogger = xlog.RawConfig(constant.ConfigPrefix + ".logger.jupiter").Build()
// }
// xlog.JupiterLogger.AutoLevel(constant.ConfigPrefix + ".logger.jupiter")
// return nil
// }
//initTracer init
// func (app *Application) initTracer() error {
// // init tracing component jaeger
// if conf.Get("jupiter.trace.jaeger") != nil {
// var config = jaeger.RawConfig("jupiter.trace.jaeger")
// trace.SetGlobalTracer(config.Build())
// }
// return nil
// }
//initSentinel init
// func (app *Application) initSentinel() error {
// // init reliability component sentinel
// if conf.Get("jupiter.reliability.sentinel") != nil {
// app.logger.Info("init sentinel")
// return sentinel.RawConfig("jupiter.reliability.sentinel").Build()
// }
// return nil
// }
//initMaxProcs init
// func (app *Application) initMaxProcs() error {
// if maxProcs := conf.GetInt("maxProc"); maxProcs != 0 {
// runtime.GOMAXPROCS(maxProcs)
// } else {
// if _, err := maxprocs.Set(); err != nil {
// app.logger.Panic("auto max procs", xlog.FieldMod(ecode.ModProc), xlog.FieldErrKind(ecode.ErrKindAny), xlog.FieldErr(err))
// }
// }
// app.logger.Info("auto max procs", xlog.FieldMod(ecode.ModProc), xlog.Int64("procs", int64(runtime.GOMAXPROCS(-1))))
// return nil
// }
func (app *Application) isDisable(d Disable) bool {
b, ok := app.disableMap[d]
if !ok {
return false
}
return b
}
//printBanner init
func (app *Application) printBanner() error {
if app.HideBanner {
return nil
}
if xdebug.IsTestingMode() {
return nil
}
const banner = `
(_)_ _ _ __ (_) |_ ___ _ __
| | | | | '_ \| | __/ _ \ '__|
| | |_| | |_) | | || __/ |
_/ |\__,_| .__/|_|\__\___|_|
|__/ |_|
Welcome to jupiter, starting application ...
`
fmt.Println(xcolor.Green(banner))
return nil
}