Skip to content

Commit

Permalink
Move missionwise localisation inside semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Jun 20, 2024
1 parent 09dcdee commit 255792c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
7 changes: 5 additions & 2 deletions backend/api/EventHandlers/MissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,17 @@ private async void OnMissionRunCreated(object? sender, MissionRunCreatedEventArg

bool isFirstMissionInQueue = false;

_scheduleLocalizationSemaphore.WaitOne();
if (!await LocalizationService.RobotIsLocalized(missionRun.Robot.Id))
{
isFirstMissionInQueue = true;
if (missionRun.Robot.RobotCapabilities != null && !missionRun.Robot.RobotCapabilities.Contains(RobotCapabilitiesEnum.localize))
{
await RobotService.UpdateCurrentArea(missionRun.Robot.Id, missionRun.Area);
_logger.LogInformation("{Message}", $"Set robot with ID {missionRun.Robot.Id} to localised for mission run with ID {missionRun.Id}");
}
else
{
_scheduleLocalizationSemaphore.WaitOne();
if (await MissionService.PendingLocalizationMissionRunExists(missionRun.Robot.Id)
|| await MissionService.OngoingLocalizationMissionRunExists(missionRun.Robot.Id))
{
Expand All @@ -102,6 +103,7 @@ private async void OnMissionRunCreated(object? sender, MissionRunCreatedEventArg
missionRun.Status = MissionStatus.Aborted;
missionRun.StatusReason = "Aborted: Robot was not available";
await MissionService.Update(missionRun);
_scheduleLocalizationSemaphore.Release();
return;
}
catch (Exception ex) when (
Expand All @@ -115,11 +117,12 @@ or IsarCommunicationException
missionRun.Status = MissionStatus.Aborted;
missionRun.StatusReason = "Aborted: Robot was not correctly localized";
await MissionService.Update(missionRun);
_scheduleLocalizationSemaphore.Release();
return;
}
finally { _scheduleLocalizationSemaphore.Release(); }
}
}
_scheduleLocalizationSemaphore.Release();

await CancelReturnToHomeOnNewMissionSchedule(missionRun);

Expand Down
1 change: 1 addition & 0 deletions backend/api/Services/MissionSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public async Task StartNextMissionRunIfSystemIsAvailable(string robotId, bool is
await robotService.UpdateCurrentArea(robot.Id, null);
}
if (missionRun == null) { return; } // The robot is already home
isFirstMissionInQueue = false;
}

if (!await TheSystemIsAvailableToRunAMission(robot.Id, missionRun.Id))
Expand Down

0 comments on commit 255792c

Please sign in to comment.