コード例 #1
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var       streamName = RedisExtensions.GetPrimaryStream();
            IDatabase db         = _connectionMultiplexer.GetDatabase();
            var       batchSize  = _configuration.BatchSize;

            while (true)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }
                var checkpoint = await _checkPoint.GetCheckpoint <T>();

                var streamInfo = db.StreamInfo(streamName);
                if (streamInfo.LastEntry.Id == checkpoint)
                {
                    await Task.Delay(_configuration.Delay, cancellationToken);

                    continue;
                }

                var currentSlice = await db.StreamReadAsync(streamName, checkpoint, batchSize);

                foreach (var streamEntry in currentSlice)
                {
                    foreach (var streamEntryValue in streamEntry.Values)
                    {
                        var @event = JsonConvert.DeserializeObject <AggregateEvent>(streamEntryValue.Value.ToString(), _serializerSettings);
                        await _eventDispatcher.DispatchAsync(@event);

                        await _checkPoint.SetCheckpoint <T>(streamEntry.Id);
                    }
                }
            }
        }