-
-
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.
Create parent class and initializable interface
- Loading branch information
Showing
10 changed files
with
98 additions
and
91 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Serval.Shared.Models; | ||
|
||
public interface IInitializableEntity : IEntity | ||
{ | ||
bool? IsInitialized { get; set; } | ||
DateTime? DateCreated { get; set; } | ||
} |
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
37 changes: 1 addition & 36 deletions
37
src/Serval/src/Serval.Translation/Services/BuildCleanupService.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 |
---|---|---|
@@ -1,42 +1,7 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SIL.ServiceToolkit.Services; | ||
|
||
namespace Serval.Translation.Services; | ||
|
||
public class BuildCleanupService( | ||
IServiceProvider services, | ||
ILogger<BuildCleanupService> logger, | ||
TimeSpan? timeout = null | ||
) : RecurrentTask("Build Cleanup Service", services, RefreshPeriod, logger) | ||
{ | ||
private readonly ILogger<BuildCleanupService> _logger = logger; | ||
private readonly TimeSpan _timeout = timeout ?? TimeSpan.FromMinutes(2); | ||
private static readonly TimeSpan RefreshPeriod = TimeSpan.FromDays(1); | ||
|
||
protected override async Task DoWorkAsync(IServiceScope scope, CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("Running build cleanup job"); | ||
var builds = scope.ServiceProvider.GetRequiredService<IRepository<Build>>(); | ||
await CheckBuildsAsync(builds, cancellationToken); | ||
} | ||
|
||
public async Task CheckBuildsAsync(IRepository<Build> builds, CancellationToken cancellationToken) | ||
{ | ||
IReadOnlyList<Build> allBuilds = await builds.GetAllAsync(cancellationToken); | ||
IEnumerable<Build> notStartedBuilds = allBuilds.Where(b => !b.SuccessfullyStarted); | ||
await Task.Delay(_timeout, cancellationToken); //Make sure the builds are not midway through starting | ||
foreach ( | ||
Build build in await builds.GetAllAsync( | ||
b => notStartedBuilds.Select(c => c.Id).Contains(b.Id), | ||
cancellationToken | ||
) | ||
) | ||
{ | ||
if (!build.SuccessfullyStarted) | ||
{ | ||
_logger.LogInformation("Deleting build {id} because it was never successfully started", build.Id); | ||
await builds.DeleteAsync(build, cancellationToken); | ||
} | ||
} | ||
} | ||
} | ||
) : UninitializedCleanupService<Build>(services, logger, timeout) { } |
36 changes: 9 additions & 27 deletions
36
src/Serval/src/Serval.Translation/Services/EngineCleanupService.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 |
---|---|---|
@@ -1,47 +1,29 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SIL.ServiceToolkit.Services; | ||
|
||
namespace Serval.Translation.Services; | ||
|
||
public class EngineCleanupService( | ||
IServiceProvider services, | ||
ILogger<EngineCleanupService> logger, | ||
TimeSpan? timeout = null | ||
) : RecurrentTask("Engine Cleanup Service", services, RefreshPeriod, logger) | ||
) : UninitializedCleanupService<Engine>(services, logger, timeout) | ||
{ | ||
private readonly ILogger<EngineCleanupService> _logger = logger; | ||
private readonly TimeSpan _timeout = timeout ?? TimeSpan.FromMinutes(2); | ||
private static readonly TimeSpan RefreshPeriod = TimeSpan.FromDays(1); | ||
public EngineService? EngineService { get; set; } | ||
|
||
protected override async Task DoWorkAsync(IServiceScope scope, CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("Running engine cleanup job"); | ||
var engines = scope.ServiceProvider.GetRequiredService<IRepository<Engine>>(); | ||
var engineService = scope.ServiceProvider.GetRequiredService<EngineService>(); | ||
await CheckEnginesAsync(engines, engineService, cancellationToken); | ||
EngineService = scope.ServiceProvider.GetRequiredService<EngineService>(); | ||
await base.DoWorkAsync(scope, cancellationToken); | ||
} | ||
|
||
public async Task CheckEnginesAsync( | ||
protected override async Task DeleteEntityAsync( | ||
IRepository<Engine> engines, | ||
EngineService engineService, | ||
Engine engine, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
IReadOnlyList<Engine> allEngines = await engines.GetAllAsync(cancellationToken); | ||
IEnumerable<Engine> notCreatedEngines = allEngines.Where(e => !e.SuccessfullyCreated); | ||
await Task.Delay(_timeout, cancellationToken); //Make sure the engines are not midway through being created | ||
foreach ( | ||
Engine engine in await engines.GetAllAsync( | ||
e => notCreatedEngines.Select(f => f.Id).Contains(e.Id), | ||
cancellationToken | ||
) | ||
) | ||
{ | ||
if (!engine.SuccessfullyCreated) | ||
{ | ||
_logger.LogInformation("Deleting engine {id} because it was never successfully created", engine.Id); | ||
await engineService.DeleteAsync(engine.Id, cancellationToken); | ||
} | ||
} | ||
if (EngineService == null) | ||
return; | ||
await EngineService.DeleteAsync(engine.Id, 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
50 changes: 50 additions & 0 deletions
50
src/Serval/src/Serval.Translation/Services/UnitializedEntityCleanupService.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,50 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SIL.ServiceToolkit.Services; | ||
|
||
namespace Serval.Translation.Services; | ||
|
||
public abstract class UninitializedCleanupService<T>( | ||
IServiceProvider services, | ||
ILogger<UninitializedCleanupService<T>> logger, | ||
TimeSpan? timeout = null | ||
) : RecurrentTask($"{typeof(T)} Cleanup Service", services, RefreshPeriod, logger) | ||
where T : IInitializableEntity | ||
{ | ||
private readonly ILogger<UninitializedCleanupService<T>> _logger = logger; | ||
private readonly TimeSpan _timeout = timeout ?? TimeSpan.FromMinutes(2); | ||
private static readonly TimeSpan RefreshPeriod = TimeSpan.FromDays(1); | ||
|
||
protected override async Task DoWorkAsync(IServiceScope scope, CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("Running build cleanup job"); | ||
var entities = scope.ServiceProvider.GetRequiredService<IRepository<T>>(); | ||
await CheckEntitiesAsync(entities, cancellationToken); | ||
} | ||
|
||
public async Task CheckEntitiesAsync(IRepository<T> entities, CancellationToken cancellationToken) | ||
{ | ||
IReadOnlyList<T> allEntities = await entities.GetAllAsync(cancellationToken); | ||
var now = DateTime.UtcNow; | ||
IEnumerable<T> uninitializedEntities = allEntities.Where(b => | ||
!(b.IsInitialized ?? true) && (now - (b.DateCreated ?? DateTime.UtcNow)) > _timeout | ||
); | ||
foreach (T entity in uninitializedEntities) | ||
{ | ||
_logger.LogInformation( | ||
"Deleting {type} {id} because it was never successfully started", | ||
typeof(T), | ||
entity.Id | ||
); | ||
await DeleteEntityAsync(entities, entity, cancellationToken); | ||
} | ||
} | ||
|
||
protected virtual async Task DeleteEntityAsync( | ||
IRepository<T> entities, | ||
T entity, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
await entities.DeleteAsync(e => e.Id == entity.Id, 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
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