Exemplo n.º 1
0
        public async Task <IEnumerable <FakeEntity> > GetListe2Async(ClaimsPrincipal user)
        {
            _logger.LogInformation("GetListe2Async");

            return(await _cache.CacheLoadItems(() => _entityStore.LoadItemsAsync <FakeEntity>(),
                                               CacheKeys.Create <FakeEntity>("liste2")));
        }
Exemplo n.º 2
0
        public async Task <IEnumerable <FakeEntity> > GetListe3Async(ClaimsPrincipal user, CancellationToken cancellationToken)
        {
            _logger.LogInformation("GetListe3Async");

            var items = await _cache.LoadSetItems <FakeEntity>(
                CacheKeys.Create <FakeEntity>("set")
                , x => true, cancellationToken : cancellationToken);

            return(items);
        }
Exemplo n.º 3
0
        private async Task UpdateInitializationStatusAsync(CancellationToken cancellationToken, string projectorId, string status, string statusText)
        {
            cancellationToken.ThrowIfCancellationRequested();
            await _cache.RemoveByPrefixAsync(CacheKeys.Create <ProjectionInitializationStatus>());

            var statusItem = await _entityStore.LoadAsync <ProjectionInitializationStatus>(projectorId, cancellationToken);

            statusItem.Entity.Status     = status;
            statusItem.Entity.StatusText = statusText;
            await _entityStore.SaveAsync(statusItem, cancellationToken);
        }
Exemplo n.º 4
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Starting InitializeAsync");
            var sw = Stopwatch.StartNew();

            foreach (var projector in _projectors)
            {
                if (string.IsNullOrEmpty(projector.StreamName))
                {
                    throw new InvalidOperationException($"Empty Stream in Projector: {projector.GetType().FullName}");
                }
            }
            var streamsToCheck = _projectors.Select(x => x.StreamName).Distinct().ToArray();

            foreach (var s in streamsToCheck)
            {
                _logger.LogDebug("Preparing Stream {StreamName}", s);
                await InsertEmptyDomainEventIfStreamIsEmpty(s);
            }

            foreach (var projector in _projectors)
            {
                if (projector != null)
                {
                    var projectorId = projector.GetType().FullName;
                    await _cache.RemoveByPrefixAsync(CacheKeys.Create <ProjectionInitializationStatus>());

                    var statusItem = await _entityStore.LoadAsync <ProjectionInitializationStatus>(projectorId, cancellationToken);

                    if (statusItem.IsNew)
                    {
                        var entity = statusItem.Entity;
                        entity.ProjectorId   = projectorId;
                        entity.StreamName    = projector.StreamName;
                        entity.Status        = ProjectionInitializationStatus.StatusNew;
                        entity.ModifiedOnUtc = DateTime.UtcNow;
                        await _entityStore.SaveAsync(statusItem, cancellationToken);
                    }
                }
            }

            var tasks = new List <Task>();

            foreach (var projector in _projectors)
            {
                //await InitProjectorInternal(cancellationToken, projector);
                var t = InitProjectorInternal(cancellationToken, projector);
                tasks.Add(t);
            }

            await Task.WhenAll(tasks);

            _logger.LogInformation("EventStore Projection Initializer Finished in {Elapsed:0.0000} ms", sw.ElapsedMilliseconds);
        }
Exemplo n.º 5
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Starting InitializeAsync");
            var sw = Stopwatch.StartNew();

            foreach (var projector in _projectors)
            {
                if (string.IsNullOrEmpty(projector.StreamName))
                {
                    throw new InvalidOperationException($"Empty Stream in Projector: {projector.GetType().FullName}");
                }
            }

            var streamsToCheck = _projectors.Select(x => x.StreamName).Distinct().ToArray();

            await Task.WhenAll(streamsToCheck.Select(s =>
            {
                _logger.LogDebug("Preparing Stream {StreamName}", s);
                return(InsertEmptyDomainEventIfStreamIsEmpty(s));
            })).ConfigureAwait(false);


            await _cache.RemoveByPrefixAsync(CacheKeys.Create <ProjectionInitializationStatus>()).ConfigureAwait(false);

            await Task.WhenAll(_projectors.Select(async projector =>
            {
                var projectorId = projector.GetType().FullName;

                var statusItem = await _entityStore.LoadAsync <ProjectionInitializationStatus>(projectorId, cancellationToken).ConfigureAwait(false);

                if (statusItem.IsNew)
                {
                    var entity           = statusItem.Entity;
                    entity.ProjectorId   = projectorId;
                    entity.StreamName    = projector.StreamName;
                    entity.Status        = ProjectionInitializationStatus.StatusNew;
                    entity.ModifiedOnUtc = DateTime.UtcNow;
                    await _entityStore.SaveAsync(statusItem, cancellationToken).ConfigureAwait(false);
                }
            }));

            var contexte = await Task.WhenAll(_projectors.Select(projector =>
            {
                return(Policy.Handle <Exception>().RetryAsync(2, (exception, i) =>
                                                              _logger.LogError(exception, "Retry {Retry}", i)).ExecuteAsync(() => InitProjectorInternal(cancellationToken, projector)));
            })).ConfigureAwait(false);

            await Task.WhenAll(contexte.Select(ctx => StartProjectorInternal(cancellationToken, ctx))).ConfigureAwait(false);

            _logger.LogInformation("EventStore Projection Initializer Finished in {Elapsed} ms", sw.ElapsedMilliseconds);
        }
        public void CacheKeyTest()
        {
            var strings = new List <string> {
                "s1", "s2"
            };

            CacheKeys.Create <MyEntity1>().Should().Be(MyEntity1.TypeKey);
            CacheKeys.Create <MyEntity1>(strings).Should().Be($"{MyEntity1.TypeKey}:s1:s2");

            CacheKeys.Create <MyEntity1>("s1").Should().Be($"{MyEntity1.TypeKey}:s1");
            CacheKeys.Create <MyEntity1>("s1", 1, 2).Should().Be($"{MyEntity1.TypeKey}:s1:1:2");
            CacheKeys.Create <MyEntity1>(1, 2, strings).Should().Be($"{MyEntity1.TypeKey}:1:2:s1:s2");
            CacheKeys.Create <MyEntity1>("sx", 1, 2, strings).Should().Be($"{MyEntity1.TypeKey}:sx:1:2:s1:s2");
            CacheKeys.Create <MyEntity1>("sx", strings).Should().Be($"{MyEntity1.TypeKey}:sx:s1:s2");
            CacheKeys.Create <MyEntity1>("sx", strings, strings).Should().Be($"{MyEntity1.TypeKey}:sx:s1:s2:s1:s2");
            CacheKeys.Create <MyEntity1>("sx", "sy", strings).Should().Be($"{MyEntity1.TypeKey}:sx:sy:s1:s2");
            CacheKeys.Create <MyEntity1>("sx", "sy").Should().Be($"{MyEntity1.TypeKey}:sx:sy");
            CacheKeys.Create <MyEntity1>("sx", "sy", "sz").Should().Be($"{MyEntity1.TypeKey}:sx:sy:sz");
            CacheKeys.Create <MyEntity1>("sx", "sy", "sz", "szz").Should().Be($"{MyEntity1.TypeKey}:sx:sy:sz:szz");
            CacheKeys.Create <MyEntity1>("sx", "sy", "sz", strings).Should().Be($"{MyEntity1.TypeKey}:sx:sy:sz:s1:s2");
            CacheKeys.Create <MyEntity1>("sx", "sy", 1, 2).Should().Be($"{MyEntity1.TypeKey}:sx:sy:1:2");
            CacheKeys.Create <MyEntity1>("sx", "sy", 1, 2, strings).Should().Be($"{MyEntity1.TypeKey}:sx:sy:1:2:s1:s2");
            CacheKeys.Create <MyEntity1>("sx", "sy", "sz", 1, 2).Should().Be($"{MyEntity1.TypeKey}:sx:sy:sz:1:2");
        }
Exemplo n.º 7
0
        private async Task InitProjectorInternal(CancellationToken cancellationToken, IReadModelProjector projector)
        {
            var projectorId = projector.GetType().FullName;

            projector.Initialize(cancellationToken);

            _logger.LogInformation("Initialize Projector {ProjectorName}", projectorId);

            await UpdateInitializationStatusAsync(cancellationToken, projectorId, ProjectionInitializationStatus.StatusPending, "Starting");

            var context =
                new EventStoreProjectionContext(_loggerFactory, _connectionProvider, _entityStore, projector)
            {
                StreamName  = projector.StreamName,
                ProjectorId = projectorId
            };

            projector.Context = context;

            await _cache.RemoveByPrefixAsync(CacheKeys.Create <ProjectionStatus>());

            var status = await _entityStore.LoadAsync <ProjectionStatus>(projectorId, cancellationToken);

            context.Status = status.Entity;

            if (status.IsNew)
            {
                _logger.LogDebug("Starting Empty Initialization for Projector {Projector}", projectorId);

                await UpdateInitializationStatusAsync(cancellationToken, projectorId, ProjectionInitializationStatus.StatusPending, "StartingInitialization");

                await context.StartInitializationModeAsync();

                await UpdateInitializationStatusAsync(cancellationToken, projectorId, ProjectionInitializationStatus.StatusPending, "InitializationStartingStreamReading");

                try
                {
                    var tempStatus = await ReadStreamAsync(context.StreamName, context, cancellationToken);

                    if (tempStatus != null)
                    {
                        status.Entity.LastCheckPoint = tempStatus.LastCheckPoint;
                        status.Entity.ModifiedOnUtc  = tempStatus.ModifiedOnUtc;
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error ReadStreamAsync");
                    throw;
                }

                await UpdateInitializationStatusAsync(cancellationToken, projectorId, ProjectionInitializationStatus.StatusPending, "InitializationStartingCopy");

                try
                {
                    await projector.CopyEntitiesAsync(context.EntityStore, _entityStore, cancellationToken);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error CopyEntitites");
                    throw;
                }

                await context.StopInitializationModeAsync();

                await _entityStore.SaveAsync(status, cancellationToken);

                await UpdateInitializationStatusAsync(cancellationToken, projectorId, ProjectionInitializationStatus.StatusPending, "InitializationFinished");
            }

            cancellationToken.ThrowIfCancellationRequested();
            _logger.LogDebug("Starting Subscription on EventStore for Projector {Projector}", context.ProjectorId);
            await context.StartSubscription(cancellationToken);

            await UpdateInitializationStatusAsync(cancellationToken, projectorId, ProjectionInitializationStatus.StatusReady, "Startet");
        }