/// <summary> /// 生产消息 /// </summary> /// <param name="message">消息</param> /// <returns></returns> Task <IMessageResult> IMessagePoster.Post(IInlineMessage message) { message.Offline(); var item = new ConsulQueueItem { ID = message.ID, Name = message.Topic, }; message.ID = null; message.Topic = null; message.Trace = null; item.Message = SmartSerializer.SerializeMessage(message).ToUtf8Bytes(); redisQueues.Enqueue(item); semaphore.Release(); message.RealState = MessageState.AsyncQueue; LogRecorder.MonitorDetails("[ConsulPoster.Post] 消息已投入发送队列,将在后台静默发送直到成功"); return(Task.FromResult <IMessageResult>(null));//直接使用状态 }
private async Task <bool> DoPost(ILogger logger, string path, ConsulQueueItem item) { var state = false; try { var result = await client.Event.Fire(new UserEvent { ID = item.ID, Name = item.Name, Payload = item.Message }); state = result.StatusCode == System.Net.HttpStatusCode.OK; } catch (Exception ex) { logger.Warning(() => $"[异步消息投递] {item.ID} 发送失败.{ex.Message}"); redisQueues.Enqueue(item); state = false; } if (state) { if (item.FileName != null) { try { File.Delete(item.FileName); logger.Warning(() => $"[异步消息投递] {item.ID} 发送成功,删除备份文件,{item.FileName}"); } catch { logger.Warning(() => $"[异步消息投递] {item.ID} 发送成功,删除备份文件失败,{item.FileName}"); } } else { logger.Debug(() => $"[异步消息投递] {item.ID} 发送成功"); } return(true); } //写入异常文件 ++item.Try; if (item.FileName != null) { return(false); } item.FileName = Path.Combine(path, $"{item.ID}.msg"); logger.Warning(() => $"[异步消息投递] {item.ID} 发送失败,记录异常备份文件,{item.FileName}"); try { File.WriteAllText(item.FileName, SmartSerializer.ToString(item)); } catch (Exception ex) { item.FileName = null; logger.Error(() => $"[异步消息投递] {item.ID} 发送失败,记录异常备份文件失败.错误:{ex.Message}.文件名:{item.FileName}"); } return(false); }