protected override async Task <JobExecutionStatus> RunJobAsync(IClusterOperations cluster, CancellationToken cancellationToken = default)
        {
            bool hasMoreRecords = true;

            while (hasMoreRecords && Data.IsCompleted == false)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    logger.Info(() => $"The job has been cancelled.");
                    return(JobExecutionStatus.Running);
                }

                LoadAggregateCommitsResult result = await eventStorePlayer.LoadAggregateCommitsAsync(Data.PaginationToken).ConfigureAwait(false);

                List <Task> indexTasks = new List <Task>();

                logger.Info(() => $"Loaded aggregate commits count ${result.Commits.Count} using pagination token {result.PaginationToken}");
                foreach (var aggregateCommit in result.Commits)
                {
                    indexTasks.Add(index.IndexAsync(aggregateCommit));
                }

                await Task.WhenAll(indexTasks).ConfigureAwait(false);

                Data.PaginationToken = result.PaginationToken;
                Data = await cluster.PingAsync(Data, cancellationToken).ConfigureAwait(false);

                hasMoreRecords = result.Commits.Any();
            }

            Data.IsCompleted = true;
            Data             = await cluster.PingAsync(Data, cancellationToken).ConfigureAwait(false);

            logger.Info(() => $"The job has been completed.");

            return(JobExecutionStatus.Completed);
        }
Ejemplo n.º 2
0
        public async Task ValidateAsync(EventStreamIntegrityPolicy policy)
        {
            var aggregateCommits = new List <AggregateCommit>();

            byte[] currentId = { 0 };
            await foreach (var commit in player.LoadAggregateCommitsAsync(1000))
            {
                if (ByteArrayHelper.Compare(currentId, commit.AggregateRootId) == false)
                {
                    if (aggregateCommits.Count > 0)
                    {
                        var result = policy.Apply(new EventStream(aggregateCommits));
                        if (result.IsIntegrityViolated)
                        {
                            throw new Exception(result.Output.ToString());
                        }
                        aggregateCommits.Clear();
                    }
                }

                aggregateCommits.Add(commit);
                currentId = commit.AggregateRootId;
            }
        }
Ejemplo n.º 3
0
        protected override async Task <JobExecutionStatus> RunJobAsync(IClusterOperations cluster, CancellationToken cancellationToken = default)
        {
            Dictionary <int, string> processedAggregates = new Dictionary <int, string>();

            string eventTypeId    = Data.SourceEventTypeId;
            bool   hasMoreRecords = true;

            while (hasMoreRecords && Data.IsCompleted == false)
            {
                string paginationToken = Data.EventTypePaging?.PaginationToken;
                LoadIndexRecordsResult indexRecordsResult = await eventToAggregateIndex.EnumerateRecordsAsync(eventTypeId, paginationToken).ConfigureAwait(false);

                IEnumerable <IndexRecord> indexRecords = indexRecordsResult.Records;
                Type          publicEventType          = typeof(IPublicEvent);
                ReplayOptions opt = new ReplayOptions()
                {
                    AggregateIds = indexRecordsResult.Records.Select(indexRecord => AggregateUrn.Parse(Convert.ToBase64String(indexRecord.AggregateRootId), Urn.Base64)),
                    ShouldSelect = commit =>
                    {
                        bool result = (from publicEvent in commit.PublicEvents
                                       let eventType = publicEvent.GetType()
                                                       where publicEventType.IsAssignableFrom(eventType)
                                                       where eventType.GetContractId().Equals(eventTypeId)
                                                       select publicEvent)
                                      .Any();

                        return(result);
                    },
                    After  = Data.After,
                    Before = Data.Before
                };
                LoadAggregateCommitsResult foundAggregateCommits = await eventStorePlayer.LoadAggregateCommitsAsync(opt).ConfigureAwait(false);

                foreach (AggregateCommit arCommit in foundAggregateCommits.Commits)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        logger.Info(() => $"Job has been cancelled.");
                        return(JobExecutionStatus.Running);
                    }

                    foreach (IPublicEvent publicEvent in arCommit.PublicEvents)
                    {
                        if (publicEvent.GetType().GetContractId().Equals(eventTypeId))
                        {
                            var headers = new Dictionary <string, string>()
                            {
                                { MessageHeader.RecipientBoundedContext, Data.RecipientBoundedContext },
                                { MessageHeader.RecipientHandlers, Data.RecipientHandlers }
                            };
                            publicEventPublisher.Publish(publicEvent, headers);
                        }
                    }
                }

                var progress = new ReplayPublicEvents_JobData.EventTypePagingProgress(eventTypeId, indexRecordsResult.PaginationToken, 0, 0);
                Data.MarkEventTypeProgress(progress);
                Data.Timestamp = DateTimeOffset.UtcNow;
                Data           = await cluster.PingAsync(Data, cancellationToken).ConfigureAwait(false);

                hasMoreRecords = indexRecordsResult.Records.Any();
            }

            Data.IsCompleted = true;
            Data.Timestamp   = DateTimeOffset.UtcNow;

            Data = await cluster.PingAsync(Data).ConfigureAwait(false);

            logger.Info(() => $"The job has been completed.");

            return(JobExecutionStatus.Completed);
        }