Skip to content

Commit

Permalink
Cancel build gRPC endpoint returns Aborted when no build is running (#…
Browse files Browse the repository at this point in the history
…165)

- fixes #162
  • Loading branch information
ddaspit authored Feb 6, 2024
1 parent 628a5ba commit 8fd1a81
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public async Task CancelBuildAsync(string engineId, CancellationToken cancellati
IDistributedReaderWriterLock @lock = await _lockFactory.CreateAsync(engineId, cancellationToken);
await using (await @lock.WriterLockAsync(cancellationToken: cancellationToken))
{
await CancelBuildJobAsync(engineId, cancellationToken);
if (!await CancelBuildJobAsync(engineId, cancellationToken))
throw new InvalidOperationException("The engine is not currently building.");
}
}

Expand Down Expand Up @@ -148,13 +149,14 @@ public bool IsLanguageNativeToModel(string language, out string internalCode)
return _languageTagService.ConvertToFlores200Code(language, out internalCode);
}

private async Task CancelBuildJobAsync(string engineId, CancellationToken cancellationToken)
private async Task<bool> CancelBuildJobAsync(string engineId, CancellationToken cancellationToken)
{
(string? buildId, BuildJobState jobState) = await _buildJobService.CancelBuildJobAsync(
engineId,
cancellationToken
);
if (buildId is not null && jobState is BuildJobState.None)
await _platformService.BuildCanceledAsync(buildId, CancellationToken.None);
return buildId is not null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override async Task<TranslateResponse> Translate(TranslateRequest request
}
catch (EngineNotBuiltException e)
{
throw new RpcException(new Status(StatusCode.Aborted, e.Message));
throw new RpcException(new Status(StatusCode.Aborted, e.Message, e));
}

return new TranslateResponse { Results = { results.Select(Map) } };
Expand All @@ -73,7 +73,7 @@ ServerCallContext context
}
catch (EngineNotBuiltException e)
{
throw new RpcException(new Status(StatusCode.Aborted, e.Message));
throw new RpcException(new Status(StatusCode.Aborted, e.Message, e));
}
return new GetWordGraphResponse { WordGraph = Map(wordGraph) };
}
Expand Down Expand Up @@ -107,15 +107,22 @@ await engineService.StartBuildAsync(
}
catch (InvalidOperationException e)
{
throw new RpcException(new Status(StatusCode.Aborted, e.Message));
throw new RpcException(new Status(StatusCode.Aborted, e.Message, e));
}
return Empty;
}

public override async Task<Empty> CancelBuild(CancelBuildRequest request, ServerCallContext context)
{
ITranslationEngineService engineService = GetEngineService(request.EngineType);
await engineService.CancelBuildAsync(request.EngineId, context.CancellationToken);
try
{
await engineService.CancelBuildAsync(request.EngineId, context.CancellationToken);
}
catch (InvalidOperationException e)
{
throw new RpcException(new Status(StatusCode.Aborted, e.Message, e));
}
return Empty;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ public async Task CancelBuildAsync(string engineId, CancellationToken cancellati
IDistributedReaderWriterLock @lock = await _lockFactory.CreateAsync(engineId, cancellationToken);
await using (await @lock.WriterLockAsync(cancellationToken: cancellationToken))
{
await CancelBuildJobAsync(engineId, cancellationToken);
if (!await CancelBuildJobAsync(engineId, cancellationToken))
throw new InvalidOperationException("The engine is not currently building.");
SmtTransferEngineState state = _stateService.Get(engineId);
state.LastUsedTime = DateTime.UtcNow;
}
Expand All @@ -215,14 +216,15 @@ public bool IsLanguageNativeToModel(string language, out string internalCode)
throw new NotSupportedException("SMT transfer engines do not support language info.");
}

private async Task CancelBuildJobAsync(string engineId, CancellationToken cancellationToken)
private async Task<bool> CancelBuildJobAsync(string engineId, CancellationToken cancellationToken)
{
(string? buildId, BuildJobState jobState) = await _buildJobService.CancelBuildJobAsync(
engineId,
cancellationToken
);
if (buildId is not null && jobState is BuildJobState.None)
await _platformService.BuildCanceledAsync(buildId, CancellationToken.None);
return buildId is not null;
}

private async Task<TranslationEngine> GetEngineAsync(string engineId, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task StartBuildAsync()
}

[Test]
public async Task CancelBuildAsync()
public async Task CancelBuildAsync_Building()
{
using var env = new TestEnvironment();

Expand Down Expand Up @@ -47,6 +47,13 @@ public async Task CancelBuildAsync()
Assert.That(engine.BuildRevision, Is.EqualTo(1));
}

[Test]
public void CancelBuildAsync_NotBuilding()
{
using var env = new TestEnvironment();
Assert.ThrowsAsync<InvalidOperationException>(() => env.Service.CancelBuildAsync("engine1"));
}

[Test]
public async Task DeleteAsync_WhileBuilding()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ await env
}

[Test]
public async Task CancelBuildAsync()
public async Task CancelBuildAsync_Building()
{
using var env = new TestEnvironment();
await env.SmtBatchTrainer.TrainAsync(
Expand All @@ -73,6 +73,13 @@ await env.SmtBatchTrainer.TrainAsync(
Assert.That(engine.CurrentBuild, Is.Null);
}

[Test]
public void CancelBuildAsync_NotBuilding()
{
using var env = new TestEnvironment();
Assert.ThrowsAsync<InvalidOperationException>(() => env.Service.CancelBuildAsync("engine1"));
}

[Test]
public async Task StartBuildAsync_RestartUnfinishedBuild()
{
Expand Down

0 comments on commit 8fd1a81

Please sign in to comment.