public Position GetLastPosition(EventSubscriberType eventSubscriberType) { var lastPosition = LastPosition(eventSubscriberType); if (lastPosition == null) { return(Position.Start); } return(new Position(lastPosition.CommitPosition, lastPosition.PreparePosition)); }
public async Task StoreLastPosition(EventSubscriberType eventSubscriberType, long commit, long prepare) { var firstPosition = LastPosition(eventSubscriberType); if (firstPosition != null) { firstPosition.Update(commit, prepare); } else { _context.Add(new EventPosition(eventSubscriberType, commit, prepare)); } await _context.SaveChangesAsync(); }
public EventPosition(EventSubscriberType type, long commitPosition, long preparePosition) { Type = type; CommitPosition = commitPosition; PreparePosition = preparePosition; }
private EventPosition LastPosition(EventSubscriberType eventSubscriberType) { return(_context.Set <EventPosition>().FirstOrDefault(e => e.Type == eventSubscriberType)); }
public void Subscribe(EventSubscriberType eventSubscriberType, List <string> eventsToHandle) { var catchUpSettings = new CatchUpSubscriptionSettings(10, 500, true, false); var position = _eventStorePositionService.GetLastPosition(eventSubscriberType); var userCredentials = new UserCredentials(_eventStoreOption.Value.Username, _eventStoreOption.Value.Password); using (var _eventStoreConnection = EventStoreConnection.Create(_eventStoreOption.Value.ConnectionString)) { _eventStoreConnection.ConnectAsync().Wait(); _eventStoreConnection.SubscribeToAllFrom( position, catchUpSettings, eventAppeared: async(EventStoreCatchUpSubscription sub, ResolvedEvent resolvedEvent) => { if (resolvedEvent.OriginalStreamId.StartsWith("$")) { return; } if (resolvedEvent.Event.Data.Length <= 0) { return; } if (eventsToHandle.IsNotNull() && !eventsToHandle.Contains(resolvedEvent.Event.EventType)) { return; } try { await _eventStorePositionService.StoreLastPosition( eventSubscriberType, resolvedEvent.OriginalPosition.Value.CommitPosition, resolvedEvent.OriginalPosition.Value.PreparePosition ); var @event = _storedEventSerializer.As(resolvedEvent.Event.EventType, resolvedEvent.Event.Data); if (@event == null) { return; } await _mediator.Publish(@event); } catch (Exception ex) { Console.WriteLine(ex); } }, userCredentials: userCredentials, subscriptionDropped: (x, b, n) => { Console.WriteLine(n.ToString()); } ); Console.WriteLine("waiting for events. press enter to exit"); Console.ReadLine(); } }