Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Throw error for unbuilt engine #81 #82

Merged
merged 6 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.Aborted, "The engine must be built first"));
return engine;
}
}
Loading