67 lines
1.9 KiB
Go
67 lines
1.9 KiB
Go
package ozon
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/hibiken/asynq"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/samber/lo"
|
|
"sipro-mps/internal/marketplace"
|
|
"sipro-mps/internal/ozon/products/mapping/generated"
|
|
"sipro-mps/internal/redis"
|
|
"sipro-mps/internal/tasks/types"
|
|
|
|
"sipro-mps/internal/ozon/products"
|
|
)
|
|
|
|
type FetchProductsProcessor struct {
|
|
Dbpool *pgxpool.Pool
|
|
}
|
|
|
|
func (p *FetchProductsProcessor) ProcessTask(ctx context.Context, task *asynq.Task) error {
|
|
var payload types.FetchProductsTask
|
|
err := payload.Unmarshal(task)
|
|
if err != nil {
|
|
return asynq.SkipRetry
|
|
}
|
|
marketplaceRepo := marketplace.NewDBRepository(p.Dbpool)
|
|
marketplaceById, err := marketplaceRepo.GetMarketplaceByID(ctx, payload.MarketplaceId)
|
|
if err != nil {
|
|
return asynq.SkipRetry
|
|
}
|
|
identifier, err := marketplaceById.GetIdentifier()
|
|
if err != nil {
|
|
return asynq.SkipRetry
|
|
}
|
|
lockKey := fmt.Sprintf("ozon:products:marketplace:%s:lock", identifier)
|
|
locker := *redis.Locker
|
|
_, cancel, err := locker.TryWithContext(ctx, lockKey)
|
|
if err != nil {
|
|
fmt.Printf("Failed to acquire lock for marketplace %s: %v\n", identifier, err)
|
|
return asynq.SkipRetry
|
|
}
|
|
defer cancel()
|
|
ozonRepo := products.NewAPIRepository(marketplaceRepo)
|
|
|
|
productsRaw, err := ozonRepo.GetAllProducts(ctx, payload.MarketplaceId)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to fetch products for marketplace %d: %w", payload.MarketplaceId, err)
|
|
}
|
|
converter := generated.ConverterImpl{}
|
|
productsProto := lo.Map(productsRaw, func(item products.OzonProduct, _ int) *products.PbProduct {
|
|
return converter.ToProto(&item)
|
|
})
|
|
productsJson, err := json.Marshal(productsProto)
|
|
if err != nil {
|
|
return asynq.SkipRetry
|
|
}
|
|
redisClient := *redis.Client
|
|
productsKey := fmt.Sprintf("ozon:products:%s", identifier)
|
|
err = redisClient.Do(ctx, redisClient.B().Set().Key(productsKey).Value(string(productsJson)).Build()).Error()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|