Example #1
0
        public Task IndexAsync(CronusMessage message)
        {
            if (message.Payload is IEvent @event)
            {
                eventCounter.IncrementAsync(@event.Unwrap().GetType());
            }

            return(Task.CompletedTask);
        }
Example #2
0
        protected override async Task <JobExecutionStatus> RunJobAsync(IClusterOperations cluster, CancellationToken cancellationToken = default)
        {
            // mynkow. this one fails
            IndexStatus indexStatus = await GetIndexStatusAsync <EventToAggregateRootId>().ConfigureAwait(false);

            if (indexStatus.IsNotPresent())
            {
                return(JobExecutionStatus.Running);
            }

            //projectionStoreInitializer.Initialize(version);

            foreach (Type eventType in eventTypes.Items)
            {
                string eventTypeId = eventType.GetContractId();

                bool hasMoreRecords = true;

                while (hasMoreRecords && Data.IsCompleted == false)
                {
                    RebuildEventCounterIndex_JobData.EventTypeRebuildPaging paging = Data.EventTypePaging.Where(et => et.Type.Equals(eventTypeId, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                    string paginationToken = paging?.PaginationToken;
                    if (string.IsNullOrEmpty(paginationToken))
                    {
                        logger.Info(() => $"Message counter for {eventTypeId} has been reset");
                        // Maybe we should move this to a BeforeRun method.
                        await messageCounter.ResetAsync(eventType).ConfigureAwait(false);
                    }
                    LoadIndexRecordsResult indexRecordsResult = await eventToAggregateIndex.EnumerateRecordsAsync(eventTypeId, paginationToken).ConfigureAwait(false);

                    IEnumerable <IndexRecord> indexRecords = indexRecordsResult.Records;
                    long currentSessionProcessedCount      = 0;
                    foreach (IndexRecord indexRecord in indexRecords)
                    {
                        currentSessionProcessedCount++;

                        string           mess   = Encoding.UTF8.GetString(indexRecord.AggregateRootId);
                        IAggregateRootId arId   = GetAggregateRootId(mess);
                        EventStream      stream = await eventStore.LoadAsync(arId).ConfigureAwait(false);

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

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

                                if (eventTypeId.Equals(@event.GetType().GetContractId(), StringComparison.OrdinalIgnoreCase))
                                {
                                    incrementTasks.Add(messageCounter.IncrementAsync(eventType));
                                }
                            }
                        }

                        await Task.WhenAll(incrementTasks).ConfigureAwait(false);
                    }

                    Data.MarkPaginationTokenAsProcessed(eventTypeId, indexRecordsResult.PaginationToken);
                    Data = await cluster.PingAsync(Data, cancellationToken).ConfigureAwait(false);

                    hasMoreRecords = indexRecordsResult.Records.Any();
                }
            }

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

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

            return(JobExecutionStatus.Completed);
        }