コード例 #1
0
        private async Task CancelJobAsync(IClusterOperations cluster)
        {
            Data.IsCanceled = true;
            Data.Timestamp  = DateTimeOffset.UtcNow;
            Data            = await cluster.PingAsync(Data).ConfigureAwait(false);

            var finishSignal = progressTracker.GetProgressFinishedSignal();

            signalPublisher.Publish(finishSignal);
        }
コード例 #2
0
ファイル: CronusJob.cs プロジェクト: youraregood/Cronus
        public async Task SyncInitialStateAsync(IClusterOperations cluster, CancellationToken cancellationToken = default)
        {
            Data = await cluster.PingAsync <TData>(cancellationToken);

            if (Data is null)
            {
                Data = BuildInitialData();
            }

            Data = DataOverride(Data);
        }
コード例 #3
0
        private async Task NotifyClusterAsync(string eventTypeId, string paginationToken, IClusterOperations cluster, CancellationToken cancellationToken)
        {
            var progress = new RebuildProjection_JobData.EventPaging(eventTypeId, paginationToken, progressTracker.Processed, progressTracker.TotalEvents);

            Data.MarkEventTypeProgress(progress);
            Data.Timestamp = DateTimeOffset.UtcNow;
            Data           = await cluster.PingAsync(Data, cancellationToken).ConfigureAwait(false);

            var progressSignal = progressTracker.GetProgressSignal();

            signalPublisher.Publish(progressSignal);
        }
コード例 #4
0
        protected override async Task <JobExecutionStatus> RunJob(IClusterOperations cluster, CancellationToken cancellationToken = default)
        {
            bool hasMoreRecords = true;

            while (hasMoreRecords && Data.IsCompleted == false)
            {
                var result = eventStorePlayer.LoadAggregateCommits(Data.PaginationToken);
                foreach (var aggregateCommit in result.Commits)
                {
                    index.Index(aggregateCommit);
                }

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

                hasMoreRecords = result.Commits.Any();
            }

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

            return(JobExecutionStatus.Completed);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
ファイル: CronusJob.cs プロジェクト: Elders/Cronus
        public async Task SyncInitialStateAsync(IClusterOperations cluster, CancellationToken cancellationToken = default)
        {
            try
            {
                Data = await cluster.PingAsync <TData>(cancellationToken).ConfigureAwait(false);

                if (Data is null)
                {
                    Data = BuildInitialData();
                }

                Data = DataOverride(Data);

                using (logger.BeginScope(s => s
                                         .AddScope(Log.JobData, System.Text.Json.JsonSerializer.Serialize <TData>(Data))))
                {
                    logger.Info(() => "Job state synced.");
                }
            }
            catch (Exception ex) when(logger.ErrorException(ex, () => "Error when syncing initial state of a job"))
            {
            }
        }
コード例 #7
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);
        }
コード例 #8
0
        protected override async Task <JobExecutionStatus> RunJobAsync(IClusterOperations cluster, CancellationToken cancellationToken = default)
        {
            ProjectionVersion version = Data.Version;
            Type projectionType       = version.ProjectionName.GetTypeByContract();

            await progressTracker.InitializeAsync(version).ConfigureAwait(false);

            if (await projectionVersionHelper.ShouldBeRetriedAsync(version).ConfigureAwait(false))
            {
                return(JobExecutionStatus.Running);
            }

            if (await projectionVersionHelper.ShouldBeCanceledAsync(version, Data.DueDate).ConfigureAwait(false))
            {
                if (Data.IsCanceled == false)
                {
                    await CancelJobAsync(cluster).ConfigureAwait(false);
                }

                logger.Info(() => $"The job {version} has been cancelled.");
                return(JobExecutionStatus.Canceled);
            }

            await projectionStoreInitializer.InitializeAsync(version).ConfigureAwait(false);

            var startSignal = progressTracker.GetProgressStartedSignal();

            signalPublisher.Publish(startSignal);

            IEnumerable <Type> projectionHandledEventTypes = projectionVersionHelper.GetInvolvedEventTypes(projectionType);

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

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

                    if (Data.IsCanceled || await projectionVersionHelper.ShouldBeCanceledAsync(version, Data.DueDate).ConfigureAwait(false))
                    {
                        if (Data.IsCanceled == false)
                        {
                            await CancelJobAsync(cluster).ConfigureAwait(false);
                        }

                        logger.Info(() => $"The job {version} has been cancelled.");
                        return(JobExecutionStatus.Canceled);
                    }

                    hasMoreRecords = await RebuildEventsAsync(eventTypeId, cluster, cancellationToken).ConfigureAwait(false);
                }
            }

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

            var finishSignal = progressTracker.GetProgressFinishedSignal();

            signalPublisher.Publish(finishSignal);

            logger.Info(() => $"The job has been completed.");
            return(JobExecutionStatus.Completed);
        }
コード例 #9
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);
        }