From 0ea15b6b3bf909f4676f8ccad3fadab33de3b9a7 Mon Sep 17 00:00:00 2001 From: Enkidu93 Date: Mon, 11 Sep 2023 09:14:57 -0400 Subject: [PATCH] Fixes Throw error for unbuilt engine #81 --ECL --- .../Services/SmtTransferEngineService.cs | 12 ++---------- .../Services/TranslationEngineServiceBase.cs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/SIL.Machine.AspNetCore/Services/SmtTransferEngineService.cs b/src/SIL.Machine.AspNetCore/Services/SmtTransferEngineService.cs index e7619bc63..2cfccdc7e 100644 --- a/src/SIL.Machine.AspNetCore/Services/SmtTransferEngineService.cs +++ b/src/SIL.Machine.AspNetCore/Services/SmtTransferEngineService.cs @@ -70,7 +70,7 @@ public override async Task> TranslateAsync( IDistributedReaderWriterLock @lock = await LockFactory.CreateAsync(engineId, cancellationToken); await using (await @lock.ReaderLockAsync(cancellationToken: cancellationToken)) { - TranslationEngine engine = await GetEngineAsync(engineId, cancellationToken); + TranslationEngine engine = await GetBuiltEngineAsync(engineId, cancellationToken); HybridTranslationEngine hybridEngine = await state.GetHybridEngineAsync(engine.BuildRevision); IReadOnlyList results = await hybridEngine.TranslateAsync(n, segment, cancellationToken); state.LastUsedTime = DateTime.Now; @@ -88,7 +88,7 @@ public override async Task GetWordGraphAsync( IDistributedReaderWriterLock @lock = await LockFactory.CreateAsync(engineId, cancellationToken); await using (await @lock.ReaderLockAsync(cancellationToken: cancellationToken)) { - TranslationEngine engine = await GetEngineAsync(engineId, cancellationToken); + TranslationEngine engine = await GetBuiltEngineAsync(engineId, cancellationToken); HybridTranslationEngine hybridEngine = await state.GetHybridEngineAsync(engine.BuildRevision); WordGraph result = await hybridEngine.GetWordGraphAsync(segment, cancellationToken); state.LastUsedTime = DateTime.Now; @@ -170,12 +170,4 @@ IReadOnlyList corpora // Token "None" is used here because hangfire injects the proper cancellation token return r => r.RunAsync(engineId, buildId, corpora, CancellationToken.None); } - - private async Task GetEngineAsync(string engineId, CancellationToken cancellationToken) - { - TranslationEngine? engine = await Engines.GetAsync(e => e.EngineId == engineId, cancellationToken); - if (engine is null) - throw new InvalidOperationException(""); - return engine; - } } diff --git a/src/SIL.Machine.AspNetCore/Services/TranslationEngineServiceBase.cs b/src/SIL.Machine.AspNetCore/Services/TranslationEngineServiceBase.cs index 0f3d710af..dc702f8de 100644 --- a/src/SIL.Machine.AspNetCore/Services/TranslationEngineServiceBase.cs +++ b/src/SIL.Machine.AspNetCore/Services/TranslationEngineServiceBase.cs @@ -209,4 +209,20 @@ CancellationToken cancellationToken } return false; } + + protected async Task GetEngineAsync(string engineId, CancellationToken cancellationToken) + { + TranslationEngine? engine = await Engines.GetAsync(e => e.EngineId == engineId, cancellationToken); + if (engine is null) + throw new InvalidOperationException(""); + return engine; + } + + protected async Task GetBuiltEngineAsync(string engineId, CancellationToken cancellationToken) + { + TranslationEngine engine = await GetEngineAsync(engineId, cancellationToken); + if (engine.BuildState != BuildState.None || engine.BuildRevision == 0) //TranslationEngine.Revision - I don't see it used anywhere + throw new RpcException(new Status(StatusCode.FailedPrecondition, "The engine must be built first")); + return engine; + } }