コード例 #1
0
        private async Task ConnectToPersistentSubscriptionInternal()
        {
            var cred = _connectionProvider.OpsCredentials;
            var con  = await _connectionProvider.GetActiveConnection();

            await con.ConnectToPersistentSubscriptionAsync(_stream, _groupName, EventAppeared, SubscriptionDropped, cred, _bufferSize);
        }
コード例 #2
0
        private async Task <ProjectionStatus> ReadStreamAsync(string stream, EventStoreProjectionContext context,
                                                              CancellationToken cancellationToken = default(CancellationToken))
        {
            _logger.LogDebug("ReadFromStreamAsync {Stream} {ProjectorId}", stream, context.ProjectorId);
            var sw = Stopwatch.StartNew();

            var cred = _connectionProvider.OpsCredentials;
            //var src = await _connectionProvider.GetActiveConnection().ConfigureAwait(false);
            var src = _connectionProvider.GetActiveConnection();

            StreamEventsSlice slice;

            long sliceStart = StreamPosition.Start;

            do
            {
                _logger.LogTrace("Reading Slice from {0}", sliceStart);

                slice = await src.ReadStreamEventsForwardAsync(stream, sliceStart, _options.InitReadCount, true, cred).ConfigureAwait(false);

                _logger.LogTrace("Next Event: {0} , IsEndOfStream: {1}", slice.NextEventNumber, slice.IsEndOfStream);

                sliceStart = slice.NextEventNumber;

                foreach (var ev in slice.Events)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        _logger.LogWarning("Initialization Cancelled");
                        return(null);
                    }

                    try
                    {
                        var de = ev.Event.ToDomainEvent();

                        if (de != null)
                        {
                            var entry = new ProjectionEventData
                            {
                                EventData   = de,
                                EventNumber = ev.OriginalEventNumber
                            };

                            await context.HandleEventAsync(entry).ConfigureAwait(false);
                        }
                    }
                    catch (Exception e) when(LogError(e))
                    {
                        _logger.LogError(e, "Error Reading Event: {Stream} {ProjectorId} {OriginalEventNumber}", stream,
                                         context.ProjectorId, ev.OriginalEventNumber);
                    }
                }
            } while (!slice.IsEndOfStream);

            _logger.LogDebug("ReadFromStreamAsync {Stream} {ProjectorId} Finished in {Elapsed} ms", stream, context.ProjectorId, sw.ElapsedMilliseconds);

            return(context.Status);
        }
コード例 #3
0
        public async Task <bool> CheckAggregateExists <T>(string aggregateId, string streamName) where T : AggregateRoot
        {
            _logger.LogDebug("CheckAggregateExists {type} {AggregateId} {StreamName}", typeof(T), aggregateId, streamName);

            //var sliceStart = 1; //Ignores $StreamCreated
            long sliceStart = 0; //Ignores $StreamCreated

            var con = await _provider.GetActiveConnection();

            var currentSlice = await con
                               .ReadStreamEventsForwardAsync(streamName, sliceStart, 1, false, _provider.OpsCredentials)
                               .ConfigureAwait(false);

            if (currentSlice.Status == SliceReadStatus.StreamNotFound)
            {
                _logger.LogTrace("CheckAggregateExists {type} {AggregateId} {StreamName} not found", typeof(T), aggregateId, streamName);
                return(false);
            }

            return(true);
        }
コード例 #4
0
        public async Task ReadStreamAndDoAsync(string streamName, Action <MyRecordedEvent> action, int count = 200)
        {
            _logger.LogDebug("ReadStreamAndDoAsync {StreamName}", streamName);

            long sliceStart = StreamPosition.Start;
            StreamEventsSlice currentSlice;

            //var con = await _provider.GetActiveConnection().ConfigureAwait(false);
            var con = _provider.GetActiveConnection();

            do
            {
                currentSlice =
                    await con.ReadStreamEventsForwardAsync(streamName,
                                                           sliceStart,
                                                           count,
                                                           true,
                                                           _provider.OpsCredentials).ConfigureAwait(false);

                if (currentSlice.Status == SliceReadStatus.Success)
                {
                    sliceStart = currentSlice.NextEventNumber;

                    foreach (var evnt in currentSlice.Events)
                    {
                        var ev = evnt.Event;
                        if (ev.IsJson)
                        {
                            var dataJson = Encoding.UTF8.GetString(ev.Data);
                            var dataJo   = JObject.Parse(dataJson);

                            var metaJson = Encoding.UTF8.GetString(ev.Metadata);
                            var metaJo   = JObject.Parse(metaJson);

                            action(new MyRecordedEvent(ev.EventNumber, ev.EventType, ev.Created, dataJo, metaJo));
                        }
                    }
                }
            } while (!currentSlice.IsEndOfStream);
        }
        private void StartSubscriptionInternal()
        {
            _logger.LogDebug("StartSubscriptionInternal for Projector {ProjectorId} on {Stream}",
                             ProjectorId,
                             StreamName);

            var cred = _connectionProvider.OpsCredentials;
            //var src = await _connectionProvider.GetActiveConnection().ConfigureAwait(false);
            var src = _connectionProvider.GetActiveConnection();

            src.Connected += (s, e) =>
            {
                if (_subscriptionDropped)
                {
                    _logger.LogDebug("Reconnected after Subscription Dropped. Resubscribe to Subscription");
                    _subscriptionDropped = false;
                    SubscribeInternal(src, cred);
                }
            };

            SubscribeInternal(src, cred);
        }
        private async Task StartSubscriptionInternal()
        {
            _logger.LogDebug("StartSubscriptionInternal for Projector {ProjectorId} on {Stream}", ProjectorId, StreamName);

            var cred = _connectionProvider.OpsCredentials;
            var src  = await _connectionProvider.GetActiveConnection();

            long?lastCheckpoint = null;

            if (Status.LastCheckPoint.HasValue && Status.LastCheckPoint != -1)
            {
                lastCheckpoint = Status.LastCheckPoint;
            }

            var sub = src.SubscribeToStreamFrom(StreamName
                                                , lastCheckpoint
                                                , CatchUpSubscriptionSettings.Default
                                                , EventAppeared
                                                , LiveProcessingStarted
                                                , SubscriptionDropped
                                                , cred);

            _cancellationToken.Register(sub.Stop);
        }