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
Changes from 1 commit
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
Next Next commit
Improve mission starting stability
  • Loading branch information
andchiind committed Dec 17, 2024
commit 4f0f8db83649b5e6fc97401f3017b6a07288a574
67 changes: 6 additions & 61 deletions backend/api/Services/MissionSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ 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);
Expand Down Expand Up @@ -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))
oysand marked this conversation as resolved.
Show resolved Hide resolved
{
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 Down Expand Up @@ -308,16 +308,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 +363,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,32 +421,8 @@ 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);
oysand marked this conversation as resolved.
Show resolved Hide resolved
oysand marked this conversation as resolved.
Show resolved Hide resolved

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)
andchiind marked this conversation as resolved.
Show resolved Hide resolved
{
logger.LogInformation("Mission run {MissionRunId} was not started as the mission run queue for robot {RobotName} is frozen", missionRun.Id, robot.Name);
Expand Down