From 6c663f484fa24f7f3210cc8c59af7a435775d51b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Chirico=20Indreb=C3=B8?= Date: Mon, 16 Dec 2024 17:15:54 +0100 Subject: [PATCH] Handle missions being scheduled at the same time --- backend/api/EventHandlers/MqttEventHandler.cs | 9 +++++---- backend/api/Services/IsarService.cs | 3 +++ backend/api/Services/MissionSchedulingService.cs | 5 ++++- backend/api/Utilities/Exceptions.cs | 4 ++++ 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/backend/api/EventHandlers/MqttEventHandler.cs b/backend/api/EventHandlers/MqttEventHandler.cs index cbfe42986..6974167fe 100644 --- a/backend/api/EventHandlers/MqttEventHandler.cs +++ b/backend/api/EventHandlers/MqttEventHandler.cs @@ -100,15 +100,16 @@ private async void OnIsarStatus(object? sender, MqttReceivedArgs mqttArgs) _updateRobotSemaphore.WaitOne(); _logger.LogDebug("Semaphore acquired for updating robot status"); - var updatedRobot = await RobotService.UpdateRobotStatus(robot.Id, isarStatus.Status); + await RobotService.UpdateRobotStatus(robot.Id, isarStatus.Status); + robot.Status = isarStatus.Status; _updateRobotSemaphore.Release(); _logger.LogDebug("Semaphore released after updating robot status"); - _logger.LogInformation("Updated status for robot {Name} to {Status}", updatedRobot.Name, updatedRobot.Status); + _logger.LogInformation("Updated status for robot {Name} to {Status}", robot.Name, robot.Status); - _logger.LogInformation("OnIsarStatus: Robot {robotName} has status {robotStatus} and current inspection area {areaName}", updatedRobot.Name, updatedRobot.Status, updatedRobot.CurrentInspectionArea?.Name); + _logger.LogInformation("OnIsarStatus: Robot {robotName} has status {robotStatus} and current inspection area {areaName}", robot.Name, robot.Status, robot.CurrentInspectionArea?.Name); if (isarStatus.Status == RobotStatus.Available) { @@ -118,6 +119,7 @@ private async void OnIsarStatus(object? sender, MqttReceivedArgs mqttArgs) _logger.LogDebug("Semaphore acquired for updating robot current mission id"); await RobotService.UpdateCurrentMissionId(robot.Id, null); + await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot); } catch (RobotNotFoundException) { @@ -129,7 +131,6 @@ private async void OnIsarStatus(object? sender, MqttReceivedArgs mqttArgs) _updateRobotSemaphore.Release(); _logger.LogDebug("Semaphore released after updating robot current mission id"); } - await MissionScheduling.StartNextMissionRunIfSystemIsAvailable(robot); } } diff --git a/backend/api/Services/IsarService.cs b/backend/api/Services/IsarService.cs index 37caef005..039592144 100644 --- a/backend/api/Services/IsarService.cs +++ b/backend/api/Services/IsarService.cs @@ -57,6 +57,9 @@ public async Task StartMission(Robot robot, MissionRun missionRun) if (!response.IsSuccessStatusCode) { + if (response.StatusCode == HttpStatusCode.Conflict) + throw new RobotBusyException("Robot was not available when starting mission"); + (string message, int statusCode) = GetErrorDescriptionFoFailedIsarRequest(response); string errorResponse = await response.Content.ReadAsStringAsync(); logger.LogError("{Message}: {ErrorResponse}", message, errorResponse); diff --git a/backend/api/Services/MissionSchedulingService.cs b/backend/api/Services/MissionSchedulingService.cs index cf1546dfe..181703803 100644 --- a/backend/api/Services/MissionSchedulingService.cs +++ b/backend/api/Services/MissionSchedulingService.cs @@ -114,6 +114,9 @@ or MissionRunNotFoundException ); await missionRunService.SetMissionRunToFailed(missionRun.Id, $"Mission run '{missionRun.Id}' was not started successfully. '{ex.Message}'"); } + catch (RobotBusyException) + { + } } public async Task HandleBatteryAndPressureLevel(Robot robot) @@ -388,7 +391,7 @@ private async Task StartMissionRun(MissionRun queuedMissionRun) { string errorMessage = $"Robot {robotId} has status {robot.Status} and is not available"; logger.LogError("{Message}", errorMessage); - throw new RobotNotAvailableException(errorMessage); + throw new RobotBusyException(errorMessage); } if (robot.Deprecated) diff --git a/backend/api/Utilities/Exceptions.cs b/backend/api/Utilities/Exceptions.cs index df3b5a085..1d929616f 100644 --- a/backend/api/Utilities/Exceptions.cs +++ b/backend/api/Utilities/Exceptions.cs @@ -91,6 +91,10 @@ public class RobotNotAvailableException(string message) : Exception(message) { } + public class RobotBusyException(string message) : Exception(message) + { + } + public class RobotNotInSameInstallationAsMissionException(string message) : Exception(message) { }