public async Task CompleteMessage(ProcessingCommand processingCommand, CommandResult commandResult) { using (await _asyncLock.LockAsync()) { LastActiveTime = DateTime.Now; try { if (processingCommand.Sequence == _consumedSequence + 1) { _messageDict.Remove(processingCommand.Sequence); await CompleteCommand(processingCommand, commandResult); _consumedSequence = ProcessNextCompletedCommands(processingCommand.Sequence); } else if (processingCommand.Sequence > _consumedSequence + 1) { _requestToCompleteCommandDict[processingCommand.Sequence] = commandResult; } else if (processingCommand.Sequence < _consumedSequence + 1) { _messageDict.Remove(processingCommand.Sequence); await CompleteCommand(processingCommand, commandResult); _requestToCompleteCommandDict.Remove(processingCommand.Sequence); } } catch (Exception ex) { _logger.Error(string.Format("Command mailbox complete command failed, commandId: {0}, aggregateRootId: {1}", processingCommand.Message.Id, processingCommand.Message.AggregateRootId), ex); } } }
public void PublishDomainEventAsync(ProcessingCommand processingCommand, DomainEventStream eventStream) { if (eventStream.Items == null || eventStream.Items.Count == 0) { eventStream.Items = processingCommand.Items; } var eventStreamMessage = new DomainEventStreamMessage(processingCommand.Message.Id, eventStream.AggregateRootId, eventStream.Version, eventStream.AggregateRootTypeName, eventStream.Events, eventStream.Items); PublishDomainEventAsync(processingCommand, eventStreamMessage, 0); }
public void EnqueueMessage(ProcessingCommand message) { lock (_lockObj) { message.Sequence = _maxOffset; message.Mailbox = this; _messageDict.TryAdd(message.Sequence, message); _maxOffset++; } RegisterForExecution(); }
public void Run() { if (_stopHandling == 1) { return; } var hasException = false; ProcessingCommand processingMessage = null; try { if (HasRemainningMessage()) { processingMessage = GetNextMessage(); IncreaseConsumingOffset(); if (processingMessage != null) { _messageHandler.HandleAsync(processingMessage); } } } catch (Exception ex) { hasException = true; if (ex is IOException) { //We need to retry the command. DecreaseConsumingOffset(); } if (processingMessage != null) { var command = processingMessage.Message; _logger.Error(string.Format("Failed to handle command [id: {0}, type: {1}]", command.Id, command.GetType().Name), ex); } else { _logger.Error("Failed to run command mailbox.", ex); } } finally { if (hasException || processingMessage == null) { ExitHandlingMessage(); if (HasRemainningMessage()) { RegisterForExecution(); } } } }
private void CompleteMessageWithResult(ProcessingCommand processingCommand, CommandResult commandResult) { try { processingCommand.Complete(commandResult); } catch (Exception ex) { _logger.Error(string.Format("Failed to complete command, commandId: {0}, aggregateRootId: {1}", processingCommand.Message.Id, processingCommand.Message.AggregateRootId), ex); } }
private Task CompleteCommand(ProcessingCommand processingCommand, CommandResult commandResult) { try { return(processingCommand.CompleteAsync(commandResult)); } catch (Exception ex) { _logger.Error(string.Format("Failed to complete command, commandId: {0}, aggregateRootId: {1}", processingCommand.Message.Id, processingCommand.Message.AggregateRootId), ex); return(Task.CompletedTask); } }
public void EnqueueMessage(ProcessingCommand message) { lock (_lockObj) { message.Sequence = _nextSequence; message.Mailbox = this; if (_messageDict.TryAdd(message.Sequence, message)) { _nextSequence++; } } RegisterForExecution(); }
public void EnqueueMessage(ProcessingCommand message) { lock (_lockObj) { message.Sequence = _nextSequence; message.Mailbox = this; if (_messageDict.TryAdd(message.Sequence, message)) { _nextSequence++; } } LastActiveTime = DateTime.Now; TryRun(); }
public async Task CompleteMessage(ProcessingCommand message, CommandResult result) { try { if (_messageDict.TryRemove(message.Sequence, out ProcessingCommand removed)) { LastActiveTime = DateTime.Now; await message.CompleteAsync(result).ConfigureAwait(false); } } catch (Exception ex) { _logger.Error(string.Format("{0} complete message with result failed, aggregateRootId: {1}, messageId: {2}, messageSequence: {3}, result: {4}", GetType().Name, AggregateRootId, message.Message.Id, message.Sequence, result), ex); } }
public void EnqueueMessage(ProcessingCommand message) { lock (_lockObj) { message.Sequence = NextSequence; message.MailBox = this; if (_messageDict.TryAdd(message.Sequence, message)) { NextSequence++; _logger.DebugFormat("{0} enqueued new message, aggregateRootId: {1}, messageId: {2}, messageSequence: {3}", GetType().Name, AggregateRootId, message.Message.Id, message.Sequence); LastActiveTime = DateTime.Now; TryRun(); } } }
public void EnqueueMessage(ProcessingCommand message) { lock (_lockObj) { message.Sequence = _nextSequence; message.MailBox = this; if (_messageDict.TryAdd(message.Sequence, message)) { _nextSequence++; _logger.InfoFormat("{0} enqueued new message, message: {1}, sequence: {2}", GetType().Name, _jsonSerializer.Serialize(message), message.Sequence); LastActiveTime = DateTime.Now; TryRun(); } else { _logger.ErrorFormat("{0} enqueue message failed, message: {1}, sequence: {2}", GetType().Name, _jsonSerializer.Serialize(message), message.Sequence); } } }
public async void Run() { LastActiveTime = DateTime.Now; while (_isPaused) { _logger.InfoFormat("Command mailbox is pausing and we should wait for a while, aggregateRootId: {0}", AggregateRootId); _pauseWaitHandle.WaitOne(1000); } ProcessingCommand processingCommand = null; try { _processingWaitHandle.Reset(); _isProcessingCommand = true; var count = 0; while (_consumingSequence < _nextSequence && count < _batchSize) { processingCommand = GetProcessingCommand(_consumingSequence); if (processingCommand != null) { await _messageHandler.Handle(processingCommand); } _consumingSequence++; count++; } } catch (Exception ex) { _logger.Error(string.Format("Command mailbox run has unknown exception, aggregateRootId: {0}, commandId: {1}", AggregateRootId, processingCommand != null ? processingCommand.Message.Id : string.Empty), ex); Thread.Sleep(1); } finally { _isProcessingCommand = false; _processingWaitHandle.Set(); Exit(); if (_consumingSequence < _nextSequence) { TryRun(); } } }
public void CompleteMessage(ProcessingCommand message, CommandResult commandResult) { lock (_lockObj2) { if (message.Sequence == _consumedOffset + 1) { _messageDict.Remove(message.Sequence); _consumedOffset = message.Sequence; CompleteMessageWithResult(message, commandResult); ProcessRequestToCompleteOffsets(); } else if (message.Sequence > _consumedOffset + 1) { _requestToCompleteOffsetDict[message.Sequence] = commandResult; } else if (message.Sequence < _consumedOffset + 1) { _messageDict.Remove(message.Sequence); _requestToCompleteOffsetDict.Remove(message.Sequence); } } }
public void CompleteMessage(ProcessingCommand processingCommand, CommandResult commandResult) { lock (_lockObj2) { try { if (processingCommand.Sequence == _consumedSequence + 1) { _messageDict.Remove(processingCommand.Sequence); CompleteCommandWithResult(processingCommand, commandResult); _consumedSequence = ProcessNextCompletedCommands(processingCommand.Sequence); } else if (processingCommand.Sequence > _consumedSequence + 1) { _requestToCompleteSequenceDict[processingCommand.Sequence] = commandResult; } else if (processingCommand.Sequence < _consumedSequence + 1) { _messageDict.Remove(processingCommand.Sequence); CompleteCommandWithResult(processingCommand, commandResult); _requestToCompleteSequenceDict.Remove(processingCommand.Sequence); } if (_logger.IsDebugEnabled) { _logger.DebugFormat("Command mailbox complete command success, commandId: {0}, aggregateRootId: {1}", processingCommand.Message.Id, processingCommand.Message.AggregateRootId); } } catch (Exception ex) { _logger.Error(string.Format("Command mailbox complete command failed, commandId: {0}, aggregateRootId: {1}", processingCommand.Message.Id, processingCommand.Message.AggregateRootId), ex); } finally { RegisterForExecution(); } } }
private void PublishDomainEventAsync(ProcessingCommand processingCommand, DomainEventStreamMessage eventStream, int retryTimes) { _ioHelper.TryAsyncActionRecursively<AsyncTaskResult>("PublishDomainEventAsync", () => _domainEventPublisher.PublishAsync(eventStream), currentRetryTimes => PublishDomainEventAsync(processingCommand, eventStream, currentRetryTimes), result => { _logger.DebugFormat("Publish domain events success, {0}", eventStream); processingCommand.Complete(new CommandResult(CommandStatus.Success, processingCommand.Message.Id, eventStream.AggregateRootId, null, null)); }, () => string.Format("[eventStream:{0}]", eventStream), errorMessage => processingCommand.Complete(new CommandResult(CommandStatus.Failed, processingCommand.Message.Id, eventStream.AggregateRootId, null, errorMessage ?? "Publish domain event async failed.")), retryTimes); }
public EventProcessingContext(IAggregateRoot aggregateRoot, EventStream eventStream, ProcessingCommand processingCommand) { AggregateRoot = aggregateRoot; EventStream = eventStream; ProcessingCommand = processingCommand; }
private void PublishEvents(IDictionary<string, string> contextItems, ProcessingCommand processingCommand, EventStream eventStream) { var publishEvents = new Func<bool>(() => { try { _eventPublisher.PublishEvent(contextItems, eventStream); return true; } catch (Exception ex) { _logger.Error(string.Format("Exception raised when publishing events:{0}", eventStream), ex); return false; } }); var publishEventsCallback = new Func<object, bool>(obj => { var currentProcessingCommand = obj as ProcessingCommand; currentProcessingCommand.CommandExecuteContext.OnCommandExecuted(currentProcessingCommand.Command, CommandStatus.Success, 0, null); return true; }); _actionExecutionService.TryAction("PublishEvents", publishEvents, 3, new ActionInfo("PublishEventsCallback", publishEventsCallback, processingCommand, null)); }
private void PublishDomainEventAsync(ProcessingCommand processingCommand, DomainEventStreamMessage eventStream, int retryTimes) { _ioHelper.TryAsyncActionRecursively("PublishEventAsync", () => _domainEventPublisher.PublishAsync(eventStream), currentRetryTimes => PublishDomainEventAsync(processingCommand, eventStream, currentRetryTimes), result => { if (_logger.IsDebugEnabled) { _logger.DebugFormat("Publish event success, {0}", eventStream); } var commandHandleResult = processingCommand.CommandExecuteContext.GetResult(); CompleteCommand(processingCommand, new CommandResult(CommandStatus.Success, processingCommand.Message.Id, eventStream.AggregateRootId, commandHandleResult, typeof(string).FullName)); }, () => string.Format("[eventStream:{0}]", eventStream), errorMessage => { _logger.Fatal(string.Format("Publish event has unknown exception, the code should not be run to here, errorMessage: {0}", errorMessage)); }, retryTimes, true); }
private void CompleteCommand(ProcessingCommand processingCommand, CommandResult commandResult) { processingCommand.Mailbox.CompleteMessage(processingCommand, commandResult); _logger.InfoFormat("Complete command, aggregateId: {0}", processingCommand.Message.AggregateRootId); }
private void PublishDomainEventAsync(ProcessingCommand processingCommand, DomainEventStreamMessage eventStream, int retryTimes) { _ioHelper.TryAsyncActionRecursively<AsyncTaskResult>("PublishDomainEventAsync", () => _domainEventPublisher.PublishAsync(eventStream), currentRetryTimes => PublishDomainEventAsync(processingCommand, eventStream, currentRetryTimes), result => { if (_logger.IsDebugEnabled) { _logger.DebugFormat("Publish domain events success, {0}", eventStream); } var commandHandleResult = processingCommand.CommandExecuteContext.GetResult(); processingCommand.Complete(new CommandResult(CommandStatus.Success, processingCommand.Message.Id, eventStream.AggregateRootId, commandHandleResult, typeof(string).FullName)); }, () => string.Format("[eventStream:{0}]", eventStream), errorMessage => processingCommand.Complete(new CommandResult(CommandStatus.Failed, processingCommand.Message.Id, eventStream.AggregateRootId, errorMessage ?? "Publish domain event async failed.", typeof(string).FullName)), retryTimes); }