SeventeenGRPC.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using XYY.Data.Model.Standard.Tracking._17;
  7. using XYY.TaskTrack.Standard;
  8. using XYY.gRpc.Tracking;
  9. using Grpc.Core;
  10. using Microsoft.Extensions.Logging;
  11. using Grpc.Net.Client;
  12. namespace XYY.WindowsService.ReshMQ.Works
  13. {
  14. public interface ISeventeenGRPC : IBaseConsumer<XYY.Data.Model.Standard.Tracking._17._17TrackingMQData>
  15. {
  16. }
  17. public class SeventeenGRPC : ISeventeenGRPC
  18. {
  19. GrpcChannel channelBase;
  20. private readonly ILogger<SeventeenGRPC> _logger;
  21. public SeventeenGRPC(ILogger<SeventeenGRPC> logger, TrackingGrpcChanel channelBase)
  22. {
  23. _logger = logger;
  24. this.channelBase = channelBase.Channel;
  25. }
  26. public async Task Consume(_17TrackingMQData message)
  27. {
  28. try
  29. {
  30. SeventeenConvert.SeventeenConvertClient client = new SeventeenConvert.SeventeenConvertClient(channelBase);
  31. var resut = await client.ConvertAsync(new SeventeenConvertReq
  32. {
  33. Data = message.Data,
  34. Event = message.Event,
  35. Sign = message.Sign
  36. });
  37. if (!resut.Success)
  38. {
  39. string resMeg = resut.Message;
  40. string reqMsg = message.Data;
  41. string errorTitle = "获取17Tracking转换";
  42. await HandErroLog(resMeg, reqMsg, errorTitle);
  43. return;
  44. }
  45. Db.DbClient dbClient = new Db.DbClient(channelBase);
  46. var addMessage = await dbClient.AddTrackingAsync(resut.Data);
  47. if (!addMessage.Success)
  48. await HandErroLog("保存数据失败", message.Data, addMessage.Message);
  49. }
  50. catch (Exception ex)
  51. {
  52. await HandErroLog("GRPC服务调用失败", message.Data, ex.Message);
  53. }
  54. }
  55. private async Task HandErroLog(string resMeg, string reqMsg, string errorTitle)
  56. {
  57. HandErrorLog.HandErrorLogClient logClient = new HandErrorLog.HandErrorLogClient(channelBase);
  58. var req = new HandErrorLogReq();
  59. req.Logs.Add(new ErrorLog
  60. {
  61. Source = "17Tracking",
  62. ErrorMessage = resMeg,
  63. RequestMessage = reqMsg,
  64. TargetTime = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(DateTime.Now.ToUniversalTime()),
  65. TrackingNumber = String.Empty
  66. });
  67. await logClient.PostDataAsync(req);
  68. _logger.LogError("api:{api} res:{res} req:{req}", errorTitle, resMeg, reqMsg);
  69. }
  70. }
  71. }