From 4390a523c7ef85c160a4fa6c5375979891825b5d Mon Sep 17 00:00:00 2001 From: dsokolovrudakov Date: Wed, 12 Mar 2025 21:42:55 +0200 Subject: [PATCH 1/2] Fix Hive Mind connect logic - allow to reconnect gracefully --- .../Services/CommunicationControlService.cs | 37 +++++++++++++++---- .../Controllers/HiveController.cs | 7 ---- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/CommunicationControl/DevOpsProject.CommunicationControl.Logic/Services/CommunicationControlService.cs b/src/CommunicationControl/DevOpsProject.CommunicationControl.Logic/Services/CommunicationControlService.cs index 5e0b449..68ea12f 100644 --- a/src/CommunicationControl/DevOpsProject.CommunicationControl.Logic/Services/CommunicationControlService.cs +++ b/src/CommunicationControl/DevOpsProject.CommunicationControl.Logic/Services/CommunicationControlService.cs @@ -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; @@ -65,19 +65,40 @@ namespace DevOpsProject.CommunicationControl.Logic.Services public async Task ConnectHive(HiveModel model) { - _logger.LogInformation("Trying to connect Hive: {@model}", 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); - await _messageBus.Publish(new HiveConnectedMessage + if (isHiveAlreadyConnected) { - HiveID = model.HiveID, - Hive = model, - InitialOperationalArea = operationalArea, - IsSuccessfullyConnected = result - }); + await _messageBus.Publish(new HiveReconnectedMessage + { + HiveID = model.HiveID, + Hive = model, + InitialOperationalArea = operationalArea, + IsSuccessfullyReconnected = result + }); + } + else + { + await _messageBus.Publish(new HiveConnectedMessage + { + HiveID = model.HiveID, + Hive = model, + InitialOperationalArea = operationalArea, + IsSuccessfullyConnected = result + }); + } return operationalArea; } else diff --git a/src/CommunicationControl/DevOpsProject/Controllers/HiveController.cs b/src/CommunicationControl/DevOpsProject/Controllers/HiveController.cs index 8c3bf9c..62a284e 100644 --- a/src/CommunicationControl/DevOpsProject/Controllers/HiveController.cs +++ b/src/CommunicationControl/DevOpsProject/Controllers/HiveController.cs @@ -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 { From b0bdf686d2812c43faf1719cae362165a2a3d603 Mon Sep 17 00:00:00 2001 From: dsokolovrudakov Date: Wed, 12 Mar 2025 21:45:40 +0200 Subject: [PATCH 2/2] Add HiveReconnectedMessage --- .../Messages/HiveReconnectedMessage.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/CommunicationControl/DevOpsProject.Shared/Messages/HiveReconnectedMessage.cs diff --git a/src/CommunicationControl/DevOpsProject.Shared/Messages/HiveReconnectedMessage.cs b/src/CommunicationControl/DevOpsProject.Shared/Messages/HiveReconnectedMessage.cs new file mode 100644 index 0000000..a19d542 --- /dev/null +++ b/src/CommunicationControl/DevOpsProject.Shared/Messages/HiveReconnectedMessage.cs @@ -0,0 +1,16 @@ +using DevOpsProject.Shared.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DevOpsProject.Shared.Messages +{ + public class HiveReconnectedMessage : BaseMessage + { + public bool IsSuccessfullyReconnected { get; set; } + public HiveModel Hive { get; set; } + public HiveOperationalArea InitialOperationalArea { get; set; } + } +}