add listener and remove memory inventory service

This commit is contained in:
2025-11-10 19:47:15 +01:00
parent ce0bac477a
commit 44d7c1faad
3 changed files with 100 additions and 47 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"context"
"log"
"time"
"git.tornberg.me/go-cart-actor/pkg/inventory"
"github.com/redis/go-redis/v9"
@@ -24,6 +25,18 @@ func main() {
log.Fatalf("Unable to connect to inventory redis: %v", err)
return
}
// Start inventory change listener
listener := inventory.NewInventoryChangeListener(rdb, ctx, func(changes []inventory.InventoryChange) {
for _, change := range changes {
log.Printf("Inventory change detected: SKU %s at location %s now has %d", change.SKU, change.StockLocationID, change.Value)
}
})
log.Println("Starting inventory change listener...")
go listener.Start()
listener.WaitReady()
log.Println("Listener is ready, proceeding with inventory operations...")
rdb.Pipelined(ctx, func(p redis.Pipeliner) error {
s.UpdateInventory(p, "1", "1", 10)
s.UpdateInventory(p, "2", "2", 20)
@@ -51,4 +64,6 @@ func main() {
}
log.Printf("Inventory after reservation: %v", v)
// Wait a bit for listener to process messages
time.Sleep(2 * time.Second)
}