private void PublishMessageAsync(ProcessingCommand processingCommand, IApplicationMessage message, int retryTimes) { var command = processingCommand.Message; _ioHelper.TryAsyncActionRecursively("PublishApplicationMessageAsync", () => _applicationMessagePublisher.PublishAsync(message), currentRetryTimes => PublishMessageAsync(processingCommand, message, currentRetryTimes), result => { CompleteCommand(processingCommand, CommandStatus.Success, message.GetType().FullName, _jsonSerializer.Serialize(message)); }, () => string.Format("[application message:[id:{0},type:{1}],command:[id:{2},type:{3}]]", message.Id, message.GetType().Name, command.Id, command.GetType().Name), errorMessage => { _logger.Fatal(string.Format("Publish application message has unknown exception, the code should not be run to here, errorMessage: {0}", errorMessage)); }, retryTimes, true); }
private Task PublishMessageAsync(ProcessingCommand processingCommand, IApplicationMessage message, int retryTimes, TaskCompletionSource <bool> taskSource) { var command = processingCommand.Message; _ioHelper.TryAsyncActionRecursivelyWithoutResult("PublishApplicationMessageAsync", () => _applicationMessagePublisher.PublishAsync(message), currentRetryTimes => PublishMessageAsync(processingCommand, message, currentRetryTimes, taskSource), async() => { await CompleteCommand(processingCommand, CommandStatus.Success, message.GetType().FullName, _jsonSerializer.Serialize(message)).ConfigureAwait(false); taskSource.SetResult(true); }, () => string.Format("[application message:[id:{0},type:{1}],command:[id:{2},type:{3}]]", message.Id, message.GetType().Name, command.Id, command.GetType().Name), null, retryTimes, true); return(taskSource.Task); }
private void PublishMessageAsync(ProcessingCommand processingCommand, IApplicationMessage message, int retryTimes) { var command = processingCommand.Message; _ioHelper.TryAsyncActionRecursively <AsyncTaskResult>("PublishApplicationMessageAsync", () => _messagePublisher.PublishAsync(message), currentRetryTimes => PublishMessageAsync(processingCommand, message, currentRetryTimes), result => { NotifyCommandExecuted(processingCommand, CommandStatus.Success, null, null); }, () => string.Format("[application message:[id:{0},type:{1}],command:[id:{2},type:{3}]]", message.Id, message.GetType().Name, command.Id, command.GetType().Name), () => NotifyCommandExecuted(processingCommand, CommandStatus.Failed, null, "Publish application message async failed."), retryTimes); }
async Task PublishApplicationMessageAsync(ProcessingCommand processingCommand, IApplicationMessage applicationMessage) { var result = await Policy .Handle <Exception>() .OrResult <AsyncExecutedResult>(r => !r.Succeeded) .WaitAndRetryForeverAsync( retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (delegateResult, retryCount, retryAttempt) => logger.LogError(delegateResult.Exception ?? delegateResult.Result.Exception, $"命令产生的应用消息发布失败,重试。 [CommandType = {processingCommand.Command.GetType()}, CommandId = {processingCommand.Command.Id}, ApplicationMessageType = {applicationMessage.GetType()}, ApplicationMessageId = {applicationMessage.Id}, ApplicationMessageRoutingKey = {applicationMessage.GetRoutingKey()}, RetryCount = {retryCount}, RetryAttempt = {retryAttempt}]")) .ExecuteAsync(() => applicationMessagePublisher.PublishAsync(applicationMessage)); if (result.Succeeded) { logger.LogDebug($"命令产生的应用消息发布成功。 [CommandType = {processingCommand.Command.GetType()}, CommandId = {processingCommand.Command.Id}, ApplicationMessageType = {applicationMessage.GetType()}, ApplicationMessageId = {applicationMessage.Id}, ApplicationMessageRoutingKey = {applicationMessage.GetRoutingKey()}]"); await processingCommand.OnQueueProcessedAsync(CommandExecutedStatus.Succeeded, objectSerializer.Serialize(applicationMessage), applicationMessage.GetTag()); } else { logger.LogDebug(result.Exception, $"命令产生的应用消息发布失败。 [CommandType = {processingCommand.Command.GetType()}, CommandId = {processingCommand.Command.Id}, ApplicationMessageType = {applicationMessage.GetType()}, ApplicationMessageId = {applicationMessage.Id}, ApplicationMessageRoutingKey = {applicationMessage.GetRoutingKey()}]"); } }