This commit is contained in:
matst80
2025-10-13 15:39:41 +02:00
parent 6094da99f3
commit 9fc3871e84
26 changed files with 927 additions and 848 deletions

View File

@@ -5,12 +5,14 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"reflect"
"sync"
"time"
"git.tornberg.me/go-cart-actor/pkg/actor"
messages "git.tornberg.me/go-cart-actor/pkg/messages"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -205,7 +207,7 @@ func AppendCartEvent(id CartId, mutation interface{}) error {
// ReplayCartEvents replays an existing cart's event log into the provided grain.
// It applies mutation payloads in order, skipping unknown types.
func ReplayCartEvents(grain *CartGrain, id CartId) error {
func ReplayCartEvents(grain *CartGrain, id CartId, registry actor.MutationRegistry) error {
start := time.Now()
path := EventLogPath(id)
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
@@ -237,18 +239,19 @@ func ReplayCartEvents(grain *CartGrain, id CartId) error {
eventReplayFailuresTotal.Inc()
continue // skip malformed line
}
factory, ok := eventTypeFactories[raw.Type]
instance, ok := registry.Create(raw.Type)
if !ok {
eventUnknownTypesTotal.Inc()
continue // skip unknown mutation type
log.Printf("loading failed for unknown mutation type: %s", raw.Type)
eventReplayFailuresTotal.Inc()
continue // skip unknown type
}
instance := factory()
if err := json.Unmarshal(raw.Payload, instance); err != nil {
eventMutationErrorsTotal.Inc()
continue
}
// Apply mutation directly using internal registration (bypass AppendCartEvent recursion).
if _, applyErr := ApplyRegistered(grain, instance); applyErr != nil {
if applyErr := registry.Apply(grain, instance); applyErr != nil {
eventMutationErrorsTotal.Inc()
continue
} else {