Skip to content

Commit

Permalink
More logging and better error handling for download endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
johnml1135 committed Feb 21, 2024
1 parent 525950f commit 6690e54
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ public static IMachineBuilder AddServalTranslationEngineService(
{
options.Interceptors.Add<CancellationInterceptor>();
options.Interceptors.Add<UnimplementedInterceptor>();
options.Interceptors.Add<InvalidOperationInterceptor>();
});
builder.AddServalPlatformService(connectionString);
engineTypes ??=
Expand Down
20 changes: 20 additions & 0 deletions src/SIL.Machine.AspNetCore/Services/InvalidOperationInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace SIL.Machine.AspNetCore.Services;

public class InvalidOperationInterceptor : Interceptor
{
public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
TRequest request,
ServerCallContext context,
UnaryServerMethod<TRequest, TResponse> continuation
)
{
try
{
return await continuation(request, context);
}
catch (InvalidOperationException)
{
throw new RpcException(new Status(StatusCode.InvalidArgument, "This operation is not valid at this time."));
}
}
}
51 changes: 42 additions & 9 deletions src/SIL.Machine.AspNetCore/Services/NmtEngineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class NmtEngineService(
IBuildJobService buildJobService,
ILanguageTagService languageTagService,
ClearMLMonitorService clearMLMonitorService,
ISharedFileService sharedFileService
ISharedFileService sharedFileService,
ILogger<NmtEngineService> logger
) : ITranslationEngineService
{
private readonly IDistributedReaderWriterLockFactory _lockFactory = lockFactory;
Expand All @@ -26,6 +27,7 @@ ISharedFileService sharedFileService
private readonly ClearMLMonitorService _clearMLMonitorService = clearMLMonitorService;
private readonly ILanguageTagService _languageTagService = languageTagService;
private readonly ISharedFileService _sharedFileService = sharedFileService;
private readonly ILogger<NmtEngineService> _logger = logger;

public const string ModelDirectory = "models/";

Expand Down Expand Up @@ -126,6 +128,7 @@ public async Task<ModelDownloadUrl> GetModelDownloadUrlAsync(
CancellationToken cancellationToken = default
)
{
_logger.LogInformation("Getting model download URL for engine {engineId}.", engineId);
TranslationEngine engine = await GetEngineAsync(engineId, cancellationToken);
if (engine.IsModelPersisted != true)
throw new NotSupportedException(
Expand All @@ -137,17 +140,47 @@ public async Task<ModelDownloadUrl> GetModelDownloadUrlAsync(
string filepath = GetModelPath(engineId, engine.BuildRevision);
bool fileExists = await _sharedFileService.ExistsAsync(filepath, cancellationToken);
if (!fileExists)
throw new FileNotFoundException(
$"The model should exist to be downloaded but is not there for BuildRevision {engine.BuildRevision}."
{
_logger.LogError(
"The model should exist to be downloaded but is not there for BuildRevision {engineBuildRevision}.",
engine.BuildRevision
);
throw new RpcException(
new Status(
StatusCode.DataLoss,
$"The model should exist to be downloaded but is not there for BuildRevision {engine.BuildRevision}."
)
);
}
var expiresAt = DateTime.UtcNow.AddMinutes(MinutesToExpire);
var modelInfo = new ModelDownloadUrl
_logger.LogInformation(
"Model download URL will expire at {expiresAt} for engine {engineId} and revision {engineBuildRevision}.",
expiresAt,
engineId,
engine.BuildRevision
);
try
{
Url = await _sharedFileService.GetDownloadUrlAsync(filepath, expiresAt),
ModelRevision = engine.BuildRevision,
ExipiresAt = expiresAt
};
return modelInfo;
var modelInfo = new ModelDownloadUrl
{
Url = await _sharedFileService.GetDownloadUrlAsync(filepath, expiresAt),
ModelRevision = engine.BuildRevision,
ExipiresAt = expiresAt
};
_logger.LogInformation(
"Model download URL: {modelInfoUrl} for engine {engineId} and revision {engineBuildRevision}.",
modelInfo.Url,
engineId,
engine.BuildRevision
);
return modelInfo;
}
catch (Exception e)
{
throw new RpcException(
new Status(StatusCode.Internal, "Error occurred while getting the model download URL.", e)
);
}
}

public Task<IReadOnlyList<TranslationResult>> TranslateAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ private NmtEngineService CreateService()
BuildJobService,
new LanguageTagService(),
ClearMLMonitorService,
SharedFileService
SharedFileService,
Substitute.For<ILogger<NmtEngineService>>()
);
}

Expand Down

0 comments on commit 6690e54

Please sign in to comment.