示例#1
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);
        }
示例#2
0
 protected DeleteExistingStreamSpecification(StreamRevision expectedStreamRevision) : this(
         new DeleteReq.Types.Options {
     Revision = expectedStreamRevision.ToUInt64(),
 })
 {
 }