内部异常捕获
This commit is contained in:
parent
0c3791c591
commit
c3fe1f6427
@ -10,3 +10,12 @@ func Sleep(min, max time.Duration) {
|
||||
t := time.Duration(rand.Int63n(ulit)) + min
|
||||
time.Sleep(t)
|
||||
}
|
||||
|
||||
func Retry(count int, f func() bool) {
|
||||
for i := 0; i < count; i++ {
|
||||
if f() {
|
||||
break
|
||||
}
|
||||
Sleep(time.Second*1, time.Second*5)
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,21 @@
|
||||
package busi
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/sirupsen/logrus"
|
||||
"legu.airobot/lib"
|
||||
"legu.airobot/pb"
|
||||
)
|
||||
|
||||
//好友场景
|
||||
|
||||
var _ lib.IScene = (*FriendScene)(nil)
|
||||
|
||||
const (
|
||||
Friend_Maintype = "friend"
|
||||
)
|
||||
|
||||
type FriendScene struct {
|
||||
lib.Action
|
||||
}
|
||||
@ -21,9 +28,65 @@ func (f *FriendScene) Info() lib.SceneInfo {
|
||||
}
|
||||
|
||||
func (f *FriendScene) Run(robot lib.IRobot) error {
|
||||
req := &pb.FriendRandlistReq{}
|
||||
rsp := &pb.FriendRandlistResp{}
|
||||
robot.SendMsg("", "", req, rsp)
|
||||
|
||||
friendList := friendRandlist(robot)
|
||||
Sleep(time.Second, time.Second*3)
|
||||
friendApply(robot, friendList)
|
||||
Sleep(time.Second, time.Second*3)
|
||||
friendAgree(robot)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 好友推荐
|
||||
func friendRandlist(robot lib.IRobot) *pb.FriendRandlistResp {
|
||||
req := &pb.FriendRandlistReq{}
|
||||
rsp := &pb.FriendRandlistResp{}
|
||||
if code := robot.SendMsg(Friend_Maintype, "randlist", req, rsp); code != pb.ErrorCode_Success {
|
||||
logrus.WithField("code", code).Error("好友推荐")
|
||||
return nil
|
||||
}
|
||||
robot.Store("friend.randlist", rsp)
|
||||
return rsp
|
||||
}
|
||||
|
||||
// 好友申请
|
||||
func friendApply(robot lib.IRobot, list *pb.FriendRandlistResp) {
|
||||
req := &pb.FriendApplyReq{}
|
||||
rsp := &pb.FriendApplyResp{}
|
||||
|
||||
do := func() bool {
|
||||
if list != nil && len(list.List) > 0 {
|
||||
// 随件选择一个玩家
|
||||
randInt := randomdata.Number(0, len(list.List))
|
||||
req.FriendId = list.List[randInt].UserId
|
||||
|
||||
if code := robot.SendMsg(Friend_Maintype, "apply", req, rsp); code != pb.ErrorCode_Success {
|
||||
logrus.WithField("code", code).Error("好友申请")
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
Retry(3, do)
|
||||
}
|
||||
|
||||
// 好友申请同意
|
||||
func friendAgree(robot lib.IRobot) {
|
||||
applyListReq := &pb.FriendApplyListReq{}
|
||||
applyListRsp := &pb.FriendApplyListResp{}
|
||||
|
||||
if code := robot.SendMsg(Friend_Maintype, "applylist", applyListReq, applyListRsp); code != pb.ErrorCode_Success {
|
||||
logrus.WithField("code", code).Error("好友申请列表")
|
||||
return
|
||||
}
|
||||
|
||||
//同意
|
||||
req := &pb.FriendAgreeReq{}
|
||||
rsp := &pb.FriendAgreeResp{}
|
||||
if applyListRsp != nil && len(applyListRsp.List) > 0 {
|
||||
req.FriendIds = append(req.FriendIds, applyListRsp.List[0].UserId)
|
||||
if code := robot.SendMsg(Friend_Maintype, "agree", req, rsp); code != pb.ErrorCode_Success {
|
||||
logrus.WithField("code", code).Error("好友申请同意")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
58
lib/robot.go
58
lib/robot.go
@ -218,29 +218,43 @@ func (m *Robot) Stop() bool {
|
||||
}
|
||||
|
||||
func (m *Robot) syncCall() {
|
||||
for {
|
||||
scene, err := m.sceneQueue.Pop()
|
||||
if err != nil {
|
||||
logrus.WithField("err", err).Debug("所有场景执行结束")
|
||||
m.prepareToStop()
|
||||
return
|
||||
}
|
||||
m.scene = scene
|
||||
info := m.scene.Info()
|
||||
go func() {
|
||||
defer func() {
|
||||
if p := recover(); p != nil {
|
||||
err, ok := interface{}(p).(error)
|
||||
var errMsg string
|
||||
if ok {
|
||||
errMsg = fmt.Sprintf("调用时Panic! (error: %s)", err)
|
||||
} else {
|
||||
errMsg = fmt.Sprintf("调用时Panic! (clue: %#v)", p)
|
||||
}
|
||||
logrus.Error(errMsg)
|
||||
}
|
||||
}()
|
||||
for {
|
||||
scene, err := m.sceneQueue.Pop()
|
||||
if err != nil {
|
||||
logrus.WithField("err", err).Debug("所有场景执行结束")
|
||||
m.prepareToStop()
|
||||
return
|
||||
}
|
||||
m.scene = scene
|
||||
info := m.scene.Info()
|
||||
|
||||
start := time.Now()
|
||||
//这里执行会花很长时间
|
||||
if err := scene.Run(m); err != nil {
|
||||
logrus.WithField("err", err).Error("执行业务时发生错误")
|
||||
continue
|
||||
}
|
||||
elapsedTime := time.Since(start)
|
||||
start := time.Now()
|
||||
//这里执行会花很长时间
|
||||
if err := scene.Run(m); err != nil {
|
||||
logrus.WithField("err", err).Error("执行业务时发生错误")
|
||||
continue
|
||||
}
|
||||
elapsedTime := time.Since(start)
|
||||
|
||||
m.elipseTotal += elapsedTime
|
||||
logrus.WithField("t", elapsedTime.String()).Debug("场景【" + info.Name + "】执行完毕耗时统计")
|
||||
//显示场景结果
|
||||
m.processResult()
|
||||
}
|
||||
m.elipseTotal += elapsedTime
|
||||
logrus.WithField("t", elapsedTime.String()).Debug("场景【" + info.Name + "】执行完毕耗时统计")
|
||||
//显示场景结果
|
||||
m.processResult()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (m *Robot) prepareToStop() {
|
||||
@ -267,8 +281,6 @@ func (m *Robot) SendResult(result *CallResult) bool {
|
||||
|
||||
func (m *Robot) processResult() {
|
||||
go func() {
|
||||
defer m.lock.Unlock()
|
||||
m.lock.Lock()
|
||||
for r := range m.resultCh {
|
||||
head := fmt.Sprintf("%s.%s", r.MainType, r.SubType)
|
||||
if routes, ok := m.ReportMap[r.Num]; ok {
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"legu.airobot/busi/friend"
|
||||
"legu.airobot/lib"
|
||||
"legu.airobot/storage"
|
||||
@ -157,6 +158,5 @@ func divide(a int, b int) int {
|
||||
|
||||
func TestDiv(t *testing.T) {
|
||||
|
||||
res := lib.FormatFloatCommon(float64(3 / 10))
|
||||
fmt.Println(res)
|
||||
fmt.Println(randomdata.Number(0, 5))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user