Exemplo n.º 1
0
 public async Task SubscribeToStream(string name, params SubscriptionInfo[] eventTypes)
 {
     var methods  = new Dictionary <string, Func <IStreamSubscription, ResolvedEvent, CancellationToken, Task> >();
     var handlers = eventTypes.ToDictionary(x => x.EventType.Name, x => x.HandlerType);
     var dict     = eventTypes.Select(x => x.EventType).ToDictionary(x => x.Name);
     await _connection.SubscribeToStreamAsync(name, StreamRevision.Start, async (s, r, c) =>
     {
         await OnEventAppearead(s, r, methods, handlers, dict, c);
     }, null, true, OnDropped);
 }
Exemplo n.º 2
0
        public async Task Subscribe(IEventStoreFacade connection)
        {
            _connection     = connection;
            _lastCheckpoint = await _checkpointRepo.GetLastCheckpoint();

            this.Subscription = await _connection.SubscribeToStreamAsync(_streamName,
                                                                         new StreamRevision(_lastCheckpoint ?? 0L),
                                                                         OnEventAppeared,
                                                                         OnLiveProcessingStarted,
                                                                         true,
                                                                         OnSubscriptionDropped);
        }
Exemplo n.º 3
0
            public async Task Configure(IEventStoreFacade connection, IEventHandlerDispatcher dispatcher, Serilog.ILogger logger)
            {
                _logger = logger;
                _logger.Information("Subscribed for {eventName} for local projections. (with EventDispatcher)", typeof(TEvent).Name);

                var stream = $"$et-{typeof(TEvent).Name}";

                this._dispatcher = dispatcher;

                // Should we wait for the subscription? - or should we re-subscribe
                await connection.SubscribeToStreamAsync(stream, OnReadEvent, true);
            }
Exemplo n.º 4
0
            public async Task Configure(IEventStoreFacade connection,
                                        IEventConverter eventConverter,
                                        IHubContext <EventStoreHub> hubConnection, Serilog.ILogger logger)
            {
                Log        = logger;
                _converter = eventConverter;
                var stream = $"$et-{typeof(TEvent).Name}";

                this._connection = hubConnection;
                var t = await connection.SubscribeToStreamAsync(stream, StreamRevision.Start, OnReadEvent, null, true);

                Log.Information("Subscribed for {eventName} for pushing to signalR clients.", typeof(TEvent).Name);
            }
Exemplo n.º 5
0
        public async Task <ISubscription> Subscribe(IEventHandlerFactory factory, object[] args = null)
        {
            if (!factory.SupportedEventTypes.Contains <TEvent>())
            {
                throw new InvalidOperationException(
                          $"Event Handler Factory seems not to support this Event. {typeof(TEvent).Name}");
            }

            // uhhh and we need to know from when...
            Type checkpointRepo = typeof(ICheckpointRepository <,>).MakeGenericType(_schema.Type, typeof(TEvent));

            var repo = (ICheckpointEventRepository <TEvent>)_serviceProvider.GetService(checkpointRepo);

            string streamName = $"$et-{typeof(TEvent).Name}";

            var lastCheckpoint = await repo.GetLastCheckpoint();

            StreamRevision start = StreamRevision.Start;

            if (!lastCheckpoint.HasValue)
            {
                // this is the first time we run this processor.
                var(globalPosition, streamRevision) = await _eventStore.GetLastStreamPosition(streamName);

                start = streamRevision;
                await repo.SaveCheckpoint(start.ToUInt64());
            }
            else
            {
                start = new StreamRevision(lastCheckpoint.Value + 1);
            }
            Subscription s = new Subscription();
            await _eventStore.SubscribeToStreamAsync(streamName, start,
                                                     async (s, r, c) =>
            {
                using (var scope = factory.Scope())
                {
                    var handler = scope.CreateHandler <TEvent>();

                    var(m, e) = _eventConverter.Convert <TEvent>(r);

                    await handler.Execute(m, e);

                    await repo.SaveCheckpoint(r.Link.EventNumber.ToUInt64());
                }
            }, ss => s.MakeLive(), true);

            return(s);
        }
        public async Task <ISubscription> Subscribe(IEventHandlerFactory factory, object[] args = null)
        {
            Subscription s = new Subscription();

            if (!factory.SupportedEventTypes.Contains <TEvent>())
            {
                throw new InvalidOperationException($"Event Handler Factory seems not to support this Event. {typeof(TEvent).Name}");
            }

            // Projections stream type might not be the same as origin of the event.
            // For instance Stream might go directly to memory, where as some events
            // come from EventStore
            // In such a case, we could assume to subscribe from the beginning
            // or we could assume that we want to subscribe from now.
            // For now we subscribe from beginning - however this is a problem.
            // We don't know the nature of this subscription - if it is temporal or not.
            // If it had been temporal, we would subscribe from now
            // It it had not been temporal, we would subscribe from beginning

            var position = await Stream.LastPosition();

            StreamRevision sr = StreamRevision.Start;

            if (position is StreamPosition lastPosition)
            {
                sr = lastPosition.IsStart ? StreamRevision.Start : new StreamRevision(lastPosition.StreamRevision + 1);
            }

            await _eventStore.SubscribeToStreamAsync(STREAM_NAME, sr, async (s, r, c) =>
            {
                using (var scope = factory.Scope())
                {
                    var handler = scope.CreateHandler <TEvent>();

                    var(m, e) = _eventConverter.Convert <TEvent>(r);

                    await handler.Execute(m, e);
                }
            }, ss => s.MakeLive(), true);

            return(s);
        }