public void Completed(ICommand command, DateTime completedAt) { _originalTracker.Completed(command, completedAt); _offlineCommandCollection.FindOneAndReplace( Builders <OfflineCommandInfo> .Filter.Eq(m => m.Id, command.MessageId.ToString()), new OfflineCommandInfo(command, true, completedAt), new FindOneAndReplaceOptions <OfflineCommandInfo, OfflineCommandInfo>() { IsUpsert = true }); }
public async Task Handle(T message) { LoggerThreadContextManager.MarkCommandExecution(message); if (Logger.IsDebugEnabled) { Logger.DebugFormat("Handling {0} {1}", message.GetType().FullName, message.MessageId); } int i = 0; bool retry = false; var notifyTo = message.GetContextData(MessagesConstants.ReplyToHeader); CommandHandled replyCommandHandled = null; Boolean success = false; Exception lastException = null; do { try { _messagesTracker.ElaborationStarted(message, DateTime.UtcNow); _commandHandler.HandleAsync(message).Wait(); //need to wait, or you will free the worker rebus thread and you will dispatch many concurrent handler. _messagesTracker.Completed(message, DateTime.UtcNow); if (notifyTo != null && message.GetContextData("disable-success-reply", "false") != "true") { replyCommandHandled = new CommandHandled( notifyTo, message.MessageId, CommandHandled.CommandResult.Handled, message.Describe() ); replyCommandHandled.CopyHeaders(message); _bus.Reply(replyCommandHandled).Wait(); } success = true; retry = false; } catch (Exception ex) { lastException = ex; if (!_commandExecutionExceptionHelper.Handle(ex, message, i, out retry, out replyCommandHandled)) { //Handler is not able to handle the exception, simply retrhow _messagesTracker.Failed(message, DateTime.UtcNow, ex); LoggerThreadContextManager.ClearMarkCommandExecution(); throw; } } i++; //if we reach here we need to increment the counter. } while (retry); if (!success) { _messagesTracker.Failed(message, DateTime.UtcNow, lastException); if (notifyTo != null && replyCommandHandled != null) { await _bus.Reply(replyCommandHandled).ConfigureAwait(false); } } if (Logger.IsDebugEnabled) { Logger.DebugFormat("Handled {0} {1} {2}", message.GetType().FullName, message.MessageId, message.Describe()); } LoggerThreadContextManager.ClearMarkCommandExecution(); }
public void Handle(T message) { try { Logger.MarkCommandExecution(message); if (Logger.IsDebugEnabled) { Logger.DebugFormat("Handling {0} {1}", message.GetType().FullName, message.MessageId); } int i = 0; bool done = false; var notifyTo = message.GetContextData(MessagesConstants.ReplyToHeader); while (!done && i < 100) { try { _messagesTracker.ElaborationStarted(message.MessageId, DateTime.UtcNow); _commandHandler.Handle(message); _messagesTracker.Completed(message.MessageId, DateTime.UtcNow); done = true; if (notifyTo != null && message.GetContextData("disable-success-reply", "false") != "true") { var replyCommand = new CommandHandled( notifyTo, message.MessageId, CommandHandled.CommandResult.Handled, message.Describe() ); replyCommand.CopyHeaders(message); _bus.Reply(replyCommand); } } catch (ConflictingCommandException ex) { MetricsHelper.MarkConcurrencyException(); // retry if (Logger.IsInfoEnabled) { Logger.InfoFormat(ex, "Handled {0} {1} [{2}], concurrency exception. Retry count: {3}", message.GetType().FullName, message.MessageId, message.Describe(), i); } if (i++ > 5) { Thread.Sleep(new Random(DateTime.Now.Millisecond).Next(i * 10)); } } catch (DomainException ex) { MetricsHelper.MarkDomainException(); done = true; _messagesTracker.Failed(message.MessageId, DateTime.UtcNow, ex); if (notifyTo != null) { var replyCommand = new CommandHandled( notifyTo, message.MessageId, CommandHandled.CommandResult.Failed, message.Describe(), ex.Message, true ); replyCommand.CopyHeaders(message); _bus.Reply(replyCommand); } _logger.ErrorFormat(ex, "DomainException on command {0} [MessageId: {1}] : {2} : {3}", message.GetType(), message.MessageId, message.Describe(), ex.Message); } catch (Exception ex) { _logger.ErrorFormat(ex, "Generic Exception on command {0} [MessageId: {1}] : {2} : {3}", message.GetType(), message.MessageId, message.Describe(), ex.Message); _messagesTracker.Failed(message.MessageId, DateTime.UtcNow, ex); throw; //rethrow exception. } } if (done == false) { _logger.ErrorFormat("Too many conflict on command {0} [MessageId: {1}] : {2}", message.GetType(), message.MessageId, message.Describe()); var exception = new Exception("Command failed. Too many Conflicts"); _messagesTracker.Failed(message.MessageId, DateTime.UtcNow, exception); } if (Logger.IsDebugEnabled) { Logger.DebugFormat("Handled {0} {1} {3}", message.GetType().FullName, message.MessageId, message.Describe()); } } finally { Logger.ClearCommandExecution(); } }
public void Handle(T message) { if (Logger.IsDebugEnabled) { Logger.DebugFormat("Handling {0} {1}", message.GetType().FullName, message.MessageId); } int i = 0; bool done = false; var notifyTo = message.GetContextData(MessagesConstants.ReplyToHeader); while (!done && i < 100) { i++; try { _commandHandler.Handle(message); _messagesTracker.Completed(message.MessageId, DateTime.UtcNow); done = true; if (notifyTo != null && message.GetContextData("disable-success-reply", "false") != "true") { var replyCommand = new CommandHandled( notifyTo, message.MessageId, CommandHandled.CommandResult.Handled, message.Describe() ); replyCommand.CopyHeaders(message); _bus.Reply(replyCommand); } } catch (ConflictingCommandException ex) { // retry if (Logger.IsDebugEnabled) { Logger.DebugFormat("Handled {0} {1}, concurrency exception. Retry count: {2}", message.GetType().FullName, message.MessageId, i); } if (i++ > 5) { Thread.Sleep(new Random(DateTime.Now.Millisecond).Next(i * 10)); } } catch (DomainException ex) { done = true; _messagesTracker.Failed(message.MessageId, DateTime.UtcNow, ex); if (notifyTo != null) { var replyCommand = new CommandHandled( notifyTo, message.MessageId, CommandHandled.CommandResult.Failed, message.Describe(), ex.Message, true ); replyCommand.CopyHeaders(message); _bus.Reply(replyCommand); } } } if (done == false) { var exception = new Exception("Command failed. Too many Conflicts"); _messagesTracker.Failed(message.MessageId, DateTime.UtcNow, exception); } if (Logger.IsDebugEnabled) { Logger.DebugFormat("Handled {0} {1}", message.GetType().FullName, message.MessageId); } }