示例#1
0
 public void OnNext(LogEvent @event)
 {
     foreach (var e in EventConverter.Convert(@event))
     {
         EventQueue.Enqueue(e);
     }
 }
        public async Task <ISubscription> Subscribe(IEventHandlerFactory factory, object[] args = null)
        {
            Subscription s = new Subscription(true);

            var lastPosition = (StreamPosition)await _stream.LastPosition();

            string[]      prefixes = _events.Select(x => x.Name).ToArray();
            FilterOptions filters  = new FilterOptions(EventTypeFilter.Prefix(prefixes));

            // be very careful. We need to subscribe after the global position.

            await _eventStore.SubscribeToAllAsync(lastPosition.GlobalPosition,
                                                  async (s, r, c) =>
            {
                var type = _schema.EventByName(r.Event.EventType);
                if (type != null && _events.Contains(type))
                {
                    using (var scope = factory.Scope())
                    {
                        var handler = scope.CreateHandler(type);

                        var(m, e) = _eventConverter.Convert(type, r);

                        await handler.Execute(m, e);
                    }
                }
            }, true, null, filters);

            return(s);
        }
 private async IAsyncEnumerable <EventEnvelope> ReadStream(string streamName)
 {
     await foreach (var e in _connection.ReadStreamAsync(Direction.Forwards, streamName, StreamRevision.Start,
                                                         100, resolveLinkTos: true))
     {
         var eventType = _projectionSchema.EventByName(e.Event.EventType);
         var(metadata, eventInstance) = _eventConveter.Convert(eventType, e);
         yield return(new EventEnvelope(eventInstance, metadata));
     }
 }
        public async IAsyncEnumerable <IEvent> Get(Guid key)
        {
            var streamName = GetStreamName(key);

            await foreach (var e in _connection.ReadStreamAsync(Direction.Forwards, streamName, StreamRevision.Start,
                                                                100, resolveLinkTos: true))
            {
                var eventType = _aggregateSchema.EventByName(e.Event.EventType);
                var(m, eventInstance) = _eventConverter.Convert(eventType.EventType, e);

                yield return(eventInstance);
            }
        }
示例#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);
        }
示例#6
0
            private async Task OnReadEvent(IStreamSubscription arg1, ResolvedEvent arg2, CancellationToken t)
            {
                var(m, ev) = _converter.Convert <TEvent>(arg2);
                var groupName = typeof(TEvent).FullName.Replace(".", "-");

                try
                {
                    await _connection.Clients.All.SendCoreAsync(groupName, new object[] { m, ev });

                    Log.Information("SignalR hub send event {eventName} to it's clients.", typeof(TEvent).Name);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
示例#7
0
        public void Send(CommittedEventStream committedEventStream)
        {
            if (_senderConfiguration.Topics.Count() == 0)
            {
                return;
            }

            _logger.Information("Sending committed event stream");
            _bridge.Send(committedEventStream);
            var eventContentAndEnvelopes = _eventConverter.Convert(committedEventStream);
            var json = _serializer.ToJson(eventContentAndEnvelopes);

            _logger.Trace("Sending JSON : " + json);
            _senderConfiguration.Topics.ForEach(topic =>
            {
                _logger.Information($"Send committed event stream to topic: '{topic}'");
                _publisher.Publish(topic, json);
            });
        }
        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);
        }