检查Module退出机制修改

This commit is contained in:
boyce
2019-02-27 14:39:28 +08:00
parent f9ce014dc3
commit 9b48b30d54

View File

@@ -1,10 +1,11 @@
package service package service
import ( import (
"fmt"
"sync/atomic" "sync/atomic"
"sync" "sync"
"github.com/duanhf2012/origin/util"
) )
const ( const (
@@ -260,31 +261,37 @@ func (slf *BaseModule) RunModule(module IModule) {
if err != nil { if err != nil {
GetLogger().Printf(LEVER_ERROR, "Start module %T id is %d is fail,reason:%v...", module, module.GetModuleId(), err) GetLogger().Printf(LEVER_ERROR, "Start module %T id is %d is fail,reason:%v...", module, module.GetModuleId(), err)
} else { } else {
GetLogger().Printf(LEVER_INFO, "Start module %T id is %d...", module, module.GetModuleId()) GetLogger().Printf(LEVER_INFO, "Start module %T ...", module)
} }
//运行所有子模块 //运行所有子模块
timer := util.Timer{}
timer.SetupTimer(1000)
slf.WaitGroup.Add(1) slf.WaitGroup.Add(1)
defer slf.WaitGroup.Done() defer slf.WaitGroup.Done()
for { for {
if atomic.LoadInt32(&slf.corouterstatus) != 0 { //每500ms检查退出
module.OnEndRun() if timer.CheckTimeOut() {
GetLogger().Printf(LEVER_INFO, "Stopping module %T id is %d...", module, module.GetModuleId()) if atomic.LoadInt32(&slf.corouterstatus) != 0 {
break module.OnEndRun()
} GetLogger().Printf(LEVER_INFO, "OnEndRun module %T ...", module)
break
}
select { select {
case <-slf.ExitChan: case <-slf.ExitChan:
module.OnEndRun() module.OnEndRun()
GetLogger().Printf(LEVER_INFO, "Stopping module %T...", module) GetLogger().Printf(LEVER_INFO, "OnEndRun module %T...", module)
fmt.Printf("Stopping module %T...\n", module) return
return default:
default: }
} }
if module.OnRun() == false { if module.OnRun() == false {
module.OnEndRun() module.OnEndRun()
GetLogger().Printf(LEVER_INFO, "OnEndRun module %T...", module)
return return
} }
} }
} }