feat(event sourcing): Publish EventStored event after successful SaveEvent
Implement EventStored infrastructure event that notifies subscribers when an event is successfully persisted. This enables observability and triggers downstream workflows (caching, metrics, projections) without coupling to application events. Changes: - Add EventStored type to event.go containing EventID, ActorID, Version, Timestamp - Update InMemoryEventStore with optional EventBus and metrics support via builder methods - Update JetStreamEventStore with optional EventBus and metrics support via builder methods - Publish EventStored to __internal__ namespace after successful SaveEvent - EventStored not published if SaveEvent fails (e.g., version conflict) - EventStored publishing is optional - stores work without EventBus configured - Metrics are recorded for each EventStored publication - Add comprehensive test suite covering all acceptance criteria Meets acceptance criteria: - EventStored published after SaveEvent succeeds - EventStored contains EventID, ActorID, Version, Timestamp - No EventStored published if SaveEvent fails - EventBus receives EventStored in same operation - Metrics increment for each EventStored published Closes #61 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package store
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.flowmade.one/flowmade-one/aether"
|
||||
)
|
||||
@@ -11,6 +12,8 @@ type InMemoryEventStore struct {
|
||||
mu sync.RWMutex
|
||||
events map[string][]*aether.Event // actorID -> events
|
||||
snapshots map[string][]*aether.ActorSnapshot // actorID -> snapshots (sorted by version)
|
||||
eventBus aether.EventBroadcaster // Optional EventBus for publishing EventStored
|
||||
metrics aether.MetricsCollector // Optional metrics collector
|
||||
}
|
||||
|
||||
// NewInMemoryEventStore creates a new in-memory event store
|
||||
@@ -21,9 +24,24 @@ func NewInMemoryEventStore() *InMemoryEventStore {
|
||||
}
|
||||
}
|
||||
|
||||
// WithEventBus sets the EventBus for publishing EventStored events.
|
||||
// This is optional - if not set, EventStored will not be published.
|
||||
func (es *InMemoryEventStore) WithEventBus(bus aether.EventBroadcaster) *InMemoryEventStore {
|
||||
es.eventBus = bus
|
||||
return es
|
||||
}
|
||||
|
||||
// WithMetrics sets the metrics collector for recording EventStored metrics.
|
||||
// This is optional - if not set, metrics will not be recorded.
|
||||
func (es *InMemoryEventStore) WithMetrics(metrics aether.MetricsCollector) *InMemoryEventStore {
|
||||
es.metrics = metrics
|
||||
return es
|
||||
}
|
||||
|
||||
// SaveEvent saves an event to the in-memory store.
|
||||
// Returns VersionConflictError if the event's version is not strictly greater
|
||||
// than the current latest version for the actor.
|
||||
// On success, publishes an EventStored event to the EventBus (if configured).
|
||||
func (es *InMemoryEventStore) SaveEvent(event *aether.Event) error {
|
||||
es.mu.Lock()
|
||||
defer es.mu.Unlock()
|
||||
@@ -51,9 +69,50 @@ func (es *InMemoryEventStore) SaveEvent(event *aether.Event) error {
|
||||
es.events[event.ActorID] = make([]*aether.Event, 0)
|
||||
}
|
||||
es.events[event.ActorID] = append(es.events[event.ActorID], event)
|
||||
|
||||
// Publish EventStored event on success
|
||||
es.publishEventStored(event)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// publishEventStored publishes an EventStored event to the EventBus and records metrics
|
||||
func (es *InMemoryEventStore) publishEventStored(event *aether.Event) {
|
||||
if es.eventBus == nil {
|
||||
return
|
||||
}
|
||||
|
||||
stored := &aether.EventStored{
|
||||
EventID: event.ID,
|
||||
ActorID: event.ActorID,
|
||||
Version: event.Version,
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
|
||||
// Convert EventStored to Event for publishing (internal system event)
|
||||
storedEvent := &aether.Event{
|
||||
ID: "eventstored-" + event.ID,
|
||||
EventType: "EventStored",
|
||||
ActorID: event.ActorID,
|
||||
Version: event.Version,
|
||||
Data: map[string]interface{}{
|
||||
"eventId": stored.EventID,
|
||||
"actorId": stored.ActorID,
|
||||
"version": stored.Version,
|
||||
"timestamp": stored.Timestamp,
|
||||
},
|
||||
Timestamp: stored.Timestamp,
|
||||
}
|
||||
|
||||
// Publish to default namespace (internal events)
|
||||
es.eventBus.Publish("__internal__", storedEvent)
|
||||
|
||||
// Record metrics if collector is configured
|
||||
if es.metrics != nil {
|
||||
es.metrics.RecordPublish("__internal__")
|
||||
}
|
||||
}
|
||||
|
||||
// GetEvents retrieves events for an actor from a specific version
|
||||
func (es *InMemoryEventStore) GetEvents(actorID string, fromVersion int64) ([]*aether.Event, error) {
|
||||
es.mu.RLock()
|
||||
|
||||
Reference in New Issue
Block a user