From cb68360f2f6dc4b5fcf373aae66338474fa7503a Mon Sep 17 00:00:00 2001 From: nite07 Date: Sat, 23 Nov 2024 19:44:58 +0800 Subject: [PATCH] fix task.Crawl --- db/game.go | 3 +++ task/crawl.go | 26 +++++++++++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/db/game.go b/db/game.go index c1b0357..54fa22b 100644 --- a/db/game.go +++ b/db/game.go @@ -231,6 +231,9 @@ func GetGameItemByID(id primitive.ObjectID) (*model.GameItem, error) { } func GetGameItemsByIDs(ids []primitive.ObjectID) ([]*model.GameItem, error) { + if len(ids) == 0 { + return nil, nil + } var items []*model.GameItem ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() diff --git a/task/crawl.go b/task/crawl.go index 1976220..889f4a0 100644 --- a/task/crawl.go +++ b/task/crawl.go @@ -51,21 +51,25 @@ func Crawl(logger *zap.Logger) { for _, game := range games { ids = append(ids, game.ID) } - items, err := db.GetGameItemsByIDs(ids) - if err != nil { - logger.Error("Failed to get game items", zap.Error(err)) - return - } + var infos []*model.GameInfo - for _, game := range items { - info, err := db.GetGameInfoByGameItemID(game.ID) + if len(ids) != 0 { + items, err := db.GetGameItemsByIDs(ids) if err != nil { - logger.Error("Failed to get game info", zap.Error(err)) - continue + logger.Error("Failed to get game items", zap.Error(err)) + return + } + for _, game := range items { + info, err := db.GetGameInfoByGameItemID(game.ID) + if err != nil { + logger.Error("Failed to get game info", zap.Error(err)) + continue + } + info.Games = append(info.Games, game) + infos = append(infos, info) } - info.Games = append(info.Games, game) - infos = append(infos, info) } + for _, u := range config.Config.Webhooks.CrawlTask { _, err := url.Parse(u) if err != nil {