Skip to content

Commit

Permalink
Handle missions being scheduled at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Dec 16, 2024
1 parent bf441a9 commit f2d9d45
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
9 changes: 5 additions & 4 deletions backend/api/EventHandlers/MqttEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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);
}
}

Expand Down
3 changes: 3 additions & 0 deletions backend/api/Services/IsarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public async Task<IsarMission> 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);
Expand Down
5 changes: 4 additions & 1 deletion backend/api/Services/MissionSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MissionRun?> HandleBatteryAndPressureLevel(Robot robot)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions backend/api/Utilities/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
Expand Down

0 comments on commit f2d9d45

Please sign in to comment.