Skip to content

Commit

Permalink
Use robot specific mission run queues
Browse files Browse the repository at this point in the history
  • Loading branch information
aeshub authored and oysand committed Aug 25, 2023
1 parent 5a704b7 commit 138f66e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
35 changes: 35 additions & 0 deletions backend/api.test/EventHandlers/TestMissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,41 @@ public async void NoMissionIsStartedIfQueueIsEmptyWhenRobotBecomesAvailable()
Assert.False(ongoingMission);
}

[Fact]
public async void MissionRunIsStartedForOtherAvailableRobotIfOneRobotHasAnOngoingMissionRun()
{
// Arrange
var robotOne = await NewRobot(RobotStatus.Available);
var robotTwo = await NewRobot(RobotStatus.Available);
await _robotService.Create(robotOne);
await _robotService.Create(robotTwo);

var missionRunOne = ScheduledMission;
var missionRunTwo = ScheduledMission;

missionRunOne.Robot = robotOne;
missionRunTwo.Robot = robotTwo;

SetupMocksForRobotController(robotOne, missionRunOne);

// Act (Ensure first mission is started)
await _missionRunService.Create(missionRunOne);

// Assert
var postStartMissionRunOne = await _missionRunService.ReadById(missionRunOne.Id);
Assert.Equal(MissionStatus.Ongoing, postStartMissionRunOne.Status);

// Rearrange
SetupMocksForRobotController(robotTwo, missionRunTwo);

// Act (Ensure second mission is started for second robot)
await _missionRunService.Create(missionRunTwo);

// Assert
var postStartMissionTunTwo = await _missionRunService.ReadById(missionRunTwo.Id);
Assert.Equal(MissionStatus.Ongoing, postStartMissionTunTwo.Status);
}

private void SetupMocksForRobotController(Robot robot, MissionRun missionRun)
{
_robotControllerMock.IsarServiceMock
Expand Down
48 changes: 26 additions & 22 deletions backend/api/EventHandlers/MissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,31 @@ IServiceScopeFactory scopeFactory
Subscribe();
}

private IList<MissionRun> MissionRunQueue =>
MissionService
private IMissionRunService MissionService =>
_scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IMissionRunService>();

private IRobotService RobotService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IRobotService>();

private RobotController RobotController =>
_scopeFactory.CreateScope().ServiceProvider.GetRequiredService<RobotController>();

private IList<MissionRun> MissionRunQueue(string robotId)
{
return MissionService
.ReadAll(
new MissionRunQueryStringParameters
{
Statuses = new List<MissionStatus>
{
MissionStatus.Pending
},
RobotId = robotId,
OrderBy = "DesiredStartTime",
PageSize = 100
}
)
.Result;

private IMissionRunService MissionService =>
_scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IMissionRunService>();

private IRobotService RobotService => _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IRobotService>();

private RobotController RobotController =>
_scopeFactory.CreateScope().ServiceProvider.GetRequiredService<RobotController>();
}

public override void Subscribe()
{
Expand All @@ -70,12 +73,6 @@ private void OnMissionRunCreated(object? sender, MissionRunCreatedEventArgs e)
{
_logger.LogInformation("Triggered MissionRunCreated event for mission run ID: {MissionRunId}", e.MissionRunId);

if (MissionRunQueueIsEmpty())
{
_logger.LogInformation("Mission run {MissionRunId} was not started as there are no mission runs on the queue", e.MissionRunId);
return;
}

var missionRun = MissionService.ReadById(e.MissionRunId).Result;

if (missionRun == null)
Expand All @@ -84,6 +81,12 @@ private void OnMissionRunCreated(object? sender, MissionRunCreatedEventArgs e)
return;
}

if (MissionRunQueueIsEmpty(MissionRunQueue(missionRun.Robot.Id)))
{
_logger.LogInformation("Mission run {MissionRunId} was not started as there are no mission runs on the queue", e.MissionRunId);
return;
}

_scheduleMissionMutex.WaitOne();
StartMissionRunIfSystemIsAvailable(missionRun);
_scheduleMissionMutex.ReleaseMutex();
Expand All @@ -99,13 +102,13 @@ private async void OnRobotAvailable(object? sender, RobotAvailableEventArgs e)
return;
}

if (MissionRunQueueIsEmpty())
if (MissionRunQueueIsEmpty(MissionRunQueue(robot.Id)))
{
_logger.LogInformation("The robot was changed to available but there are no mission runs in the queue to be scheduled");
return;
}

var missionRun = MissionRunQueue.First(missionRun => missionRun.Robot.Id == robot.Id);
var missionRun = MissionRunQueue(robot.Id).First(missionRun => missionRun.Robot.Id == robot.Id);

_scheduleMissionMutex.WaitOne();
StartMissionRunIfSystemIsAvailable(missionRun);
Expand Down Expand Up @@ -139,14 +142,14 @@ private void StartMissionRunIfSystemIsAvailable(MissionRun missionRun)
}
}

private bool MissionRunQueueIsEmpty()
private static bool MissionRunQueueIsEmpty(IList<MissionRun> missionRunQueue)
{
return !MissionRunQueue.Any();
return !missionRunQueue.Any();
}

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

if (ongoingMission)
{
Expand All @@ -171,7 +174,7 @@ private async Task<bool> TheSystemIsAvailableToRunAMission(Robot robot, MissionR
return true;
}

private async Task<bool> OngoingMission()
private async Task<bool> OngoingMission(string robotId)
{
var ongoingMissions = await MissionService.ReadAll(
new MissionRunQueryStringParameters
Expand All @@ -180,6 +183,7 @@ private async Task<bool> OngoingMission()
{
MissionStatus.Ongoing
},
RobotId = robotId,
OrderBy = "DesiredStartTime",
PageSize = 100
});
Expand Down

0 comments on commit 138f66e

Please sign in to comment.