Enable Dead-Letter Queue (DLQ) in development configuration and enhance NATS consumer error handling. Update config.dev.toml to enable DLQ routing for failed messages. Modify NATS consumer to append DLQ subject to stream subjects if enabled. Improve error logging in message processing to include message IDs and DLQ routing status, ensuring better observability and error management.

This commit is contained in:
windyboy
2025-11-19 17:09:52 +08:00
parent 8914156a58
commit 5ad86d6296
4 changed files with 98 additions and 11 deletions
+37 -5
View File
@@ -40,12 +40,44 @@ func NewStreamManager(js nats.JetStreamContext, streamName string, subjects []st
// EnsureStream ensures that the configured JetStream stream exists, creating it if necessary
func (sm *StreamManager) EnsureStream(cfg *StreamConfig) error {
// Check if stream already exists
_, err := sm.js.StreamInfo(sm.streamName)
info, err := sm.js.StreamInfo(sm.streamName)
if err == nil {
sm.logger.Info("JetStream stream verified",
zap.String("stream", sm.streamName),
zap.Strings("subjects", sm.subjects),
)
// Stream exists - check if we need to add any missing subjects
existingSubjects := make(map[string]bool)
for _, subj := range info.Config.Subjects {
existingSubjects[subj] = true
}
// Check if any configured subjects are missing
missingSubjects := []string{}
for _, subj := range sm.subjects {
if !existingSubjects[subj] {
missingSubjects = append(missingSubjects, subj)
}
}
if len(missingSubjects) > 0 {
// Update stream to include missing subjects
updatedSubjects := info.Config.Subjects
updatedSubjects = append(updatedSubjects, missingSubjects...)
info.Config.Subjects = updatedSubjects
_, updateErr := sm.js.UpdateStream(&info.Config)
if updateErr != nil {
return fmt.Errorf("failed to update stream %s with new subjects %v: %w", sm.streamName, missingSubjects, updateErr)
}
sm.logger.Info("Updated JetStream stream with new subjects",
zap.String("stream", sm.streamName),
zap.Strings("added_subjects", missingSubjects),
zap.Strings("all_subjects", updatedSubjects),
)
} else {
sm.logger.Info("JetStream stream verified",
zap.String("stream", sm.streamName),
zap.Strings("subjects", sm.subjects),
)
}
return nil
}