-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Presigned URL code cleaning script IsModelPersisted nullable Return IsModelPersistedState to Serval Check for modelRevision + 1 in cleanup and just delete without keeping internal states. * Reviewer comments * update to most recent api
- Loading branch information
1 parent
32a8471
commit 525950f
Showing
24 changed files
with
437 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace SIL.Machine.AspNetCore.Models | ||
{ | ||
public class ModelDownloadUrl | ||
{ | ||
public string Url { get; set; } = default!; | ||
public int ModelRevision { get; set; } = default!; | ||
public DateTime ExipiresAt { get; set; } = default!; | ||
} | ||
} |
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
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
59 changes: 59 additions & 0 deletions
59
src/SIL.Machine.AspNetCore/Services/ModelCleanupService.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,59 @@ | ||
namespace SIL.Machine.AspNetCore.Services; | ||
|
||
public class ModelCleanupService( | ||
IServiceProvider services, | ||
ISharedFileService sharedFileService, | ||
IRepository<TranslationEngine> engines, | ||
ILogger<ModelCleanupService> logger | ||
) : RecurrentTask("Model Cleanup Service", services, RefreshPeriod, logger) | ||
{ | ||
private readonly ISharedFileService _sharedFileService = sharedFileService; | ||
private readonly ILogger<ModelCleanupService> _logger = logger; | ||
private readonly IRepository<TranslationEngine> _engines = engines; | ||
private static readonly TimeSpan RefreshPeriod = TimeSpan.FromDays(1); | ||
|
||
protected override async Task DoWorkAsync(IServiceScope scope, CancellationToken cancellationToken) | ||
{ | ||
await CheckModelsAsync(cancellationToken); | ||
} | ||
|
||
private async Task CheckModelsAsync(CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("Running model cleanup job"); | ||
IReadOnlyCollection<string> paths = await _sharedFileService.ListFilesAsync( | ||
NmtEngineService.ModelDirectory, | ||
cancellationToken: cancellationToken | ||
); | ||
// Get all engine ids from the database | ||
IReadOnlyList<TranslationEngine>? allEngines = await _engines.GetAllAsync(cancellationToken: cancellationToken); | ||
IEnumerable<string> validFilenames = allEngines.Select(e => | ||
NmtEngineService.GetModelPath(e.EngineId, e.BuildRevision) | ||
); | ||
// If there is a currently running build that creates and pushes a new file, but the database has not | ||
// updated yet, don't delete the new file. | ||
IEnumerable<string> validFilenamesForNextBuild = allEngines.Select(e => | ||
NmtEngineService.GetModelPath(e.EngineId, e.BuildRevision + 1) | ||
); | ||
HashSet<string> filenameFilter = validFilenames.Concat(validFilenamesForNextBuild).ToHashSet(); | ||
|
||
foreach (string path in paths) | ||
{ | ||
if (!filenameFilter.Contains(path)) | ||
{ | ||
await DeleteFileAsync( | ||
path, | ||
$"file in S3 bucket not found in database. It may be an old rev, etc.", | ||
cancellationToken | ||
); | ||
} | ||
} | ||
} | ||
|
||
private async Task DeleteFileAsync(string path, string message, CancellationToken cancellationToken = default) | ||
{ | ||
// This may delete a file while it is being downloaded, but the chance is rare | ||
// enough and the solution easy enough (just download again) to just live with it. | ||
_logger.LogInformation("Deleting old model file {filename}: {message}", path, message); | ||
await _sharedFileService.DeleteAsync(path, cancellationToken); | ||
} | ||
} |
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
Oops, something went wrong.