|
| 1 | +package natsnodev1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + |
| 7 | + "github.com/nats-io/nats.go" |
| 8 | + "github.com/nats-io/nats.go/jetstream" |
| 9 | + sdkv2betalib "github.com/openecosystems/ecosystem/go/oeco-sdk/v2beta" |
| 10 | + zaploggerv1 "github.com/openecosystems/ecosystem/go/oeco-sdk/v2beta/bindings/zap" |
| 11 | + optionv2pb "github.com/openecosystems/ecosystem/go/oeco-sdk/v2beta/gen/platform/options/v2" |
| 12 | + specv2pb "github.com/openecosystems/ecosystem/go/oeco-sdk/v2beta/gen/platform/spec/v2" |
| 13 | + "go.opentelemetry.io/otel/trace" |
| 14 | + "go.uber.org/zap" |
| 15 | + protopb "google.golang.org/protobuf/proto" |
| 16 | +) |
| 17 | + |
| 18 | +type SpecEventBatchListener interface { |
| 19 | + Configure() *ListenerBatchConfiguration |
| 20 | + Listen(ctx context.Context, listenerErr chan sdkv2betalib.SpecListenableErr) |
| 21 | + BatchProcess(ctx context.Context, messages []*ListenerBatchMessage) |
| 22 | +} |
| 23 | + |
| 24 | +// ListenerBatchConfiguration defines the configuration for a listener, including stream type, entity, subject, queue, and Jetstream settings. |
| 25 | +type ListenerBatchConfiguration struct { |
| 26 | + StreamType Stream |
| 27 | + Entity sdkv2betalib.Entity |
| 28 | + BatchSize int |
| 29 | + TypeName string |
| 30 | + Procedure string |
| 31 | + Topic string |
| 32 | + CQRS optionv2pb.CQRSType |
| 33 | + JetstreamConfiguration *jetstream.ConsumerConfig |
| 34 | +} |
| 35 | + |
| 36 | +type ListenerBatchMessage struct { |
| 37 | + Context context.Context |
| 38 | + SpecKey *specv2pb.SpecKey |
| 39 | + Spec *specv2pb.Spec |
| 40 | + Subscription *jetstream.Consumer |
| 41 | + Message *jetstream.Msg |
| 42 | + NatsMessage *nats.Msg |
| 43 | + ListenerBatchConfiguration *ListenerBatchConfiguration |
| 44 | + EventResponseChannel string |
| 45 | + Request protopb.Message |
| 46 | + Response protopb.Message |
| 47 | + SpecError sdkv2betalib.SpecErrorable |
| 48 | +} |
| 49 | + |
| 50 | +// ListenForJetStreamEvents subscribes to a Jetstream subject. |
| 51 | +func ListenForJetStreamEvents(ctx context.Context, env string, listener SpecEventBatchListener) { |
| 52 | + log := *zaploggerv1.Bound.Logger |
| 53 | + configuration := listener.Configure() |
| 54 | + |
| 55 | + if configuration == nil || configuration.Entity == nil || configuration.StreamType == nil { |
| 56 | + log.Error("Configuration is missing. Entity, StreamType are required when configuring a SpecListener") |
| 57 | + panic("Configuration is missing") |
| 58 | + } |
| 59 | + |
| 60 | + js := *Bound.JetStream |
| 61 | + streamName := GetStreamName(env, configuration.StreamType.StreamPrefix(), configuration.Entity.TypeName()) |
| 62 | + |
| 63 | + stream, err := js.Stream(ctx, streamName) |
| 64 | + if err != nil { |
| 65 | + log.Error("Could not find stream: " + streamName + "; " + err.Error()) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + c, err := stream.CreateOrUpdateConsumer(ctx, *configuration.JetstreamConfiguration) |
| 70 | + if err != nil { |
| 71 | + log.Error("SpecError creating consumer", zap.Error(err)) |
| 72 | + panic("Cannot start consumer") |
| 73 | + } |
| 74 | + |
| 75 | + log.Info("Listening for stream spec events on subject: " + streamName) |
| 76 | + |
| 77 | + for { |
| 78 | + messages, err := c.Fetch(configuration.BatchSize) |
| 79 | + if err != nil && !errors.Is(err, nats.ErrTimeout) { |
| 80 | + log.Error("Fetch error:", zap.Error(err)) |
| 81 | + continue |
| 82 | + } |
| 83 | + |
| 84 | + var batch []*ListenerBatchMessage |
| 85 | + for msg := range messages.Messages() { |
| 86 | + message, _ := convertJetstreamToListenerMessage(configuration, &msg) |
| 87 | + batch = append(batch, message) |
| 88 | + } |
| 89 | + |
| 90 | + listener.BatchProcess(ctx, batch) |
| 91 | + } |
| 92 | + |
| 93 | + // |
| 94 | + //_, err = c.Fetch(configuration.BatchSize, func(msg jetstream.Msg) { |
| 95 | + // messageCtx, message, _ := convertJetstreamToListenerMessage(configuration, &msg) |
| 96 | + // |
| 97 | + // // The Processor is responsible for replying to the Reply subject and responding with any errors |
| 98 | + // listener.BatchProcess(messageCtx, message) |
| 99 | + // // RespondToJetstreamEvent(messageCtx, message) |
| 100 | + //}, jetstream.ConsumeErrHandler(func(_ jetstream.ConsumeContext, err error) { |
| 101 | + // fmt.Println(err) |
| 102 | + //})) |
| 103 | + //if err != nil { |
| 104 | + // log.Fatal("consume error", zap.Error(err)) |
| 105 | + //} |
| 106 | + |
| 107 | + // |
| 108 | +} |
| 109 | + |
| 110 | +// RespondToJetstreamEvent processes an inbound message, modifies the provided message, and sends a response through NATS. |
| 111 | +func RespondToJetstreamEvent(_ context.Context, message *ListenerMessage) { |
| 112 | + log := *zaploggerv1.Bound.Logger |
| 113 | + jm := *message.Message |
| 114 | + |
| 115 | + err4 := jm.Ack() |
| 116 | + if err4 != nil { |
| 117 | + log.Error("SpecError acknowledging message", zap.Error(err4)) |
| 118 | + return |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func convertJetstreamToListenerMessage(config *ListenerBatchConfiguration, msg *jetstream.Msg) (*ListenerBatchMessage, sdkv2betalib.SpecErrorable) { |
| 123 | + // Start with a new ctx here because it must remain transaction safe |
| 124 | + ctx := context.Background() |
| 125 | + s := &specv2pb.Spec{} |
| 126 | + m := *msg |
| 127 | + err := protopb.Unmarshal(m.Data(), s) |
| 128 | + if err != nil { |
| 129 | + return nil, sdkv2betalib.ErrServerInternal.WithInternalErrorDetail(errors.New("could not unmarshall spec"), err) |
| 130 | + } |
| 131 | + |
| 132 | + parentSpanCtx := convertSpecSpanContextToContext(s) |
| 133 | + ctx = trace.ContextWithRemoteSpanContext(ctx, parentSpanCtx) |
| 134 | + |
| 135 | + return &ListenerBatchMessage{ |
| 136 | + Context: ctx, |
| 137 | + Spec: s, |
| 138 | + Subscription: nil, |
| 139 | + Message: &m, |
| 140 | + ListenerBatchConfiguration: config, |
| 141 | + }, nil |
| 142 | +} |
0 commit comments