gvk
gvk

Reputation: 617

Replay a particular type of event from eventstore

I am currently using the Event Store to handle my events. I currently need to replay a particular type of event as I have made changes in the way they are subscribed and written to DB.

Is this possible? If so, how can it be done? Thanks.

Upvotes: 2

Views: 1553

Answers (2)

epitka
epitka

Reputation: 17637

If you enable $by_event_type projection than you can access that projection stream under

/streams/$et-{event-type}

https://eventstore.org/docs/projections/system-projections/index.html

Then you can read it using .net api if you wish.

Here is some code to get you started

      private static T GetInstanceOfEvent<T>(ResolvedEvent resolvedEvent) where T : BaseEvent
            {

                var metadataString = Encoding.UTF8.GetString(resolvedEvent.Event.Metadata);
                var eventClrTypeName = JObject.Parse(metadataString).Property(EventClrTypeHeader).Value;
                var @event = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(resolvedEvent.Event.Data), Type.GetType((string) eventClrTypeName));
                if (!(@event is BaseEvent))
                {
                    throw new MessageDeserializationException((string) eventClrTypeName, metadataString);
                }

                return @event as T;
            }

            private static IEventStoreConnection GetEventStoreConnection()
            {
                var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["EventStore"].ConnectionString;
                var connection = EventStoreConnection.Create(connectionString);
                connection.ConnectAsync().Wait();
                return connection;
            }

            private static string GetStreamName<T>() where T : BaseEvent
            {
                return "$et-" + typeof(T).Name;
            }

And to read events you can use this code snippet

 StreamEventsSlice currentSlice;
            long nextSliceStart = StreamPosition.Start;
            const int sliceCount = 200;

            do
            {
                currentSlice = await esConnection.ReadStreamEventsForwardAsync(streamName, nextSliceStart, sliceCount, true);

                foreach (var @event in currentSlice.Events)
                {
                    var myEvent = GetInstanceOfEvent<OrderMerchantFeesCalculatedEvent>(@event);


                    TransformEvent(myEvent);
                }

                nextSliceStart = currentSlice.NextEventNumber;

            } while (currentSlice.IsEndOfStream == false);

Upvotes: 0

Piotr Kula
Piotr Kula

Reputation: 9821

You cannot tell EventStore to replay a specific event onto a persistent subscription because the point of the persistent subscription is to keep state for the subscribers.

To achieve this kind of fix you would really need a catch up application to do the work.

And really if you think about, if you replayed ALL the events to a new database then you would have the correct data in there?

So I have a console application that reuses the same logic as the persistent connection but the only difference is:

  1. I change the target database connection string - So this would be a new Database or Collection (not the broken one)
  2. It connects to EventStore and replays all the events from the start
  3. It rebuilds the entire database to the correct state
  4. Switch the business over to the new database

This is the point of EventStore - You just replay all the events to build any database at any time and it will be correct

Your persistent connections deal with new, incoming events and apply updates.

Upvotes: 2

Related Questions