捕获异常处理

This commit is contained in:
boyce
2019-03-23 15:03:16 +08:00
parent e1987a1578
commit 6095693405
2 changed files with 47 additions and 7 deletions

View File

@@ -139,6 +139,10 @@ import (
"sync" "sync"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
runtimedebug "runtime/debug"
orginservice "github.com/duanhf2012/origin/service"
) )
const ( const (
@@ -389,9 +393,25 @@ func (m *methodType) NumCalls() (n uint) {
} }
func (s *service) call(server *Server, sending *sync.Mutex, wg *sync.WaitGroup, mtype *methodType, req *Request, argv, replyv reflect.Value, codec ServerCodec) { func (s *service) call(server *Server, sending *sync.Mutex, wg *sync.WaitGroup, mtype *methodType, req *Request, argv, replyv reflect.Value, codec ServerCodec) {
defer func() {
if r := recover(); r != nil {
var coreInfo string
str, ok := r.(string)
if ok {
coreInfo += str + "\n" + string(runtimedebug.Stack())
} else {
coreInfo = "Panic!"
}
coreInfo += "\nCore Request RPC Name:" + req.ServiceMethod
orginservice.GetLogger().Printf(orginservice.LEVER_FATAL, coreInfo)
}
}()
if wg != nil { if wg != nil {
defer wg.Done() defer wg.Done()
} }
mtype.Lock() mtype.Lock()
mtype.numCalls++ mtype.numCalls++
mtype.Unlock() mtype.Unlock()

View File

@@ -1,10 +1,11 @@
package service package service
import ( import (
"fmt"
"os" "os"
"sync/atomic" "runtime/debug"
"sync" "sync"
"sync/atomic"
"github.com/duanhf2012/origin/util" "github.com/duanhf2012/origin/util"
) )
@@ -54,6 +55,8 @@ type BaseModule struct {
ExitChan chan bool ExitChan chan bool
WaitGroup *sync.WaitGroup WaitGroup *sync.WaitGroup
bInit bool bInit bool
recoverCount int8
} }
func (slf *BaseModule) GetRoot() IModule { func (slf *BaseModule) GetRoot() IModule {
@@ -278,19 +281,36 @@ func (slf *BaseModule) IsInit() bool {
} }
func (slf *BaseModule) RunModule(module IModule) { func (slf *BaseModule) RunModule(module IModule) {
/*err := module.OnInit()
if err != nil {
GetLogger().Printf(LEVER_ERROR, "Start module %T id is %d is fail,reason:%v...", module, module.GetModuleId(), err)
os.Exit(-1)
}*/
GetLogger().Printf(LEVER_INFO, "Start Run module %T ...", module) GetLogger().Printf(LEVER_INFO, "Start Run module %T ...", module)
defer func() {
if r := recover(); r != nil {
var coreInfo string
str, ok := r.(string)
if ok {
coreInfo += str + "\n" + string(debug.Stack())
} else {
coreInfo = "Panic!"
}
coreInfo += "\n" + fmt.Sprintf("Core module is %T, try count %d.\n", module, slf.recoverCount)
GetLogger().Printf(LEVER_FATAL, coreInfo)
slf.recoverCount += 1
//重试3次
if slf.recoverCount < 4 {
go slf.RunModule(slf.GetSelf())
} else {
GetLogger().Printf(LEVER_FATAL, "Routine %T.OnRun has exited!", module)
}
}
}()
//运行所有子模块 //运行所有子模块
timer := util.Timer{} timer := util.Timer{}
timer.SetupTimer(1000) 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 { if atomic.LoadInt32(&slf.corouterstatus) != 0 {
module.OnEndRun() module.OnEndRun()
GetLogger().Printf(LEVER_INFO, "OnEndRun module %T ...", module) GetLogger().Printf(LEVER_INFO, "OnEndRun module %T ...", module)