整理系统日志

This commit is contained in:
boyce
2019-02-21 18:19:48 +08:00
parent bcace08cf0
commit ee61b6145e
6 changed files with 122 additions and 169 deletions

View File

@@ -22,10 +22,9 @@ type IModule interface {
GetModuleId() uint32
GetModuleById(moduleId uint32) IModule
AddModule(module IModule) uint32
//DynamicAddModule(module IModule) uint32
RunModule(module IModule) error
RunModule(module IModule)
InitModule(exit chan bool, pwaitGroup *sync.WaitGroup) error
OnInit() error
OnRun() bool
@@ -89,7 +88,6 @@ type BaseModule struct {
CurrMaxModuleId uint32
rwModuleLocker sync.RWMutex
selfModule IModule
ownerModule IModule
}
@@ -139,15 +137,6 @@ func (slf *BaseService) Init(iservice IService) error {
return nil
}
func (slf *BaseService) RPC_CheckServiceTickTimeOut(microSecond int64) error {
if slf.IsTimeOutTick(microSecond) == true {
// Log.Printf("service:%s is timeout,state:%d", slf.GetServiceName(), slf.GetStatus())
}
return nil
}
func (slf *BaseService) IsTimeOutTick(microSecond int64) bool {
nowtm := time.Now().UnixNano() / 1e6
@@ -177,7 +166,6 @@ func (slf *BaseModule) GetModuleById(moduleId uint32) IModule {
}
func (slf *BaseModule) genModuleId() uint32 {
slf.rwModuleLocker.Lock()
slf.CurrMaxModuleId++
moduleId := slf.CurrMaxModuleId
@@ -190,7 +178,7 @@ func (slf *BaseModule) RemoveModule(moduleId uint32) bool {
slf.rwModuleLocker.Lock()
_, ok := slf.mapModule[moduleId]
if ok == false {
GetLogger().Printf(LEVER_WARN, "RemoveModule fail %d...", moduleId)
GetLogger().Printf(LEVER_WARN, "%T RemoveModule fail %d...", slf.GetOwner().GetModuleById(slf.GetModuleId()), moduleId)
slf.rwModuleLocker.Unlock()
return false
}
@@ -205,15 +193,25 @@ func (slf *BaseModule) IsRoot() bool {
return slf.GetOwner().GetModuleById(slf.GetModuleId()) == nil
}
const (
//ModuleNone ...
MAX_ALLOW_SET_MODULE_ID = iota + 100000000
INIT_AUTO_INCREMENT
)
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 {
GetLogger().Printf(LEVER_FATAL, "AddModule error wait group is nil:%T...", module)
return 0
}
//用户设置的id不允许大于MAX_ALLOW_SET_MODULE_ID
if module.GetModuleId() > MAX_ALLOW_SET_MODULE_ID {
return 0
}
//如果没有设置自动生成ModuleId
if module.GetModuleId() == 0 {
module.SetModuleId(slf.genModuleId())
}
@@ -224,17 +222,16 @@ func (slf *BaseModule) AddModule(module IModule) uint32 {
} else {
module.SetOwner(slf.GetOwner().GetModuleById(slf.GetModuleId()))
}
}
//设置模块退出信号捕获
module.InitModule(slf.ExitChan, slf.WaitGroup)
//存入父模块中
slf.rwModuleLocker.Lock()
if slf.mapModule == nil {
slf.mapModule = make(map[uint32]IModule)
}
_, ok := slf.mapModule[module.GetModuleId()]
if ok == true {
slf.rwModuleLocker.Unlock()
@@ -244,12 +241,13 @@ func (slf *BaseModule) AddModule(module IModule) uint32 {
slf.mapModule[module.GetModuleId()] = module
slf.rwModuleLocker.Unlock()
//运行模块
go module.RunModule(module)
return module.GetModuleId()
}
func (slf *BaseModule) OnInit() error {
return fmt.Errorf("not implement OnInit moduletype %d ", slf.GetModuleId())
return nil
}
func (slf *BaseModule) OnRun() bool {
@@ -274,7 +272,7 @@ func (slf *BaseModule) SetOwnerService(iservice IService) {
}
func (slf *BaseModule) InitModule(exit chan bool, pwaitGroup *sync.WaitGroup) error {
slf.CurrMaxModuleId = 100000
slf.CurrMaxModuleId = INIT_AUTO_INCREMENT
slf.WaitGroup = pwaitGroup
slf.ExitChan = exit
return nil
@@ -284,33 +282,30 @@ func (slf *BaseModule) getBaseModule() *BaseModule {
return slf
}
func (slf *BaseModule) RunModule(module IModule) error {
func (slf *BaseModule) GetSelf() IModule {
if slf.IsRoot() {
return slf.GetOwner()
}
return slf.GetOwner().GetModuleById(slf.GetModuleId())
}
func (slf *BaseModule) RunModule(module IModule) {
module.OnInit()
//运行所有子模块
slf.rwModuleLocker.RLock()
/*
for _, subModule := range slf.mapModule {
go subModule.RunModule(subModule)
}*/
slf.rwModuleLocker.RUnlock()
slf.WaitGroup.Add(1)
defer slf.WaitGroup.Done()
for {
select {
case <-slf.ExitChan:
GetLogger().Printf(LEVER_WARN, "stopping module %s...", fmt.Sprintf("%T", slf))
fmt.Println("stopping module %s...", fmt.Sprintf("%T", slf))
return nil
GetLogger().Printf(LEVER_WARN, "Stopping module %T...", slf.GetSelf())
fmt.Println("Stopping module %T...", slf.GetSelf())
return
}
if module.OnRun() == false {
break
}
slf.tickTime = time.Now().UnixNano() / 1e6
}
return nil
}

View File

@@ -1,9 +1,7 @@
package service
import (
"fmt"
"sync"
"time"
)
type IServiceManager interface {
@@ -49,7 +47,6 @@ func (slf *CServiceManager) Init(logger ILogger, exit chan bool, pwaitGroup *syn
slf.logger = logger
for _, s := range slf.localserviceMap {
(s.(IModule)).InitModule(exit, pwaitGroup)
//(s.(IModule)).OnInit()
}
return true
@@ -63,52 +60,25 @@ func (slf *CServiceManager) Start() bool {
return true
}
func (slf *CServiceManager) CheckServiceTimeTimeout(exit chan bool, pwaitGroup *sync.WaitGroup) {
defer pwaitGroup.Done()
for {
select {
case <-exit:
fmt.Println("CheckServiceTimeTimeout stopping...")
return
}
for _, s := range slf.localserviceMap {
if s.IsTimeOutTick(20000) == true {
Log.Printf("service:%s is timeout,state:%d", s.GetServiceName(), s.GetStatus())
}
}
time.Sleep(2 * time.Second)
}
}
func (slf *CServiceManager) GenServiceID() int {
slf.genserviceid += 1
return slf.genserviceid
}
func (slf *CServiceManager) Get() bool {
for _, s := range slf.localserviceMap {
go s.OnRun()
}
return true
}
func (slf *CServiceManager) GetLogger() ILogger {
return slf.logger
}
var _self *CServiceManager
var self *CServiceManager
func InstanceServiceMgr() *CServiceManager {
if _self == nil {
_self = new(CServiceManager)
_self.localserviceMap = make(map[string]IService)
return _self
if self == nil {
self = new(CServiceManager)
self.localserviceMap = make(map[string]IService)
return self
}
return _self
return self
}
func GetLogger() ILogger {