修改module模块使用

This commit is contained in:
boyce
2019-02-20 15:38:30 +08:00
parent 5b932edd74
commit 9384227cf6
4 changed files with 99 additions and 103 deletions

View File

@@ -15,20 +15,16 @@ type GlobalModule struct {
var g_module GlobalModule var g_module GlobalModule
func AddModule(module service.IModule) bool { func AddModule(module service.IModule) uint32 {
if module.GetModuleType() == 0 {
return false
}
return g_module.AddModule(module) return g_module.AddModule(module)
} }
func GetModuleByType(moduleType uint32) service.IModule { func GetModuleById(moduleId uint32) service.IModule {
return g_module.GetModuleByType(moduleType) return g_module.GetModuleById(moduleId)
} }
func GetLog(logmodule uint32) sysmodule.ILogger { func GetLog(logmodule uint32) sysmodule.ILogger {
module := g_module.GetModuleByType(logmodule) module := g_module.GetModuleById(logmodule)
if nil == module { if nil == module {
return nil return nil
} }
@@ -36,10 +32,10 @@ func GetLog(logmodule uint32) sysmodule.ILogger {
return module.(sysmodule.ILogger) return module.(sysmodule.ILogger)
} }
func InitGlobalModule() { func InitGlobalModule(exit chan bool, pwaitGroup *sync.WaitGroup) {
g_module.InitModule(&g_module) g_module.InitModule(exit, pwaitGroup)
} }
func RunGlobalModule(exit chan bool, pwaitGroup *sync.WaitGroup) { func RunGlobalModule() {
g_module.RunModule(&g_module, exit, pwaitGroup) g_module.RunModule(&g_module)
} }

View File

@@ -29,14 +29,15 @@ type COriginNode struct {
} }
func (s *COriginNode) Init() { func (s *COriginNode) Init() {
//初始化全局模块 //s.exit = make(chan bool)
InitGlobalModule() //s.waitGroup = &sync.WaitGroup{}
service.InitLog()
imodule := g_module.GetModuleByType(sysmodule.SYS_LOG) //初始化全局模块
service.InstanceServiceMgr().Init(imodule.(service.ILogger))
service.InitLog()
imodule := g_module.GetModuleById(sysmodule.SYS_LOG)
service.InstanceServiceMgr().Init(imodule.(service.ILogger), s.exit, s.waitGroup)
s.exit = make(chan bool)
s.waitGroup = &sync.WaitGroup{}
s.sigs = make(chan os.Signal, 1) s.sigs = make(chan os.Signal, 1)
signal.Notify(s.sigs, syscall.SIGINT, syscall.SIGTERM) signal.Notify(s.sigs, syscall.SIGINT, syscall.SIGTERM)
} }
@@ -79,8 +80,8 @@ func (s *COriginNode) Start() {
} }
cluster.InstanceClusterMgr().Start() cluster.InstanceClusterMgr().Start()
RunGlobalModule(s.exit, s.waitGroup) RunGlobalModule()
service.InstanceServiceMgr().Start(s.exit, s.waitGroup) service.InstanceServiceMgr().Start()
select { select {
case <-s.sigs: case <-s.sigs:
@@ -96,9 +97,13 @@ func (s *COriginNode) Stop() {
} }
func NewOrginNode() *COriginNode { func NewOrginNode() *COriginNode {
node := new(COriginNode)
node.exit = make(chan bool)
node.waitGroup = &sync.WaitGroup{}
InitGlobalModule(node.exit, node.waitGroup)
var syslogmodule sysmodule.LogModule var syslogmodule sysmodule.LogModule
syslogmodule.Init("system", sysmodule.LEVER_INFO) syslogmodule.Init("system", sysmodule.LEVER_INFO)
syslogmodule.SetModuleType(sysmodule.SYS_LOG) syslogmodule.SetModuleId(sysmodule.SYS_LOG)
AddModule(&syslogmodule) AddModule(&syslogmodule)
err := cluster.InstanceClusterMgr().Init() err := cluster.InstanceClusterMgr().Init()
@@ -107,7 +112,7 @@ func NewOrginNode() *COriginNode {
return nil return nil
} }
return new(COriginNode) return node
} }
func HasCmdParam(param string) bool { func HasCmdParam(param string) bool {

View File

@@ -18,16 +18,17 @@ type MethodInfo struct {
} }
type IModule interface { type IModule interface {
SetModuleType(moduleType uint32) SetModuleId(moduleId uint32) bool
GetModuleType() uint32 GetModuleId() uint32
DynamicRun(module IModule) GetModuleById(moduleId uint32) IModule
RunModule(module IModule, exit chan bool, pwaitGroup *sync.WaitGroup) error AddModule(module IModule) uint32
InitModule(module IModule) error //DynamicAddModule(module IModule) uint32
RunModule(module IModule) error
InitModule(exit chan bool, pwaitGroup *sync.WaitGroup) error
OnInit() error OnInit() error
OnRun() bool OnRun() bool
OnEndRun()
AddModule(module IModule) bool
GetModuleByType(moduleType uint32) IModule
GetOwnerService() IService GetOwnerService() IService
SetOwnerService(iservice IService) SetOwnerService(iservice IService)
} }
@@ -35,7 +36,6 @@ type IModule interface {
type IService interface { type IService interface {
Init(Iservice IService, servicetype int) error Init(Iservice IService, servicetype int) error
OnInit() error OnInit() error
OnEndInit() error
OnRun() bool OnRun() bool
OnFetchService(iservice IService) error OnFetchService(iservice IService) error
OnSetupService(iservice IService) //其他服务被安装 OnSetupService(iservice IService) //其他服务被安装
@@ -70,19 +70,20 @@ type BaseService struct {
serviceid int serviceid int
servicename string servicename string
servicetype int servicetype int
Status int
Status int
} }
type BaseModule struct { type BaseModule struct {
moduleType uint32 moduleId uint32
mapModule map[uint32]IModule mapModule map[uint32]IModule
ownerService IService ownerService IService
tickTime int64 tickTime int64
ExitChan chan bool ExitChan chan bool
WaitGroup *sync.WaitGroup WaitGroup *sync.WaitGroup
CurrMaxModuleId uint32
} }
func (slf *BaseService) GetServiceId() int { func (slf *BaseService) GetServiceId() int {
@@ -106,10 +107,6 @@ func (slf *BaseService) GetStatus() int {
return slf.Status return slf.Status
} }
func (slf *BaseService) OnEndInit() error {
return nil
}
func (slf *BaseService) OnFetchService(iservice IService) error { func (slf *BaseService) OnFetchService(iservice IService) error {
return nil return nil
} }
@@ -152,39 +149,20 @@ func (slf *BaseService) IsTimeOutTick(microSecond int64) bool {
nowtm := time.Now().UnixNano() / 1e6 nowtm := time.Now().UnixNano() / 1e6
return nowtm-slf.tickTime >= microSecond return nowtm-slf.tickTime >= microSecond
} }
func (slf *BaseModule) SetModuleType(moduleType uint32) { func (slf *BaseModule) SetModuleId(moduleId uint32) bool {
slf.moduleType = moduleType
}
func (slf *BaseModule) GetModuleType() uint32 { slf.moduleId = moduleId
return slf.moduleType
}
func (slf *BaseModule) AddModule(module IModule) bool {
if module.GetModuleType() == 0 {
return false
}
module.SetOwnerService(slf.ownerService)
if slf.mapModule == nil {
slf.mapModule = make(map[uint32]IModule)
}
_, ok := slf.mapModule[module.GetModuleType()]
if ok == true {
return false
}
slf.mapModule[module.GetModuleType()] = module
return true return true
} }
func (slf *BaseModule) GetModuleByType(moduleType uint32) IModule { func (slf *BaseModule) GetModuleId() uint32 {
ret, ok := slf.mapModule[moduleType] return slf.moduleId
}
func (slf *BaseModule) GetModuleById(moduleId uint32) IModule {
ret, ok := slf.mapModule[moduleId]
if ok == false { if ok == false {
return nil return nil
} }
@@ -192,18 +170,50 @@ func (slf *BaseModule) GetModuleByType(moduleType uint32) IModule {
return ret return ret
} }
func (slf *BaseModule) genModuleId() uint32 {
slf.CurrMaxModuleId++
return slf.CurrMaxModuleId
}
func (slf *BaseModule) AddModule(module IModule) uint32 {
if slf.WaitGroup == nil {
GetLogger().Printf(LEVER_FATAL, "AddModule error %s...", fmt.Sprintf("%T", module))
}
if module.GetModuleId() > 100000000 {
return 0
}
if module.GetModuleId() == 0 {
module.SetModuleId(slf.genModuleId())
}
module.SetOwnerService(slf.ownerService)
module.InitModule(slf.ExitChan, slf.WaitGroup)
if slf.mapModule == nil {
slf.mapModule = make(map[uint32]IModule)
}
_, ok := slf.mapModule[module.GetModuleId()]
if ok == true {
return 0
}
slf.mapModule[module.GetModuleId()] = module
go module.RunModule(module)
return module.GetModuleId()
}
func (slf *BaseModule) OnInit() error { func (slf *BaseModule) OnInit() error {
return fmt.Errorf("not implement OnInit moduletype %d ", slf.GetModuleType()) return fmt.Errorf("not implement OnInit moduletype %d ", slf.GetModuleId())
} }
func (slf *BaseModule) OnRun() bool { func (slf *BaseModule) OnRun() bool {
return false return false
} }
func (slf *BaseModule) OnEndRun() {
}
func (slf *BaseModule) GetOwnerService() IService { func (slf *BaseModule) GetOwnerService() IService {
return slf.ownerService return slf.ownerService
} }
@@ -212,33 +222,27 @@ func (slf *BaseModule) SetOwnerService(iservice IService) {
slf.ownerService = iservice slf.ownerService = iservice
} }
func (slf *BaseModule) InitModule(module IModule) error { func (slf *BaseModule) InitModule(exit chan bool, pwaitGroup *sync.WaitGroup) error {
module.OnInit() slf.CurrMaxModuleId = 100000
for _, subModule := range slf.mapModule { slf.WaitGroup = pwaitGroup
go subModule.OnInit() slf.ExitChan = exit
}
return nil return nil
} }
func (slf *BaseModule) DynamicRun(module IModule) { func (slf *BaseModule) RunModule(module IModule) error {
module.InitModule(module)
module.RunModule(module, slf.ExitChan, slf.WaitGroup) module.OnInit()
}
func (slf *BaseModule) RunModule(module IModule, exit chan bool, pwaitGroup *sync.WaitGroup) error {
slf.ExitChan = exit
slf.WaitGroup = pwaitGroup
//运行所有子模块 //运行所有子模块
for _, subModule := range slf.mapModule { for _, subModule := range slf.mapModule {
go subModule.RunModule(subModule, exit, pwaitGroup) go subModule.RunModule(subModule)
} }
pwaitGroup.Add(1) slf.WaitGroup.Add(1)
defer pwaitGroup.Done() defer slf.WaitGroup.Done()
for { for {
select { select {
case <-exit: case <-slf.ExitChan:
GetLogger().Printf(LEVER_WARN, "stopping module %s...", fmt.Sprintf("%T", slf)) GetLogger().Printf(LEVER_WARN, "stopping module %s...", fmt.Sprintf("%T", slf))
fmt.Println("stopping module %s...", fmt.Sprintf("%T", slf)) fmt.Println("stopping module %s...", fmt.Sprintf("%T", slf))
return nil return nil
@@ -250,6 +254,5 @@ func (slf *BaseModule) RunModule(module IModule, exit chan bool, pwaitGroup *syn
slf.tickTime = time.Now().UnixNano() / 1e6 slf.tickTime = time.Now().UnixNano() / 1e6
} }
module.OnEndRun()
return nil return nil
} }

View File

@@ -44,30 +44,22 @@ func (slf *CServiceManager) FetchService(s FetchService) IService {
return nil return nil
} }
func (slf *CServiceManager) Init(logger ILogger) bool { func (slf *CServiceManager) Init(logger ILogger, exit chan bool, pwaitGroup *sync.WaitGroup) bool {
slf.logger = logger slf.logger = logger
for _, s := range slf.localserviceMap { for _, s := range slf.localserviceMap {
(s.(IModule)).InitModule(s.(IModule)) (s.(IModule)).InitModule(exit, pwaitGroup)
} //(s.(IModule)).OnInit()
// 初始化结束
for _, s := range slf.localserviceMap {
if s.OnEndInit() != nil {
return false
}
} }
return true return true
} }
func (slf *CServiceManager) Start(exit chan bool, pwaitGroup *sync.WaitGroup) bool { func (slf *CServiceManager) Start() bool {
for _, s := range slf.localserviceMap { for _, s := range slf.localserviceMap {
go (s.(IModule)).RunModule(s.(IModule), exit, pwaitGroup) go (s.(IModule)).RunModule(s.(IModule))
} }
pwaitGroup.Add(1)
//go slf.CheckServiceTimeTimeout(exit, pwaitGroup)
return true return true
} }