- RefreshWeixinToken and RefreshElmToken firs time run must completed before main workflow going.

This commit is contained in:
gazebo
2018-07-21 13:30:43 +08:00
parent 1958f24705
commit 260453be69
3 changed files with 69 additions and 51 deletions

View File

@@ -219,7 +219,7 @@ func (s *DefScheduler) cancelOtherWaybills(savedOrderInfo *WatchOrderInfo, bill
globals.SugarLogger.Debugf("cancelOtherWaybills, order:%v, bill:%v", savedOrderInfo.order, bill)
for _, v := range savedOrderInfo.waybills {
if bill == nil || (v.WaybillVendorID != bill.OrderVendorID && !(v.WaybillVendorID == bill.WaybillVendorID && v.VendorWaybillID == bill.VendorWaybillID)) {
globals.SugarLogger.Debugf("cancelOtherWaybills, cancel bill:%v", bill)
globals.SugarLogger.Debugf("cancelOtherWaybills, cancel bill:%v", v)
_ = s.GetDeliveryPlatformFromVendorID(v.WaybillVendorID).CancelWaybill(v)
}
}

View File

@@ -1,6 +1,7 @@
package tasks
import (
"fmt"
"time"
"git.rosy.net.cn/jx-callback/globals/api"
@@ -29,24 +30,23 @@ type ElmTokenForCompatible struct {
Success bool `json:"success"`
}
func RefreshConfig(configKey string, expiresTime time.Duration, configGetter func() string, configSetter func(value string)) {
go func() {
func RefreshConfig(configKey string, expiresTime time.Duration, configGetter func() string, configSetter func(value string)) error {
sleepGap := expiresTime / 10
needRefreshGap := expiresTime * 8 / 10
if sleepGap > maxRefreshGap {
sleepGap = maxRefreshGap
}
for {
refreshFunc := func() error {
curConfig := &models.Config{
Thirdparty: configKey,
}
db := orm.NewOrm()
// 0: don't refresh, 1 update, 2 insert
handleType := 0
if err := db.Read(curConfig, "Thirdparty"); err != nil {
if err != orm.ErrNoRows {
globals.SugarLogger.Errorf("db error:%v, curConfig:%v", err, curConfig)
return err
} else {
curConfig.Token = configGetter()
handleType = 2
@@ -54,11 +54,12 @@ func RefreshConfig(configKey string, expiresTime time.Duration, configGetter fun
} else {
latestTimeStr := utils.Time2Str(time.Now().Add(-needRefreshGap))
if curConfig.Date <= latestTimeStr {
curConfig.Token = configGetter()
if curConfig.Token = configGetter(); curConfig.Token == "" {
return fmt.Errorf("can not get token for %s", configKey)
}
handleType = 1
}
}
if handleType != 0 {
globals.SugarLogger.Debugf("refresh %s, value:%s", configKey, curConfig.Token)
curConfig.Date = utils.GetCurTimeStr()
@@ -72,17 +73,27 @@ func RefreshConfig(configKey string, expiresTime time.Duration, configGetter fun
}
if err != nil || num == 0 {
globals.SugarLogger.Errorf("db error:%v, num:%d, curConfig:%v", err, num, curConfig)
return err
} else {
configSetter(curConfig.Token)
}
}
return nil
}
err := refreshFunc() // 这样写的目的是强制第一次调用时要刷新一次
if err == nil {
go func() {
for {
time.Sleep(sleepGap)
refreshFunc()
}
}()
}
return err
}
func RefreshWeixinToken() {
RefreshConfig("wechat", weixinTokenExpires, func() string {
func RefreshWeixinToken() error {
return RefreshConfig("wechat", weixinTokenExpires, func() string {
if tokenInfo, err := api.WeixinAPI.RefreshToken(); err == nil {
return tokenInfo.AccessToken
}
@@ -93,8 +104,8 @@ func RefreshWeixinToken() {
})
}
func RefreshElmToken() {
RefreshConfig("eleme", elmTokenExpires, func() string {
func RefreshElmToken() error {
return RefreshConfig("eleme", elmTokenExpires, func() string {
if tokenInfo, err := api.ElmAPI.RefreshTokenIndividual(); err == nil {
tokenInfo2 := &ElmTokenForCompatible{
Error: "",

11
main.go
View File

@@ -2,6 +2,7 @@ package main
import (
bzcon "git.rosy.net.cn/jx-callback/business/controller"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/legacy/jd/controller"
"git.rosy.net.cn/jx-callback/legacy/tasks"
_ "git.rosy.net.cn/jx-callback/routers"
@@ -10,9 +11,15 @@ import (
func main() {
if beego.BConfig.RunMode == "prod" {
if err := tasks.RefreshWeixinToken(); err != nil {
globals.SugarLogger.Errorf("RefreshWeixinToken failed with error:%s", err)
return
}
if err := tasks.RefreshElmToken(); err != nil {
globals.SugarLogger.Errorf("RefreshElmToken failed with error:%s", err)
return
}
controller.InitOrder()
tasks.RefreshWeixinToken()
tasks.RefreshElmToken()
}
bzcon.OrderManager.LoadPendingOrders()
bzcon.WaybillManager.LoadPendingWaybills()