- GetAndStoreCitiesShops添加isAsync参数

This commit is contained in:
gazebo
2019-07-08 15:34:58 +08:00
parent faa43fef5a
commit 6c4ca2fd17
2 changed files with 12 additions and 5 deletions

View File

@@ -22,7 +22,7 @@ const (
func GetCityShops(ctx *jxcontext.Context, parentTask tasksch.ITask, vendorIDs []int, cityCode, radius, gridWith int) (pageStoreList []*model.PageShop, err error) { func GetCityShops(ctx *jxcontext.Context, parentTask tasksch.ITask, vendorIDs []int, cityCode, radius, gridWith int) (pageStoreList []*model.PageShop, err error) {
coordList := ditu.GetCityCoordinateList(cityCode, radius, gridWith) coordList := ditu.GetCityCoordinateList(cityCode, radius, gridWith)
if len(coordList) > 0 { if len(coordList) > 0 {
task := tasksch.NewParallelTask("GetCityShops", nil, ctx, task := tasksch.NewParallelTask("GetCityShops", tasksch.NewParallelConfig().SetIsContinueWhenError(true), ctx,
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
vendorID := batchItemList[0].(int) vendorID := batchItemList[0].(int)
storeList, err2 := getStoreListByCoordinates(ctx, task, vendorID, utils.Int2Str(cityCode), coordList) storeList, err2 := getStoreListByCoordinates(ctx, task, vendorID, utils.Int2Str(cityCode), coordList)
@@ -100,7 +100,7 @@ func getStoreListByCoordinates(ctx *jxcontext.Context, parentTask tasksch.ITask,
return storeList, err return storeList, err
} }
func GetAndStoreCitiesShops(ctx *jxcontext.Context, vendorIDs []int, cityCodeList []int, radius, gridWith int) (hint string, err error) { func GetAndStoreCitiesShops(ctx *jxcontext.Context, vendorIDs []int, cityCodeList []int, radius, gridWith int, isAsync bool) (hint string, err error) {
db := dao.GetDB() db := dao.GetDB()
if len(cityCodeList) == 0 { if len(cityCodeList) == 0 {
placeList, err2 := dao.GetPlacesByCond(db, dao.EnableCondAll) placeList, err2 := dao.GetPlacesByCond(db, dao.EnableCondAll)
@@ -131,8 +131,8 @@ func GetAndStoreCitiesShops(ctx *jxcontext.Context, vendorIDs []int, cityCodeLis
dao.Begin(db) dao.Begin(db)
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
panic(r)
dao.Rollback(db) dao.Rollback(db)
panic(r)
} }
}() }()
for _, v := range shopList { for _, v := range shopList {
@@ -145,6 +145,7 @@ func GetAndStoreCitiesShops(ctx *jxcontext.Context, vendorIDs []int, cityCodeLis
if err != nil { if err != nil {
dao.Rollback(db) dao.Rollback(db)
} else { } else {
hint = utils.Int2Str(len(shopList))
dao.Commit(db) dao.Commit(db)
} }
} }
@@ -152,7 +153,12 @@ func GetAndStoreCitiesShops(ctx *jxcontext.Context, vendorIDs []int, cityCodeLis
return nil, nil // 强制继续 return nil, nil // 强制继续
}, len(cityCodeList)) }, len(cityCodeList))
tasksch.ManageTask(task).Run() tasksch.ManageTask(task).Run()
return task.GetID(), err if !isAsync {
_, err = task.GetResult(0)
} else {
hint = task.GetID()
}
return hint, err
} }
func RefreshPageStore(ctx *jxcontext.Context) (err error) { func RefreshPageStore(ctx *jxcontext.Context) (err error) {

View File

@@ -18,6 +18,7 @@ type NetSpiderController struct {
// @Param cityCodes formData string false "城市列表,缺省为后台启用了的城市" // @Param cityCodes formData string false "城市列表,缺省为后台启用了的城市"
// @Param radius formData int false "区域半径市或街道的半径会自动增加或缩小单位为米缺省8000米" // @Param radius formData int false "区域半径市或街道的半径会自动增加或缩小单位为米缺省8000米"
// @Param gridWith formData int false "网格大小单位为米缺省为2000米" // @Param gridWith formData int false "网格大小单位为米缺省为2000米"
// @Param isAsync formData bool false "是否异步"
// @Success 200 {object} controllers.CallResult // @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult // @Failure 200 {object} controllers.CallResult
// @router /GetAndStoreCitiesShops [post] // @router /GetAndStoreCitiesShops [post]
@@ -26,7 +27,7 @@ func (c *NetSpiderController) GetAndStoreCitiesShops() {
var vendorIDs, cityCodes []int var vendorIDs, cityCodes []int
err = jxutils.Strings2Objs(params.VendorIDs, &vendorIDs, params.CityCodes, &cityCodes) err = jxutils.Strings2Objs(params.VendorIDs, &vendorIDs, params.CityCodes, &cityCodes)
if err == nil { if err == nil {
retVal, err = netspider.GetAndStoreCitiesShops(params.Ctx, vendorIDs, cityCodes, params.Radius, params.GridWith) retVal, err = netspider.GetAndStoreCitiesShops(params.Ctx, vendorIDs, cityCodes, params.Radius, params.GridWith, params.IsAsync)
} }
return retVal, "", err return retVal, "", err
}) })