feat: add protobuf message compression and decompression for Redis; refactor product fetching logic
This commit is contained in:
@@ -4,15 +4,16 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/samber/lo"
|
||||
pb "sipro-mps/api/generated/v1/wb/products"
|
||||
"sipro-mps/internal/marketplace"
|
||||
"sipro-mps/internal/redis"
|
||||
"sipro-mps/internal/tasks/types"
|
||||
"sipro-mps/internal/wb/products"
|
||||
"sipro-mps/internal/wb/products/mapping/generated"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
type FetchProductsProcessor struct {
|
||||
@@ -22,7 +23,7 @@ type FetchProductsProcessor struct {
|
||||
func (p *FetchProductsProcessor) ProcessTask(ctx context.Context, task *asynq.Task) error {
|
||||
var payload types.FetchProductsTask
|
||||
if err := json.Unmarshal(task.Payload(), &payload); err != nil {
|
||||
return asynq.SkipRetry
|
||||
return asynq.RevokeTask
|
||||
}
|
||||
marketplaceRepo := marketplace.NewDBRepository(p.Dbpool)
|
||||
repo := products.NewAPIRepository(marketplaceRepo)
|
||||
@@ -38,7 +39,7 @@ func (p *FetchProductsProcessor) ProcessTask(ctx context.Context, task *asynq.Ta
|
||||
_, cancel, err := locker.TryWithContext(ctx, fmt.Sprintf("wb:products:marketplace:%s:lock", sellerId))
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to acquire lock for marketplace %s: %v\n", sellerId, err)
|
||||
return asynq.SkipRetry
|
||||
return asynq.RevokeTask
|
||||
|
||||
}
|
||||
fmt.Println("Working on marketplace", payload.MarketplaceId, "with seller ID", sellerId)
|
||||
@@ -54,15 +55,11 @@ func (p *FetchProductsProcessor) ProcessTask(ctx context.Context, task *asynq.Ta
|
||||
productsProto := lo.Map(productsRaw, func(item products.WbProduct, _ int) *pb.Product {
|
||||
return converter.ToProto(&item)
|
||||
})
|
||||
redisClient := *redis.Client
|
||||
productsJson, err := json.Marshal(productsProto)
|
||||
err = redis.WriteProtoMessage(ctx, redisKey, &pb.GetProductsResponse{Products: productsProto})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal products: %w", err)
|
||||
fmt.Printf(
|
||||
"Failed to write products to Redis for marketplace %s: %v\n")
|
||||
return asynq.RevokeTask
|
||||
}
|
||||
err = redisClient.Do(ctx, redisClient.B().Set().Key(redisKey).Value(string(productsJson)).Build()).Error()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user