Refactor marketplace product fetching and caching logic; update environment configuration for Redis and PostgreSQL
This commit is contained in:
66
internal/tasks/ozon/ozon.go
Normal file
66
internal/tasks/ozon/ozon.go
Normal file
@@ -0,0 +1,66 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user