-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix "get task by name" from ClearML * Use same sub for cancellation as from SMT - and refractor * Fix tests
- Loading branch information
1 parent
b8e50ca
commit dc2dade
Showing
8 changed files
with
81 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Machine/src/SIL.Machine.AspNetCore/Services/SubscribeForCancellation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
namespace SIL.Machine.AspNetCore.Services; | ||
|
||
public class SubscribeForCancellation | ||
{ | ||
private readonly IRepository<TranslationEngine> _engines; | ||
|
||
public SubscribeForCancellation(IRepository<TranslationEngine> engines) | ||
{ | ||
_engines = engines; | ||
} | ||
|
||
public CancellationToken GetCombinedCancellationToken( | ||
string engineId, | ||
string buildId, | ||
CancellationToken externalCancellationToken | ||
) | ||
{ | ||
CancellationTokenSource cts = new(); | ||
SubscribeForCancellationAsync(cts, engineId, buildId); | ||
CancellationTokenSource combinedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource( | ||
externalCancellationToken, | ||
cts.Token | ||
); | ||
return combinedCancellationSource.Token; | ||
} | ||
|
||
private async void SubscribeForCancellationAsync(CancellationTokenSource cts, string engineId, string buildId) | ||
{ | ||
var cancellationToken = cts.Token; | ||
ISubscription<TranslationEngine> sub = await _engines.SubscribeAsync( | ||
e => e.EngineId == engineId && e.BuildId == buildId | ||
); | ||
if (sub.Change.Entity is null) | ||
return; | ||
while (true) | ||
{ | ||
await sub.WaitForChangeAsync(TimeSpan.FromSeconds(10), cancellationToken); | ||
TranslationEngine? engine = sub.Change.Entity; | ||
if (engine is null || engine.IsCanceled) | ||
{ | ||
cts.Cancel(); | ||
return; | ||
} | ||
if (cancellationToken.IsCancellationRequested) | ||
return; | ||
Thread.Sleep(500); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters