内部异常捕获

This commit is contained in:
wh_zcy 2022-12-14 19:24:33 +08:00
parent 0c3791c591
commit c3fe1f6427
4 changed files with 114 additions and 30 deletions

View File

@ -10,3 +10,12 @@ func Sleep(min, max time.Duration) {
t := time.Duration(rand.Int63n(ulit)) + min t := time.Duration(rand.Int63n(ulit)) + min
time.Sleep(t) 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)
}
}

View File

@ -1,14 +1,21 @@
package busi package busi
import ( import (
"time"
"github.com/Pallinder/go-randomdata"
"github.com/sirupsen/logrus"
"legu.airobot/lib" "legu.airobot/lib"
"legu.airobot/pb" "legu.airobot/pb"
) )
//好友场景 //好友场景
var _ lib.IScene = (*FriendScene)(nil) var _ lib.IScene = (*FriendScene)(nil)
const (
Friend_Maintype = "friend"
)
type FriendScene struct { type FriendScene struct {
lib.Action lib.Action
} }
@ -21,9 +28,65 @@ func (f *FriendScene) Info() lib.SceneInfo {
} }
func (f *FriendScene) Run(robot lib.IRobot) error { func (f *FriendScene) Run(robot lib.IRobot) error {
req := &pb.FriendRandlistReq{} friendList := friendRandlist(robot)
rsp := &pb.FriendRandlistResp{} Sleep(time.Second, time.Second*3)
robot.SendMsg("", "", req, rsp) friendApply(robot, friendList)
Sleep(time.Second, time.Second*3)
friendAgree(robot)
return nil 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
}
}
}

View File

@ -218,6 +218,19 @@ func (m *Robot) Stop() bool {
} }
func (m *Robot) syncCall() { func (m *Robot) syncCall() {
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 { for {
scene, err := m.sceneQueue.Pop() scene, err := m.sceneQueue.Pop()
if err != nil { if err != nil {
@ -241,6 +254,7 @@ func (m *Robot) syncCall() {
//显示场景结果 //显示场景结果
m.processResult() m.processResult()
} }
}()
} }
func (m *Robot) prepareToStop() { func (m *Robot) prepareToStop() {
@ -267,8 +281,6 @@ func (m *Robot) SendResult(result *CallResult) bool {
func (m *Robot) processResult() { func (m *Robot) processResult() {
go func() { go func() {
defer m.lock.Unlock()
m.lock.Lock()
for r := range m.resultCh { for r := range m.resultCh {
head := fmt.Sprintf("%s.%s", r.MainType, r.SubType) head := fmt.Sprintf("%s.%s", r.MainType, r.SubType)
if routes, ok := m.ReportMap[r.Num]; ok { if routes, ok := m.ReportMap[r.Num]; ok {

View File

@ -5,6 +5,7 @@ import (
"math" "math"
"testing" "testing"
"github.com/Pallinder/go-randomdata"
"legu.airobot/busi/friend" "legu.airobot/busi/friend"
"legu.airobot/lib" "legu.airobot/lib"
"legu.airobot/storage" "legu.airobot/storage"
@ -157,6 +158,5 @@ func divide(a int, b int) int {
func TestDiv(t *testing.T) { func TestDiv(t *testing.T) {
res := lib.FormatFloatCommon(float64(3 / 10)) fmt.Println(randomdata.Number(0, 5))
fmt.Println(res)
} }