Skip to content

Commit

Permalink
Move more missionrun db calls to missionrunservice
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Aug 30, 2024
1 parent aff5e71 commit 6c8e887
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
16 changes: 16 additions & 0 deletions backend/api/Services/MissionRunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Api.Database.Context;
using Api.Database.Models;
using Api.Services.Events;
using Api.Services.Models;
using Api.Utilities;
using Microsoft.EntityFrameworkCore;
namespace Api.Services
Expand Down Expand Up @@ -55,6 +56,8 @@ MissionStatus missionStatus

public Task<MissionRun> UpdateMissionRunProperty(string missionRunId, string propertyName, object? value);

public Task<MissionRun> UpdateWithIsarInfo(string missionRunId, IsarMission isarMission);

public Task<MissionRun> SetMissionRunToFailed(string missionRunId, string failureDescription);

public Task UpdateCurrentRobotMissionToFailed(string robotId);
Expand Down Expand Up @@ -642,5 +645,18 @@ public void DetachTracking(MissionRun missionRun)
if (missionRun.Robot != null) robotService.DetachTracking(missionRun.Robot);
context.Entry(missionRun).State = EntityState.Detached;
}

public async Task<MissionRun> UpdateWithIsarInfo(string missionRunId, IsarMission isarMission)
{
var missionRun = await ReadById(missionRunId, readOnly: false) ?? throw new MissionRunNotFoundException($"Could not find mission run with ID {missionRunId}");

missionRun.IsarMissionId = isarMission.IsarMissionId;
foreach (var isarTask in isarMission.Tasks)
{
var task = missionRun.GetTaskByIsarId(isarTask.IsarTaskId);
task?.UpdateWithIsarInfo(isarTask);
}
return await Update(missionRun);
}
}
}
6 changes: 2 additions & 4 deletions backend/api/Services/MissionSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,13 @@ or RobotNotAvailableException
or MissionRunNotFoundException
or IsarCommunicationException)
{
const MissionStatus NewStatus = MissionStatus.Failed;
logger.LogError(
ex,
"Mission run {MissionRunId} was not started successfully due to {ErrorMessage}",
missionRun.Id,
ex.Message
);
await missionRunService.UpdateMissionRunProperty(missionRun.Id, "Status", NewStatus);
await missionRunService.UpdateMissionRunProperty(missionRun.Id, "StatusReason", ex.Message);
await missionRunService.SetMissionRunToFailed(missionRun.Id, "Mission run {MissionRunId} was not started successfully due to {ErrorMessage}");
}
}

Expand Down Expand Up @@ -464,7 +462,7 @@ private async Task StartMissionRun(MissionRun queuedMissionRun)
throw new IsarCommunicationException(ErrorMessage);
}

missionRun.UpdateWithIsarInfo(isarMission);
await missionRunService.UpdateWithIsarInfo(missionRun.Id, isarMission);
await missionRunService.UpdateMissionRunProperty(missionRun.Id, "Status", MissionStatus.Ongoing);

robot.Status = RobotStatus.Busy;
Expand Down

0 comments on commit 6c8e887

Please sign in to comment.