threads
This commit is contained in:
@ -13,6 +13,7 @@ import (
|
||||
"gitea.home.4it.me/dilap54/platiparser/plati"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"github.com/urfave/cli/v2"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -32,22 +33,34 @@ var platiCommand = &cli.Command{
|
||||
|
||||
// printNames(categories)
|
||||
|
||||
wg, _ := errgroup.WithContext(ctx)
|
||||
wg.SetLimit(30)
|
||||
|
||||
for i, c := range categories {
|
||||
log.Printf("fetching goods [%d/%d] for %s\n", i, len(categories), c.FlatName)
|
||||
goods, err := platiCli.GetBlockGoodsCategory(ctx, c.ID, c.ParentID, "cntSellDESC", 1, 100, "RUR", "ru-RU")
|
||||
if err != nil {
|
||||
return fmt.Errorf("getblockgoodscategory: %w", err)
|
||||
}
|
||||
if len(goods) == 0 {
|
||||
continue
|
||||
}
|
||||
gormGoods := convertGoodsToGorm(c, goods)
|
||||
if err := db.Create(gormGoods).Error; err != nil {
|
||||
return fmt.Errorf("db Create: %w", err)
|
||||
}
|
||||
json.NewEncoder(os.Stdout).Encode(goods)
|
||||
l := i
|
||||
cat := c
|
||||
wg.Go(func() error {
|
||||
log.Printf("fetching goods [%d/%d] for %s\n", l, len(categories), c.FlatName)
|
||||
goods, err := platiCli.GetBlockGoodsCategory(ctx, cat.ID, cat.ParentID, "cntSellDESC", 1, 100, "RUR", "ru-RU")
|
||||
if err != nil {
|
||||
return fmt.Errorf("getblockgoodscategory: %w", err)
|
||||
}
|
||||
if len(goods) == 0 {
|
||||
return nil
|
||||
}
|
||||
gormGoods := convertGoodsToGorm(cat, goods)
|
||||
if err := db.Create(gormGoods).Error; err != nil {
|
||||
return fmt.Errorf("db Create: %w", err)
|
||||
}
|
||||
json.NewEncoder(os.Stdout).Encode(goods)
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
return wg.Wait()
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
Reference in New Issue
Block a user