send all on results on amqp
All checks were successful
Build and Publish / Metadata (push) Successful in 9s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 1m20s
Build and Publish / BuildAndDeployArm64 (push) Successful in 4m2s

This commit is contained in:
matst80
2025-10-15 07:57:45 +02:00
parent 47adb12112
commit 8c2bcf5e75
5 changed files with 102 additions and 36 deletions

View File

@@ -10,6 +10,8 @@ import (
"time"
"github.com/gogo/protobuf/proto"
"github.com/matst80/slask-finder/pkg/messaging"
amqp "github.com/rabbitmq/amqp091-go"
)
type QueueEvent struct {
@@ -26,7 +28,47 @@ type DiskStorage[V any] struct {
type LogStorage[V any] interface {
LoadEvents(id uint64, grain Grain[V]) error
AppendEvent(id uint64, msg ...proto.Message) error
AppendMutations(id uint64, msg ...proto.Message) error
}
type LogListener interface {
AppendMutations(id uint64, msg ...ApplyResult)
}
type AmqpListener struct {
conn *amqp.Connection
}
func NewAmqpListener(conn *amqp.Connection) *AmqpListener {
return &AmqpListener{
conn: conn,
}
}
func (l *AmqpListener) DefineTopics() {
ch, err := l.conn.Channel()
if err != nil {
log.Fatalf("Failed to open a channel: %v", err)
}
defer ch.Close()
if err := messaging.DefineTopic(ch, "cart", "mutation"); err != nil {
log.Fatalf("Failed to declare topic mutation: %v", err)
}
}
type CartEvent struct {
Id uint64 `json:"id"`
Mutations []ApplyResult `json:"mutations"`
}
func (l *AmqpListener) AppendMutations(id uint64, msg ...ApplyResult) {
err := messaging.SendChange(l.conn, "cart", "mutation", &CartEvent{
Id: id,
Mutations: msg,
})
if err != nil {
log.Printf("Failed to send mutation event: %v", err)
}
}
func NewDiskStorage[V any](path string, registry MutationRegistry) *DiskStorage[V] {
@@ -108,7 +150,7 @@ func (s *DiskStorage[V]) Close() {
close(s.done)
}
func (s *DiskStorage[V]) AppendEvent(id uint64, msg ...proto.Message) error {
func (s *DiskStorage[V]) AppendMutations(id uint64, msg ...proto.Message) error {
if s.queue != nil {
queue := make([]QueueEvent, 0)
data, found := s.queue.Load(id)