private DispatchResult DoDispatchingLogic(EventStream eventStream) { var isDispatched = false; try { DispatchEventStreamToEventHandlers(eventStream); isDispatched = true; UpdatePublishedEventStreamVersion(eventStream); return DispatchResult.Success; } catch (Exception ex) { _logger.Error(string.Format("Exception raised when dispatching event stream:{0}", eventStream.GetStreamInformation()), ex); if (!isDispatched) { RePublishEventStream(eventStream); return DispatchResult.RePublished; } else { return DispatchResult.Failed; } } }
private bool CommitEventStream(EventStream stream) { bool executed = false; //Persist event stream. var result = PersistEventStream(stream); if (result == EventStreamPersistResult.Success) { //Refresh memory cache. try { _memoryCacheRefreshService.Refresh(stream); } catch (Exception ex) { _logger.Error(string.Format("Unknown exception raised when refreshing memory cache for event stream:{0}", stream.GetStreamInformation()), ex); } //Publish event stream. try { _eventPublisher.Publish(stream); executed = true; } catch (Exception ex) { _logger.Error(string.Format("Unknown exception raised when publishing event stream:{0}", stream.GetStreamInformation()), ex); } //Complete command async result if exist. _commandAsyncResultManager.TryComplete(stream.CommandId, null); } else if (result == EventStreamPersistResult.Retried) { executed = true; } return executed; }
private EventStreamPersistResult ProcessException(Exception exception, EventStream stream) { if (exception is ConcurrentException) { if (IsEventStreamCommitted(stream)) { return EventStreamPersistResult.Success; } var commandInfo = _processingCommandCache.Get(stream.CommandId); _logger.Error(string.Format( "Concurrent exception raised when persisting event stream, command:{0}, event stream info:{1}", commandInfo.Command.GetType().Name, stream.GetStreamInformation()), exception); //Enforce to refresh memory cache before retring the command //to enusre that when we retring the command, the memeory cache is at the latest status. _memoryCacheRefreshService.Refresh(_aggregateRootTypeProvider.GetAggregateRootType(stream.AggregateRootName), stream.AggregateRootId); _retryCommandService.RetryCommand(commandInfo, exception); return EventStreamPersistResult.Retried; } else { var commandInfo = _processingCommandCache.Get(stream.CommandId); _logger.Error(string.Format( "Unknown exception raised when persisting event stream, command:{0}, event stream info:{1}", commandInfo.Command.GetType().Name, stream.GetStreamInformation()), exception); return EventStreamPersistResult.UnknownException; } }
private void TryRefreshMemoryCache(AggregateRoot aggregateRoot, EventStream eventStream) { try { aggregateRoot.AcceptEventStream(eventStream); _memoryCache.Set(aggregateRoot); } catch (Exception ex) { _logger.Error(string.Format("Exception raised when refreshing memory cache by event stream:{0}", eventStream.GetStreamInformation()), ex); } }
private bool TryPublishEventStream(EventStream eventStream) { var success = false; try { _eventPublisher.Publish(eventStream); success = true; } catch (Exception ex) { _logger.Error(string.Format("Exception raised when publishing event stream:{0}", eventStream.GetStreamInformation()), ex); } return success; }
private bool TryCallSynchronizersBeforeEventPersisting(IEnumerable<IEventPersistenceSynchronizer> synchronizers, EventStream eventStream, ErrorInfo errorInfo) { if (synchronizers != null && synchronizers.Count() > 0) { foreach (var synchronizer in synchronizers) { try { synchronizer.OnBeforePersisting(eventStream); } catch (Exception ex) { var commandInfo = _processingCommandCache.Get(eventStream.CommandId); errorInfo.Exception = ex; errorInfo.ErrorMessage = string.Format( "Exception raised when calling synchronizer's OnBeforePersisting method. synchronizer:{0}, command:{1}, event stream:{2}", synchronizer.GetType().Name, commandInfo.Command.GetType().Name, eventStream.GetStreamInformation()); _logger.Error(errorInfo.ErrorMessage, ex); return false; } } } return true; }
private void TryCallSynchronizersAfterEventPersisted(IEnumerable<IEventPersistenceSynchronizer> synchronizers, EventStream eventStream) { if (synchronizers != null && synchronizers.Count() > 0) { foreach (var synchronizer in synchronizers) { try { synchronizer.OnAfterPersisted(eventStream); } catch (Exception ex) { var commandInfo = _processingCommandCache.Get(eventStream.CommandId); _logger.Error(string.Format( "Exception raised when calling synchronizer's OnAfterPersisted method. synchronizer:{0}, command:{1}, event stream:{2}", synchronizer.GetType().Name, commandInfo.Command.GetType().Name, eventStream.GetStreamInformation()), ex); } } } }
private PersistResult ProcessException(Exception exception, EventStream eventStream, ErrorInfo errorInfo) { if (exception is ConcurrentException) { if (IsEventStreamCommitted(eventStream)) { return PersistResult.Success; } var commandInfo = _processingCommandCache.Get(eventStream.CommandId); _logger.Error(string.Format( "Concurrent exception raised when persisting event stream, command:{0}, event stream:{1}", commandInfo.Command.GetType().Name, eventStream.GetStreamInformation()), exception); _retryCommandService.RetryCommand(commandInfo, exception); return PersistResult.Retried; } else { var commandInfo = _processingCommandCache.Get(eventStream.CommandId); _logger.Error(string.Format( "Exception raised when persisting event stream, command:{0}, event stream:{1}", commandInfo.Command.GetType().Name, eventStream.GetStreamInformation()), exception); return PersistResult.Failed; } }