ScreenController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Microsoft.AspNetCore.Mvc;
  2. using TEAMModelOS.SDK.DI;
  3. using TEAMModelOS.SDK;
  4. using Microsoft.AspNetCore.SignalR;
  5. using HTEX.Complex.Services;
  6. using System.Text.Json;
  7. using TEAMModelOS.SDK.Extension;
  8. namespace HTEX.Complex.Controllers
  9. {
  10. [ApiController]
  11. [Route("api/screen")]
  12. public class ScreenController : ControllerBase
  13. {
  14. private readonly DingDing _dingDing;
  15. private readonly IHttpClientFactory _httpClient;
  16. private readonly IConfiguration _configuration;
  17. private readonly AzureStorageFactory _azureStorage;
  18. private readonly AzureRedisFactory _azureRedis;
  19. private readonly IHubContext<SignalRScreenServerHub> _screenServerHub;
  20. private readonly ILogger<ScreenController> _logger;
  21. public ScreenController(AzureRedisFactory azureRedis, IHttpClientFactory httpClient, IConfiguration configuration, AzureStorageFactory azureStorage,
  22. DingDing dingDing, IHubContext<SignalRScreenServerHub> screenServerHub , ILogger<ScreenController> logger)
  23. {
  24. _httpClient = httpClient;
  25. _configuration = configuration;
  26. _azureStorage = azureStorage;
  27. _dingDing = dingDing;
  28. _azureRedis=azureRedis;
  29. _screenServerHub = screenServerHub;
  30. _logger = logger;
  31. }
  32. /// <summary>
  33. /// 外部触发任务发送
  34. /// </summary>
  35. /// <returns></returns>
  36. [HttpPost("send-task")]
  37. public async Task<IActionResult> SendTask(JsonElement json)
  38. {
  39. var values = await _azureRedis.GetRedisClient(8).HashGetAllAsync("ScreenApi:clients");
  40. if (values!=null)
  41. {
  42. foreach (var value in values)
  43. {
  44. ScreenClient screenClient = value.Value.ToString().ToObject<ScreenClient>();
  45. //先检查状态是否是在忙碌,在时间戳范围里,如果不在时间戳范围,强制free。
  46. if (!screenClient.status!.Equals(ScreenConstant.idle) && screenClient.last_time + screenClient.timeout+ screenClient.delay + ScreenConstant.time_excess < DateTimeOffset.UtcNow.ToUnixTimeMilliseconds())
  47. {
  48. screenClient.status = ScreenConstant.idle;
  49. }
  50. if (screenClient.status!.Equals(ScreenConstant.idle) && !string.IsNullOrWhiteSpace(screenClient.connid))
  51. {
  52. _logger.LogInformation($"客户端当前空闲=>{screenClient.name},{screenClient.region},{screenClient.clientid},分发任务......");
  53. //连接成功,马上分发任务。
  54. var task = await GenPDFService.SentTask(_azureRedis, _azureStorage);
  55. if (task.genQueue!=null && task.genRedis!=null && !string.IsNullOrWhiteSpace(task.genQueue.cntName))
  56. {
  57. screenClient.status = ScreenConstant.busy;
  58. screenClient.last_time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  59. await _screenServerHub.Clients.Client(screenClient.connid!).SendAsync ("ReceiveMessage", new ScreenProcessMessage
  60. {
  61. connid = screenClient.connid,
  62. clientid =screenClient.clientid,
  63. status = ScreenConstant.busy,
  64. grant_type =screenClient.grant_type,
  65. message_type= MessageType.task_send_success,
  66. content =$"{task.genQueue.ToJsonString()}",//从Redis中获取任务信息
  67. });
  68. }
  69. else
  70. {
  71. _logger.LogInformation($"客户端当前空闲=>{screenClient.name},{screenClient.region},{screenClient.clientid},暂无任务可领取的任务......");
  72. if (task.genRedis!=null)
  73. {
  74. string msgError = $"分发任务异常原因=>{screenClient.name},{screenClient.region},{screenClient.clientid}:{task.msg}\ngenQueue:{task.genQueue?.ToJsonString()}\ngenRedis:{task.genRedis?.ToJsonString()}";
  75. _logger.LogInformation(msgError);
  76. await _dingDing.SendBotMsg(msgError, GroupNames.成都开发測試群組);
  77. }
  78. else
  79. {
  80. _logger.LogInformation($"分发任务异常原因=>{screenClient.name},{screenClient.region},{screenClient.clientid}:{task.msg}\n");
  81. }
  82. await _screenServerHub.Clients.Client(screenClient.connid!).SendAsync("ReceiveMessage", new ScreenProcessMessage
  83. {
  84. connid = screenClient.connid,
  85. clientid = screenClient.clientid,
  86. status = ScreenConstant.idle,
  87. grant_type = screenClient.grant_type,
  88. message_type= MessageType.task_send_error,
  89. content = task.msg
  90. });
  91. }
  92. }
  93. await _azureRedis.GetRedisClient(8).HashSetAsync($"ScreenApi:clients", screenClient.clientid, screenClient.ToJsonString());
  94. }
  95. }
  96. return Ok();
  97. }
  98. }
  99. }