public Task <AsyncTaskResult <EventAppendResult> > BatchAppendAsync(IEnumerable <DomainEventStream> eventStreams) { if (eventStreams.Count() == 0) { return(Task.FromResult(new AsyncTaskResult <EventAppendResult>(AsyncTaskStatus.Success, new EventAppendResult()))); } var eventStreamDict = new Dictionary <string, IList <DomainEventStream> >(); var aggregateRootIdList = eventStreams.Select(x => x.AggregateRootId).Distinct().ToList(); foreach (var aggregateRootId in aggregateRootIdList) { var eventStreamList = eventStreams.Where(x => x.AggregateRootId == aggregateRootId).ToList(); if (eventStreamList.Count > 0) { eventStreamDict.Add(aggregateRootId, eventStreamList); } } var batchAggregateEventAppendResult = new BatchAggregateEventAppendResult(eventStreamDict.Keys.Count); foreach (var entry in eventStreamDict) { BatchAppendAggregateEventsAsync(entry.Key, entry.Value, batchAggregateEventAppendResult, 0); } return(batchAggregateEventAppendResult.TaskCompletionSource.Task); }
private void TryFindEventByCommandIdAsync(string aggregateRootId, string commandId, BatchAggregateEventAppendResult batchAggregateEventAppendResult, int retryTimes) { _ioHelper.TryAsyncActionRecursively("TryFindEventByCommandIdAsync", () => FindAsync(aggregateRootId, commandId), currentRetryTimes => TryFindEventByCommandIdAsync(aggregateRootId, commandId, batchAggregateEventAppendResult, currentRetryTimes), result => { if (result.Data != null) { batchAggregateEventAppendResult.AddCompleteAggregate(aggregateRootId, new AggregateEventAppendResult { EventAppendStatus = EventAppendStatus.DuplicateCommand, DuplicateCommandId = result.Data.CommandId }); } }, () => string.Format("[aggregateRootId:{0}, commandId:{1}]", aggregateRootId, commandId), null, retryTimes, true); }
private void BatchAppendAggregateEventsAsync(string aggregateRootId, IList <DomainEventStream> eventStreamList, BatchAggregateEventAppendResult batchAggregateEventAppendResult, int retryTimes) { _ioHelper.TryAsyncActionRecursively("BatchAppendAggregateEventsAsync", () => BatchAppendAggregateEventsAsync(aggregateRootId, eventStreamList), currentRetryTimes => BatchAppendAggregateEventsAsync(aggregateRootId, eventStreamList, batchAggregateEventAppendResult, currentRetryTimes), result => { var eventAppendStatus = result.Data; if (eventAppendStatus == EventAppendStatus.Success) { batchAggregateEventAppendResult.AddCompleteAggregate(aggregateRootId, new AggregateEventAppendResult { EventAppendStatus = EventAppendStatus.Success }); } else if (eventAppendStatus == EventAppendStatus.DuplicateEvent) { batchAggregateEventAppendResult.AddCompleteAggregate(aggregateRootId, new AggregateEventAppendResult { EventAppendStatus = EventAppendStatus.DuplicateEvent }); } else if (eventAppendStatus == EventAppendStatus.DuplicateCommand) { foreach (var eventStream in eventStreamList) { TryFindEventByCommandIdAsync(aggregateRootId, eventStream.CommandId, batchAggregateEventAppendResult, 0); } } }, () => string.Format("[aggregateRootId: {0}, eventStreamCount: {1}]", aggregateRootId, eventStreamList.Count), null, retryTimes, true); }
private void TryFindEventByCommandIdAsync(string aggregateRootId, string commandId, BatchAggregateEventAppendResult batchAggregateEventAppendResult, int retryTimes) { _ioHelper.TryAsyncActionRecursively("TryFindEventByCommandIdAsync", () => FindAsync(aggregateRootId, commandId), currentRetryTimes => TryFindEventByCommandIdAsync(aggregateRootId, commandId, batchAggregateEventAppendResult, currentRetryTimes), result => { if (result.Data != null) { batchAggregateEventAppendResult.AddCompleteAggregate(aggregateRootId, new AggregateEventAppendResult { EventAppendStatus = EventAppendStatus.DuplicateCommand, DuplicateCommandId = result.Data.CommandId }); } }, () => string.Format("[aggregateRootId:{0}, commandId:{1}]", aggregateRootId, commandId), errorMessage => { _logger.Fatal(string.Format("TryFindEventByCommandIdAsync has unknown exception, the code should not be run to here, aggregateRootId:{0}, commandId:{1}, errorMessage: {2}", aggregateRootId, commandId, errorMessage)); }, retryTimes, true); }