Fix Hive Mind connect logic - allow to reconnect gracefully

This commit is contained in:
dsokolovrudakov 2025-03-12 21:42:55 +02:00
parent 26c4c8e00c
commit 4390a523c7
2 changed files with 29 additions and 15 deletions

View File

@ -1,4 +1,4 @@
using DevOpsProject.CommunicationControl.Logic.Services.Interfaces;
using DevOpsProject.CommunicationControl.Logic.Services.Interfaces;
using DevOpsProject.Shared.Clients;
using DevOpsProject.Shared.Configuration;
using DevOpsProject.Shared.Enums;
@ -64,13 +64,33 @@ namespace DevOpsProject.CommunicationControl.Logic.Services
}
public async Task<HiveOperationalArea> ConnectHive(HiveModel model)
{
bool isHiveAlreadyConnected = await IsHiveConnected(model.HiveID);
if (isHiveAlreadyConnected)
{
_logger.LogWarning("Reconnect Hive request: {@model}", model);
}
else
{
_logger.LogInformation("Trying to connect Hive: {@model}", model);
}
bool result = await _redisService.SetAsync(GetHiveKey(model.HiveID), model);
if (result)
{
_logger.LogInformation("Successfully connected Hive: {@model}", model);
var operationalArea = _spatialService.GetHiveOperationalArea(model);
if (isHiveAlreadyConnected)
{
await _messageBus.Publish(new HiveReconnectedMessage
{
HiveID = model.HiveID,
Hive = model,
InitialOperationalArea = operationalArea,
IsSuccessfullyReconnected = result
});
}
else
{
await _messageBus.Publish(new HiveConnectedMessage
{
HiveID = model.HiveID,
@ -78,6 +98,7 @@ namespace DevOpsProject.CommunicationControl.Logic.Services
InitialOperationalArea = operationalArea,
IsSuccessfullyConnected = result
});
}
return operationalArea;
}
else

View File

@ -30,13 +30,6 @@ namespace DevOpsProject.CommunicationControl.API.Controllers
HiveSchema = request.HiveSchema
};
bool isConnected = await _communicationControlService.IsHiveConnected(request.HiveID);
if (isConnected)
{
_logger.LogError("Hive with HiveID: {hiveId} already connected. Request: {@request}", request.HiveID, request);
return BadRequest($"Hive with HiveID: {request.HiveID} already connected");
}
var hiveOperationalArea = await _communicationControlService.ConnectHive(hiveModel);
var connectResponse = new HiveConnectResponse
{