|
@@ -7,6 +7,12 @@ using System.Web;
|
|
|
using System.Text;
|
|
|
|
|
|
using StackExchange.Redis;
|
|
|
+using Microsoft.Extensions.Logging;
|
|
|
+using TEAMModelOS.SDK.Models;
|
|
|
+using Google.Protobuf.WellKnownTypes;
|
|
|
+using Microsoft.Extensions.DependencyInjection;
|
|
|
+using TEAMModelOS.SDK.DI.Device;
|
|
|
+
|
|
|
namespace HTEX.Complex.Services
|
|
|
{
|
|
|
public class SignalRScreenServerHub : Hub<IClient>
|
|
@@ -16,12 +22,14 @@ namespace HTEX.Complex.Services
|
|
|
private readonly AzureRedisFactory _azureRedis;
|
|
|
private readonly AzureStorageFactory _azureStorage;
|
|
|
private readonly DingDing _dingDing;
|
|
|
- public SignalRScreenServerHub(AzureRedisFactory azureRedis, ILogger<SignalRScreenServerHub> logger ,AzureStorageFactory azureStorage, DingDing dingDing)
|
|
|
+ private readonly CoreDevice _device;
|
|
|
+ public SignalRScreenServerHub(AzureRedisFactory azureRedis, ILogger<SignalRScreenServerHub> logger, AzureStorageFactory azureStorage, DingDing dingDing, CoreDevice device)
|
|
|
{
|
|
|
_logger = logger;
|
|
|
_azureRedis = azureRedis;
|
|
|
_azureStorage = azureStorage;
|
|
|
_dingDing = dingDing;
|
|
|
+ _device=device;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -30,6 +38,9 @@ namespace HTEX.Complex.Services
|
|
|
/// <returns></returns>
|
|
|
public override async Task OnConnectedAsync()
|
|
|
{
|
|
|
+ var serverDevice = await _device.GetCoreDevice();
|
|
|
+
|
|
|
+
|
|
|
var connid = Context.ConnectionId;
|
|
|
var httpContext = Context.GetHttpContext();
|
|
|
if (httpContext != null)
|
|
@@ -49,15 +60,16 @@ namespace HTEX.Complex.Services
|
|
|
{
|
|
|
connid = connid,
|
|
|
grant_type = grant_type,
|
|
|
- clientid= clientid
|
|
|
+ clientid= clientid,
|
|
|
+ serverid=serverDevice.deviceId,
|
|
|
};
|
|
|
- await _azureRedis.GetRedisClient(8).HashSetAsync($"SignalRClient:connects", connid, client.ToJsonString());
|
|
|
- ClientDevice device = HttpUtility.UrlDecode(_device, Encoding.Unicode).ToObject<ClientDevice>();
|
|
|
+ await _azureRedis.GetRedisClient(8).HashSetAsync($"SignalRClient:connects:{serverDevice.deviceId}", connid, client.ToJsonString());
|
|
|
+ ScreenClient device = HttpUtility.UrlDecode(_device, Encoding.Unicode).ToObject<ScreenClient>();
|
|
|
switch (true)
|
|
|
{
|
|
|
case bool when grant_type.Equals(ScreenConstant.grant_type):
|
|
|
ScreenClient screenClient ;
|
|
|
- var value = await _azureRedis.GetRedisClient(8).HashGetAsync($"ScreenApi:clients", client.clientid);
|
|
|
+ var value = await _azureRedis.GetRedisClient(8).HashGetAsync($"ScreenApi:clients:{serverDevice.deviceId}", client.clientid);
|
|
|
if (value!=default && value.HasValue)
|
|
|
{
|
|
|
screenClient = value.ToString().ToObject<ScreenClient>();
|
|
@@ -93,6 +105,7 @@ namespace HTEX.Complex.Services
|
|
|
screenClient.cpu = device.cpu;
|
|
|
screenClient.ram = device.ram;
|
|
|
screenClient.last_time= DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
+ screenClient.deviceId = serverDevice.deviceId;
|
|
|
//连接成功,发送消息给客户端。
|
|
|
await SendConnection(connid, new ConnectionMessage
|
|
|
{
|
|
@@ -107,7 +120,7 @@ namespace HTEX.Complex.Services
|
|
|
if (screenClient.status!.Equals(ScreenConstant.idle)) {
|
|
|
_logger.LogInformation($"客户端当前空闲=>{screenClient.name},{screenClient.region},{clientid},分发任务......");
|
|
|
//连接成功,马上分发任务。
|
|
|
- var task = await TaskService.SentTask(_azureRedis,_azureStorage);
|
|
|
+ var task = await TaskService.SentTask(_azureRedis,_azureStorage, _logger, serverDevice);
|
|
|
if (task.genQueue!=null && task.genRedis!=null && !string.IsNullOrWhiteSpace(task.genQueue.cntName))
|
|
|
{
|
|
|
screenClient.status = ScreenConstant.busy;
|
|
@@ -121,9 +134,10 @@ namespace HTEX.Complex.Services
|
|
|
message_type= MessageType.task_send_success,
|
|
|
content =$"{task.genQueue.ToJsonString()}",//从Redis中获取任务信息
|
|
|
});
|
|
|
+ _logger.LogInformation($"分发任务:{task.genQueue.ToJsonString()}");
|
|
|
}
|
|
|
else {
|
|
|
- _logger.LogInformation($"客户端当前空闲=>{screenClient.name},{screenClient.region},{clientid},暂无任务可领取的任务......");
|
|
|
+ // _logger.LogInformation($"客户端当前空闲=>{screenClient.name},{screenClient.region},{clientid},暂无任务可领取的任务......");
|
|
|
if (task.genRedis!=null)
|
|
|
{
|
|
|
string msgError = $"分发任务异常原因=>{screenClient.name},{screenClient.region},{clientid}:{task.msg}\ngenQueue:{task.genQueue?.ToJsonString()}\ngenRedis:{task.genRedis?.ToJsonString()}";
|
|
@@ -144,7 +158,7 @@ namespace HTEX.Complex.Services
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
- await _azureRedis.GetRedisClient(8).HashSetAsync($"ScreenApi:clients", client.clientid, screenClient.ToJsonString());
|
|
|
+ await _azureRedis.GetRedisClient(8).HashSetAsync($"ScreenApi:clients:{serverDevice.deviceId}", client.clientid, screenClient.ToJsonString());
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
@@ -165,30 +179,40 @@ namespace HTEX.Complex.Services
|
|
|
|
|
|
public override async Task OnDisconnectedAsync(Exception? exception)
|
|
|
{
|
|
|
+
|
|
|
+ var serverDevice= await _device.GetCoreDevice();
|
|
|
var connid = Context.ConnectionId;
|
|
|
- var redisData = await _azureRedis.GetRedisClient(8).HashGetAsync($"SignalRClient:connects", connid);
|
|
|
+ var redisData = await _azureRedis.GetRedisClient(8).HashGetAsync($"SignalRClient:connects:{serverDevice.deviceId}", connid);
|
|
|
_logger.LogInformation($"客户端断开连接=>{connid} ");
|
|
|
///连接配置,并且使用钉钉 离线通知。
|
|
|
if (!redisData.IsNullOrEmpty)
|
|
|
{
|
|
|
var client = redisData.ToString().ToObject<SignalRClient>();
|
|
|
- await _azureRedis.GetRedisClient(8).HashDeleteAsync($"SignalRClient:connects", connid);
|
|
|
+ await _azureRedis.GetRedisClient(8).HashDeleteAsync($"SignalRClient:connects:{serverDevice.deviceId}", connid);
|
|
|
if (client != null)
|
|
|
{
|
|
|
await Groups.RemoveFromGroupAsync(connid, client.grant_type!);
|
|
|
- var value = await _azureRedis.GetRedisClient(8).HashGetAsync($"ScreenApi:clients", client.clientid);
|
|
|
+ var value = await _azureRedis.GetRedisClient(8).HashGetAsync($"ScreenApi:clients:{serverDevice.deviceId}", client.clientid);
|
|
|
if (value!=default && value.HasValue)
|
|
|
{
|
|
|
ScreenClient screenClient = value.ToString().ToObject<ScreenClient>() ;
|
|
|
_logger.LogInformation($"客户端断开连接=>{connid},{screenClient.name},{screenClient.region},{screenClient.clientid} ");
|
|
|
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
// 判断是否过期
|
|
|
- if (screenClient.status!.Equals(ScreenConstant.busy ) && screenClient.last_time+screenClient.timeout+screenClient.delay+ ScreenConstant.time_excess <=now)
|
|
|
+ if (screenClient.status!.Equals(ScreenConstant.busy))
|
|
|
{
|
|
|
+ //超时了
|
|
|
+ if (screenClient.last_time+screenClient.timeout+screenClient.delay+ ScreenConstant.time_excess <=now)
|
|
|
+ {
|
|
|
+ screenClient.status=ScreenConstant.offline;
|
|
|
+ screenClient.connid= string.Empty;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
screenClient.status=ScreenConstant.offline;
|
|
|
screenClient.connid= string.Empty;
|
|
|
- await _azureRedis.GetRedisClient(8).HashSetAsync($"ScreenApi:clients", client.clientid, screenClient.ToJsonString());
|
|
|
}
|
|
|
+ await _azureRedis.GetRedisClient(8).HashSetAsync($"ScreenApi:clients:{serverDevice.deviceId}", client.clientid, screenClient.ToJsonString());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -200,13 +224,14 @@ namespace HTEX.Complex.Services
|
|
|
////接收消息
|
|
|
//如果是超时,放回队列。
|
|
|
///分发新任务。
|
|
|
+ var serverDevice = await _device.GetCoreDevice();
|
|
|
long nowNew = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
var connid = Context.ConnectionId;
|
|
|
PDFGenQueue? task = message.content?.ToObject<PDFGenQueue>();
|
|
|
ScreenClient screenClient = null;
|
|
|
try
|
|
|
{ //释放客户端的忙碌状态。
|
|
|
- var redisData = await _azureRedis.GetRedisClient(8).HashGetAsync($"SignalRClient:connects", connid);
|
|
|
+ var redisData = await _azureRedis.GetRedisClient(8).HashGetAsync($"SignalRClient:connects:{serverDevice.deviceId}", connid);
|
|
|
var client = redisData.ToString().ToObject<SignalRClient>();
|
|
|
await SendConnection(connid, new ConnectionMessage
|
|
|
{
|
|
@@ -217,14 +242,14 @@ namespace HTEX.Complex.Services
|
|
|
message_type= MessageType.conn_success,
|
|
|
content = $"客户端空闲,等待任务分发......"
|
|
|
});
|
|
|
- var value = await _azureRedis.GetRedisClient(8).HashGetAsync($"ScreenApi:clients", client.clientid);
|
|
|
+ var value = await _azureRedis.GetRedisClient(8).HashGetAsync($"ScreenApi:clients:{serverDevice.deviceId}", client.clientid);
|
|
|
if (value!=default && value.HasValue)
|
|
|
{
|
|
|
screenClient = value.ToString().ToObject<ScreenClient>();
|
|
|
screenClient.status=ScreenConstant.idle;
|
|
|
screenClient.last_time=nowNew;
|
|
|
screenClient.taskComplete++;
|
|
|
- await _azureRedis.GetRedisClient(8).HashSetAsync($"ScreenApi:clients", client.clientid,screenClient.ToJsonString());
|
|
|
+ await _azureRedis.GetRedisClient(8).HashSetAsync($"ScreenApi:clients:{serverDevice.deviceId}", client.clientid,screenClient.ToJsonString());
|
|
|
_logger.LogInformation($"客户端空闲,等待任务分发......=>{connid},{screenClient.name},{screenClient.region},{screenClient.clientid} ");
|
|
|
}
|
|
|
|
|
@@ -248,12 +273,12 @@ namespace HTEX.Complex.Services
|
|
|
///如果是超时,放回队列。
|
|
|
if (message.result==4)
|
|
|
{
|
|
|
- await _azureRedis.GetRedisClient(8).ListLeftPushAsync($"PDFGen:Queue", task.ToJsonString());
|
|
|
+ await _azureRedis.GetRedisClient(8).ListLeftPushAsync($"PDFGen:Queue:{serverDevice.deviceId}", task.ToJsonString());
|
|
|
}
|
|
|
}
|
|
|
if (screenClient!=null && screenClient.status!.Equals(ScreenConstant.idle))
|
|
|
{
|
|
|
- var taskData = await TaskService.SentTask(_azureRedis, _azureStorage);
|
|
|
+ var taskData = await TaskService.SentTask(_azureRedis, _azureStorage, _logger, serverDevice);
|
|
|
if (taskData.genQueue!=null && taskData.genRedis!=null && !string.IsNullOrWhiteSpace(taskData.genQueue.cntName))
|
|
|
{
|
|
|
screenClient.status = ScreenConstant.busy;
|
|
@@ -267,10 +292,11 @@ namespace HTEX.Complex.Services
|
|
|
message_type= MessageType.task_send_success,
|
|
|
content =$"{taskData.genQueue.ToJsonString()}",//从Redis中获取任务信息
|
|
|
});
|
|
|
+ _logger.LogInformation($"分发任务:{taskData.genQueue.ToJsonString()}");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- _logger.LogInformation($"客户端当前空闲=>{screenClient.name},{screenClient.region},{screenClient.clientid},暂无任务可领取的任务......");
|
|
|
+ //_logger.LogInformation($"客户端当前空闲=>{screenClient.name},{screenClient.region},{screenClient.clientid},暂无任务可领取的任务......");
|
|
|
if (taskData.genRedis!=null)
|
|
|
{
|
|
|
string msgError = $"分发任务异常原因=>{screenClient.name},{screenClient.region},{screenClient.clientid}:{taskData.msg}\ngenQueue:{taskData.genQueue?.ToJsonString()}\ngenRedis:{taskData.genRedis?.ToJsonString()}";
|
|
@@ -291,7 +317,7 @@ namespace HTEX.Complex.Services
|
|
|
content = taskData.msg
|
|
|
});
|
|
|
}
|
|
|
- await _azureRedis.GetRedisClient(8).HashSetAsync($"ScreenApi:clients", screenClient.clientid, screenClient.ToJsonString());
|
|
|
+ await _azureRedis.GetRedisClient(8).HashSetAsync($"ScreenApi:clients:{serverDevice.deviceId}", screenClient.clientid, screenClient.ToJsonString());
|
|
|
}
|
|
|
}
|
|
|
public async Task SendConnection(string connectionId, MessageBody msg)
|