示例#1
0
        private async Task <EventStoreProjectionContext> 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").ConfigureAwait(false);

            var context =
                new EventStoreProjectionContext(_loggerFactory, _connectionProvider, _entityStore, projector, projectorId);

            projector.Context = context;

            //   await _cache.RemoveByPrefixAsync1(CacheKeys.Create<ProjectionStatus>());
            var status = await _entityStore.LoadAsync <ProjectionStatus>(projectorId, cancellationToken).ConfigureAwait(false);

            context.Status = status.Entity;

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

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

                await context.StartInitializationModeAsync().ConfigureAwait(false);

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

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

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

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

                await context.UpdateStreamStatusAsync().ConfigureAwait(false);

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

                await context.StopInitializationModeAsync().ConfigureAwait(false);

                await _entityStore.SaveAsync(status, cancellationToken).ConfigureAwait(false);

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

            cancellationToken.ThrowIfCancellationRequested();
            return(context);
        }
示例#2
0
        private async Task StartProjectorInternal(CancellationToken cancellationToken, EventStoreProjectionContext context)
        {
            _logger.LogDebug("Starting Subscription on EventStore for Projector {Projector}", context.ProjectorId);
            await context.StartSubscription(cancellationToken).ConfigureAwait(false);

            await UpdateInitializationStatusAsync(cancellationToken, context.ProjectorId, ProjectionInitializationStatus.StatusReady, "Startet").ConfigureAwait(false);
        }