Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mission starting stability #1889

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 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 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
74 changes: 11 additions & 63 deletions backend/api/Services/MissionSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public async Task StartNextMissionRunIfSystemIsAvailable(Robot robot)
logger.LogInformation("Robot {robotName} has status {robotStatus} and current area {areaName}", robot.Name, robot.Status, robot.CurrentInspectionArea?.Name);

MissionRun? missionRun;
try { missionRun = await SelectNextMissionRun(robot.Id); }
try { missionRun = await SelectNextMissionRun(robot); }
catch (RobotNotFoundException)
{
logger.LogError("Robot with ID: {RobotId} was not found in the database", robot.Id);
return;
}

if (robot.MissionQueueFrozen && missionRun != null && !missionRun.IsEmergencyMission())
if (robot.MissionQueueFrozen && missionRun != null && !(missionRun.IsEmergencyMission() || missionRun.IsReturnHomeMission()))
{
logger.LogInformation("Robot {robotName} was ready to start a mission but its mission queue was frozen", robot.Name);
return;
Expand All @@ -68,7 +68,7 @@ public async Task StartNextMissionRunIfSystemIsAvailable(Robot robot)

if (missionRun == null) { return; }

if (!await TheSystemIsAvailableToRunAMission(robot.Id, missionRun.Id))
if (!TheSystemIsAvailableToRunAMission(robot, missionRun))
{
logger.LogInformation("Mission {MissionRunId} was put on the queue as the system may not start a mission now", missionRun.Id);
return;
Expand Down Expand Up @@ -98,7 +98,7 @@ public async Task StartNextMissionRunIfSystemIsAvailable(Robot robot)
if (missionRun == null) { return; }
}

try { await StartMissionRun(missionRun); }
try { await StartMissionRun(missionRun, robot); }
catch (Exception ex) when (
ex is MissionException
or RobotNotFoundException
Expand All @@ -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 @@ -308,16 +311,8 @@ public void TriggerRobotAvailable(RobotAvailableEventArgs e)
OnRobotAvailable(e);
}

private async Task<MissionRun?> SelectNextMissionRun(string robotId)
private async Task<MissionRun?> SelectNextMissionRun(Robot robot)
{
var robot = await robotService.ReadById(robotId, readOnly: true);
if (robot == null)
{
string errorMessage = $"Could not find robot with id {robotId}";
logger.LogError("{Message}", errorMessage);
throw new RobotNotFoundException(errorMessage);
}

var missionRun = await missionRunService.ReadNextScheduledEmergencyMissionRun(robot.Id, readOnly: true);
if (robot.MissionQueueFrozen == false && missionRun == null) { missionRun = await missionRunService.ReadNextScheduledMissionRun(robot.Id, readOnly: true); }
return missionRun;
Expand Down Expand Up @@ -371,33 +366,10 @@ private async Task MoveInterruptedMissionsToQueue(IEnumerable<string> interrupte
}
}

private async Task StartMissionRun(MissionRun queuedMissionRun)
private async Task StartMissionRun(MissionRun queuedMissionRun, Robot robot)
{
string robotId = queuedMissionRun.Robot.Id;
string missionRunId = queuedMissionRun.Id;

var robot = await robotService.ReadById(robotId, readOnly: true);
if (robot == null)
{
string errorMessage = $"Could not find robot with id {robotId}";
logger.LogError("{Message}", errorMessage);
throw new RobotNotFoundException(errorMessage);
}

if (robot.Status is not RobotStatus.Available)
{
string errorMessage = $"Robot {robotId} has status {robot.Status} and is not available";
logger.LogError("{Message}", errorMessage);
throw new RobotNotAvailableException(errorMessage);
}

if (robot.Deprecated)
{
string errorMessage = $"Robot {robotId} is deprecated and cannot start mission";
logger.LogError("{Message}", errorMessage);
throw new RobotNotAvailableException(errorMessage);
}

var missionRun = await missionRunService.ReadById(missionRunId, readOnly: true);
if (missionRun == null)
{
Expand Down Expand Up @@ -452,33 +424,9 @@ private async Task StartMissionRun(MissionRun queuedMissionRun)
return ongoingMissions;
}

private async Task<bool> TheSystemIsAvailableToRunAMission(string robotId, string missionRunId)
private bool TheSystemIsAvailableToRunAMission(Robot robot, MissionRun missionRun)
{
bool ongoingMission = await OngoingMission(robotId);

if (ongoingMission)
{
logger.LogInformation("Mission run {MissionRunId} was not started as there is already an ongoing mission", missionRunId);
return false;
}

var robot = await robotService.ReadById(robotId, readOnly: true);
if (robot is null)
{
string errorMessage = $"Robot with ID: {robotId} was not found in the database";
logger.LogError("{Message}", errorMessage);
throw new RobotNotFoundException(errorMessage);
}

var missionRun = await missionRunService.ReadById(missionRunId, readOnly: true);
if (missionRun is null)
{
string errorMessage = $"Mission run with Id {missionRunId} was not found in the database";
logger.LogError("{Message}", errorMessage);
throw new MissionRunNotFoundException(errorMessage);
}

if (robot.MissionQueueFrozen && missionRun.MissionRunType != MissionRunType.Emergency)
if (robot.MissionQueueFrozen && !(missionRun.IsEmergencyMission() || missionRun.IsReturnHomeMission()))
{
logger.LogInformation("Mission run {MissionRunId} was not started as the mission run queue for robot {RobotName} is frozen", missionRun.Id, robot.Name);
return false;
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
Loading