1 Unstar Star 0 Fork 2

Jin / distill-infraGoApache-2.0

forked from distill / distill-infra 
Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
Go基础设施框架 spread retract

Clone or download
boot.go 1.92 KB
Copy Edit Web IDE Raw Blame History
Lyndon Hu authored 2020-05-03 09:53 . infra toml
package infra
import (
conf "gitee.com/banyanhouse/distill-infra/config"
log "github.com/sirupsen/logrus"
"reflect"
)
//应用程序
type BootApplication struct {
IsTest bool
conf *conf.TomlConfig
starterCtx StarterContext
}
//构造系统
func New(conf *conf.TomlConfig) *BootApplication {
e := &BootApplication{conf: conf, starterCtx: StarterContext{}}
e.starterCtx.SetProps(conf)
return e
}
func (b *BootApplication) Start() {
//1. 初始化starter
b.init()
//2. 安装starter
b.setup()
//3. 启动starter
b.start()
}
//程序初始化
func (e *BootApplication) init() {
log.Info("Initializing starters...")
for _, v := range GetStarters() {
typ := reflect.TypeOf(v)
log.Debugf("Initializing: PriorityGroup=%d,Priority=%d,type=%s", v.PriorityGroup(), v.Priority(), typ.String())
v.Init(e.starterCtx)
}
}
//程序安装
func (e *BootApplication) setup() {
log.Info("Setup starters...")
for _, v := range GetStarters() {
typ := reflect.TypeOf(v)
log.Debug("Setup: ", typ.String())
v.Setup(e.starterCtx)
}
}
//程序开始运行,开始接受调用
func (e *BootApplication) start() {
log.Info("Starting starters...")
for i, v := range GetStarters() {
typ := reflect.TypeOf(v)
log.Debug("Starting: ", typ.String())
if e.starterCtx.Props().App.Testing {
go v.Start(e.starterCtx)
continue
}
if v.StartBlocking() {
// 如果是阻塞的Start
// 最后一个的阻塞式Start,让其阻塞执行
if i+1 == len(GetStarters()) {
v.Start(e.starterCtx)
} else {
// 其余非最后一个,则启动协程执行
go v.Start(e.starterCtx)
}
} else {
// 非阻塞式Start则可以直接运行加载
v.Start(e.starterCtx)
}
}
}
//程序开始运行,开始接受调用
func (e *BootApplication) Stop() {
log.Info("Stoping starters...")
for _, v := range GetStarters() {
typ := reflect.TypeOf(v)
log.Debug("Stoping: ", typ.String())
v.Stop(e.starterCtx)
}
}

Comment ( 0 )

Sign in for post a comment

Go
1
https://gitee.com/sojasosse/distill-infra.git
git@gitee.com:sojasosse/distill-infra.git
sojasosse
distill-infra
distill-infra
master

Search

132457 8cb2edc1 1899542 131848 70c8d3a4 1899542