From dec14126a71a07431d020949e1bdeb505ded8965 Mon Sep 17 00:00:00 2001 From: gazebo Date: Fri, 15 Mar 2019 10:26:30 +0800 Subject: [PATCH] - replace all goroutine and AfterFunc with recover version --- business/jxcallback/orderman/oder_comment.go | 2 +- business/jxcallback/scheduler/defsch/defsch.go | 4 ++-- business/jxstore/misc/misc.go | 2 +- business/jxstore/promotion/jd_promotion.go | 16 ++++++++-------- business/jxutils/jxutils.go | 2 +- business/partner/purchase/ebai/order.go | 4 ++-- business/partner/purchase/ebai/order_comment.go | 4 ++-- business/partner/purchase/ebai/store_sku.go | 2 +- .../partner/purchase/elm/order_legacy_urge.go | 6 +++--- .../partner/purchase/jd/order_legacy_comment.go | 2 +- business/partner/purchase/mtwm/order.go | 4 ++-- business/partner/purchase/mtwm/waybill.go | 4 ++-- business/partner/purchase/weimob/wsc/order.go | 4 ++-- main.go | 3 ++- 14 files changed, 30 insertions(+), 29 deletions(-) diff --git a/business/jxcallback/orderman/oder_comment.go b/business/jxcallback/orderman/oder_comment.go index dca05378a..e62d090db 100644 --- a/business/jxcallback/orderman/oder_comment.go +++ b/business/jxcallback/orderman/oder_comment.go @@ -164,7 +164,7 @@ func (c *OrderManager) replyOrderComment(orderComment *model.OrderComment) (err delaySeconds := config.delayGapBegin + rand.Intn(config.delayGapEnd-config.delayGapBegin) content := config.comments[rand.Intn(len(config.comments))] globals.SugarLogger.Debugf("replyOrderComment orderID:%s, delaySeconds:%d, content:%s", orderComment.VendorOrderID, delaySeconds, content) - time.AfterFunc(time.Duration(delaySeconds)*time.Second, func() { + utils.AfterFuncWithRecover(time.Duration(delaySeconds)*time.Second, func() { if globals.ReallySendWeixinMsg { if handler := partner.GetPurchasePlatformFromVendorID(orderComment.VendorID); handler != nil { err = handler.ReplyOrderComment(jxcontext.AdminCtx, orderComment, content) diff --git a/business/jxcallback/scheduler/defsch/defsch.go b/business/jxcallback/scheduler/defsch/defsch.go index c563ca295..b74286c52 100644 --- a/business/jxcallback/scheduler/defsch/defsch.go +++ b/business/jxcallback/scheduler/defsch/defsch.go @@ -583,7 +583,7 @@ func (s *DefScheduler) swtich2SelfDeliverWithRetry(savedOrderInfo *WatchOrderInf if err := s.Swtich2SelfDeliver(order, ""); err != nil && err != scheduler.ErrOrderStatusAlreadySatisfyCurOperation { globals.SugarLogger.Infof("swtich2SelfDeliverWithRetry failed, bill:%v, err:%v", bill, err) if retryCount > 0 { - time.AfterFunc(duration, func() { + utils.AfterFuncWithRecover(duration, func() { jxutils.CallMsgHandlerAsync(func() { s.swtich2SelfDeliverWithRetry(savedOrderInfo, bill, retryCount-1, duration) }, order.VendorOrderID) @@ -724,7 +724,7 @@ func (s *DefScheduler) resetTimer(savedOrderInfo *WatchOrderInfo, bill *model.Wa } savedOrderInfo.timerStatusType = statusType savedOrderInfo.timerStatus = status - savedOrderInfo.timer = time.AfterFunc(timeout, func() { + savedOrderInfo.timer = utils.AfterFuncWithRecover(timeout, func() { jxutils.CallMsgHandlerAsync(func() { globals.SugarLogger.Debugf("fire timer:%s, orderID:%s", timerName, order.VendorOrderID) config.TimeoutAction(savedOrderInfo) diff --git a/business/jxstore/misc/misc.go b/business/jxstore/misc/misc.go index 326d04c9a..8c5cc2a2a 100644 --- a/business/jxstore/misc/misc.go +++ b/business/jxstore/misc/misc.go @@ -14,7 +14,7 @@ func Init() { func RefreshEbaiRealMobile() { ebai.CurPurchaseHandler.RefreshRealMobile(jxcontext.AdminCtx, time.Now().Add(-6*time.Hour), utils.DefaultTimeValue, true, true) - time.AfterFunc(1*time.Hour, func() { + utils.AfterFuncWithRecover(1*time.Hour, func() { RefreshEbaiRealMobile() }) } diff --git a/business/jxstore/promotion/jd_promotion.go b/business/jxstore/promotion/jd_promotion.go index d830602be..1043fbc7e 100644 --- a/business/jxstore/promotion/jd_promotion.go +++ b/business/jxstore/promotion/jd_promotion.go @@ -205,7 +205,7 @@ func scheduleDailyRoutine(isFirst bool) { UpdateJdPromotionStatus() } // globals.SugarLogger.Debug(duration) - time.AfterFunc(duration, func() { + utils.AfterFuncWithRecover(duration, func() { UpdateJdPromotionStatus() scheduleDailyRoutine(false) }) @@ -213,12 +213,12 @@ func scheduleDailyRoutine(isFirst bool) { func scheduleRoutine(isFirst bool) { if isFirst { - go func() { + utils.CallFuncAsync(func() { RefreshJdLockStoreSku() RefreshJdStoreSkuStock(0, nil) - }() + }) } - time.AfterFunc(stockRefreshGap, func() { + utils.AfterFuncWithRecover(stockRefreshGap, func() { RefreshJdLockStoreSku() RefreshJdStoreSkuStock(0, nil) scheduleRoutine(false) @@ -887,7 +887,7 @@ func OnStoreStockMsg(msg *jdapi.CallbackStoreStockMsg) (retVal *jdapi.CallbackRe sku := &model.Sku{} sku.JdID = msg.SkuId if err = dao.GetEntity(db, sku, model.FieldJdID); err == nil { - go func() { + utils.CallFuncAsync(func() { if msg.Vendibility == 1 { vendibility := &jdapi.StockVendibility{ OutSkuId: utils.Int2Str(sku.ID), @@ -906,7 +906,7 @@ func OnStoreStockMsg(msg *jdapi.CallbackStoreStockMsg) (retVal *jdapi.CallbackRe stock, }, userName) } - }() + }) } } return jdapi.Err2CallbackResponse(err, "") @@ -917,7 +917,7 @@ func OnNewPromotionMsg(msg *jdapi.CallbackOrderMsg) (retVal *jdapi.CallbackRespo } func createLocalPromotionFromRemote(promotionInfoId int64) (retVal *jdapi.CallbackResponse) { - go func() { + utils.CallFuncAsync(func() { result, err := api.JdAPI.QueryPromotionInfo(promotionInfoId) if err == nil { db := dao.GetDB() @@ -1005,7 +1005,7 @@ func createLocalPromotionFromRemote(promotionInfoId int64) (retVal *jdapi.Callba } } } - }() + }) return jdapi.Err2CallbackResponse(nil, "") } diff --git a/business/jxutils/jxutils.go b/business/jxutils/jxutils.go index 47c0a74fd..d7304b0f3 100644 --- a/business/jxutils/jxutils.go +++ b/business/jxutils/jxutils.go @@ -40,7 +40,7 @@ func init() { func (m *SyncMapWithTimeout) StoreWithTimeout(key, value interface{}, timeout time.Duration) { m.Map.Store(key, value) - m.timers.Store(key, time.AfterFunc(timeout, func() { + m.timers.Store(key, utils.AfterFuncWithRecover(timeout, func() { m.Delete(key) })) } diff --git a/business/partner/purchase/ebai/order.go b/business/partner/purchase/ebai/order.go index aa57dd101..f84e5b2b3 100644 --- a/business/partner/purchase/ebai/order.go +++ b/business/partner/purchase/ebai/order.go @@ -291,9 +291,9 @@ func (c *PurchaseHandler) postFakeFinishedPickupMsg(vendorOrderID string) { "order_id": vendorOrderID, }, } - go func() { + utils.CallFuncAsync(func() { OnCallbackMsg(msg) - }() + }) } func getTimeFromTimestampStr(sendTime string) time.Time { diff --git a/business/partner/purchase/ebai/order_comment.go b/business/partner/purchase/ebai/order_comment.go index 30d7460fd..94638d652 100644 --- a/business/partner/purchase/ebai/order_comment.go +++ b/business/partner/purchase/ebai/order_comment.go @@ -21,14 +21,14 @@ const ( ) func (c *PurchaseHandler) StartRefreshComment() { - time.AfterFunc(5*time.Second, func() { + utils.AfterFuncWithRecover(5*time.Second, func() { c.refreshCommentOnce() }) } func (c *PurchaseHandler) refreshCommentOnce() { c.RefreshComment(time.Now().Add(-RefreshCommentTime), time.Now()) - time.AfterFunc(RefreshCommentTimeInterval, func() { + utils.AfterFuncWithRecover(RefreshCommentTimeInterval, func() { c.refreshCommentOnce() }) } diff --git a/business/partner/purchase/ebai/store_sku.go b/business/partner/purchase/ebai/store_sku.go index 7c4d023bf..9b2dcb9ab 100644 --- a/business/partner/purchase/ebai/store_sku.go +++ b/business/partner/purchase/ebai/store_sku.go @@ -250,7 +250,7 @@ func (p *PurchaseHandler) SyncStoreSkus(ctx *jxcontext.Context, parentTask tasks // 饿百支持创建sku时绑定商家分类了 // isCreate = true // // 创建SKU后马上绑定分类,会失败,所以延迟绑定 - // time.AfterFunc(3*time.Second, func() { + // utils.AfterFuncWithRecover(3*time.Second, func() { // if err := api.EbaiAPI.SkuShopCategoryMap(strStoreID, storeSku.EbaiID, utils.Int64ToStr(storeSku.CatEbaiID)); err == nil { // storeSku.EbaiSyncStatus = 0 // dao.UpdateEntity(nil, &storeSku.StoreSkuBind, updateFields...) diff --git a/business/partner/purchase/elm/order_legacy_urge.go b/business/partner/purchase/elm/order_legacy_urge.go index a6ab32f94..abbf4077d 100644 --- a/business/partner/purchase/elm/order_legacy_urge.go +++ b/business/partner/purchase/elm/order_legacy_urge.go @@ -16,7 +16,8 @@ const ( ) func (c *PurchaseHandler) ClientUrgeOrder(orderID string) (err error) { - go func() (err error) { + utils.CallFuncAsync(func() { + var err error globals.SugarLogger.Debugf("ClientUrgeOrder orderID:%s", orderID) order, err2 := partner.CurOrderManager.LoadOrder(orderID, model.VendorIDELM) if err = err2; err == nil { @@ -45,7 +46,6 @@ func (c *PurchaseHandler) ClientUrgeOrder(orderID string) (err error) { if err != nil { globals.SugarLogger.Warnf("ClientUrgeOrder orderID:%s failed with error:%v", orderID, err) } - return err - }() + }) return err } diff --git a/business/partner/purchase/jd/order_legacy_comment.go b/business/partner/purchase/jd/order_legacy_comment.go index 3a4b630c2..34eaaa4a9 100644 --- a/business/partner/purchase/jd/order_legacy_comment.go +++ b/business/partner/purchase/jd/order_legacy_comment.go @@ -156,7 +156,7 @@ func (c *PurchaseHandler) replyOrderComment(intOrderID int64, jdStoreNo string, delaySeconds := config.delayGapBegin + rand.Intn(config.delayGapEnd-config.delayGapBegin) content := config.comments[rand.Intn(len(config.comments))] globals.SugarLogger.Debugf("replyOrderComment orderID:%d, delaySeconds:%d, content:%s", intOrderID, delaySeconds, content) - time.AfterFunc(time.Duration(delaySeconds)*time.Second, func() { + utils.AfterFuncWithRecover(time.Duration(delaySeconds)*time.Second, func() { // err2 := utils.CallFuncLogError(func() error { // if globals.ReallyCallPlatformAPI { // return api.JdAPI.OrgReplyComment(intOrderID, jdStoreNo, content, utils.GetAPIOperator("")) diff --git a/business/partner/purchase/mtwm/order.go b/business/partner/purchase/mtwm/order.go index 821e4ebe9..3c1242a1b 100644 --- a/business/partner/purchase/mtwm/order.go +++ b/business/partner/purchase/mtwm/order.go @@ -189,9 +189,9 @@ func (c *PurchaseHandler) AcceptOrRefuseOrder(order *model.GoodsOrder, isAcceptI msg.Data.Set("utime", msg.Data.Get("timestamp")) msg.Data.Set(mtwmapi.KeyOrderID, order.VendorOrderID) msg.Data.Set("status", mtwmapi.OrderStatusReceived) - go func() { + utils.CallFuncAsync(func() { OnOrderCallbackMsg(msg) - }() + }) } } else { err = api.MtwmAPI.OrderCancel(utils.Str2Int64(order.VendorOrderID)) diff --git a/business/partner/purchase/mtwm/waybill.go b/business/partner/purchase/mtwm/waybill.go index 006a6ae9c..42fd025a5 100644 --- a/business/partner/purchase/mtwm/waybill.go +++ b/business/partner/purchase/mtwm/waybill.go @@ -40,9 +40,9 @@ func (c *PurchaseHandler) onWaybillMsg(msg *mtwmapi.CallbackMsg) (response *mtwm msg.Data.Set("utime", msg.Data.Get("timestamp")) msg.Data.Set(mtwmapi.KeyOrderID, waybill.VendorOrderID) msg.Data.Set("status", mtwmapi.OrderStatusDelivering) - go func() { + utils.CallFuncAsync(func() { OnOrderCallbackMsg(msg) - }() + }) } return mtwmapi.Err2CallbackResponse(err, "") } diff --git a/business/partner/purchase/weimob/wsc/order.go b/business/partner/purchase/weimob/wsc/order.go index 590ae0991..b553f0e31 100644 --- a/business/partner/purchase/weimob/wsc/order.go +++ b/business/partner/purchase/weimob/wsc/order.go @@ -243,9 +243,9 @@ func (p *PurchaseHandler) postFakeMsg(orderNo int64, fakeStatus string) { OrderNo: orderNo, StatusTime: time.Now(), } - go func() { + utils.CallFuncAsync(func() { OnCallbackMsg(msg) - }() + }) } func (p *PurchaseHandler) arrangeSaleStore(order *model.GoodsOrder, cityName, provinceName string) { diff --git a/main.go b/main.go index 28f79d340..036e32585 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "time" "git.rosy.net.cn/baseapi/platformapi/dingdingapi" + "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/jxcallback/orderman" "git.rosy.net.cn/jx-callback/business/jxcallback/scheduler/defsch" // 导入缺省订单调度器 "git.rosy.net.cn/jx-callback/business/jxstore/cms" @@ -105,7 +106,7 @@ func main() { // 延时的原因是等回调准备好 if beego.BConfig.RunMode == "prod" { - time.AfterFunc(2*time.Second, func() { + utils.AfterFuncWithRecover(2*time.Second, func() { api.DingDingAPI.DeleteCallback() if err := api.DingDingAPI.RegisterCallback([]string{dingdingapi.CBTagUserAddOrg, dingdingapi.CBTagUserModifyOrg, dingdingapi.CBTagUserLeaveOrg}, beego.AppConfig.DefaultString("dingdingCallbackToken", ""), beego.AppConfig.DefaultString("dingdingCallbackAESKey", ""), beego.AppConfig.DefaultString("dingdingCallbackURL", "")); err != nil { globals.SugarLogger.Warnf("dingding RegisterCallback failed with error:%v", err)