Skip to content

Commit

Permalink
Fixes Throw error for unbuilt engine #81
Browse files Browse the repository at this point in the history
--ECL
  • Loading branch information
Enkidu93 committed Sep 11, 2023
1 parent da821a7 commit 0ea15b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/SIL.Machine.AspNetCore/Services/SmtTransferEngineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override async Task<IReadOnlyList<TranslationResult>> 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<TranslationResult> results = await hybridEngine.TranslateAsync(n, segment, cancellationToken);
state.LastUsedTime = DateTime.Now;
Expand All @@ -88,7 +88,7 @@ public override async Task<WordGraph> 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;
Expand Down Expand Up @@ -170,12 +170,4 @@ IReadOnlyList<Corpus> 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<TranslationEngine> GetEngineAsync(string engineId, CancellationToken cancellationToken)
{
TranslationEngine? engine = await Engines.GetAsync(e => e.EngineId == engineId, cancellationToken);
if (engine is null)
throw new InvalidOperationException("");
return engine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,20 @@ CancellationToken cancellationToken
}
return false;
}

protected async Task<TranslationEngine> 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<TranslationEngine> 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;
}
}

0 comments on commit 0ea15b6

Please sign in to comment.