1 Star 0 Fork 1

sagmain/distill-infra

forked from distill/distill-infra 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
boot.go 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
Lyndon Hu 提交于 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)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/sagmain/distill-infra.git
git@gitee.com:sagmain/distill-infra.git
sagmain
distill-infra
distill-infra
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385