-
-
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 Collapse query Move cleanup service to serval.shared
- Loading branch information
1 parent
69fa745
commit d376ec5
Showing
14 changed files
with
298 additions
and
27 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
55 changes: 55 additions & 0 deletions
55
src/Serval/src/Serval.Shared/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,55 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SIL.ServiceToolkit.Services; | ||
|
||
namespace Serval.Shared.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) | ||
{ | ||
var now = DateTime.UtcNow; | ||
IEnumerable<T> uninitializedEntities = await entities.GetAllAsync( | ||
e => | ||
e.DateCreated != null | ||
&& e.DateCreated < now - _timeout | ||
&& e.IsInitialized != null | ||
&& !e.IsInitialized.Value, | ||
cancellationToken | ||
); | ||
|
||
foreach (T entity in uninitializedEntities) | ||
{ | ||
_logger.LogInformation( | ||
"Deleting {type} {id} because it was never successfully initialized.", | ||
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
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
7 changes: 7 additions & 0 deletions
7
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Serval.Translation.Services; | ||
|
||
public class BuildCleanupService( | ||
IServiceProvider services, | ||
ILogger<BuildCleanupService> logger, | ||
TimeSpan? timeout = null | ||
) : UninitializedCleanupService<Build>(services, logger, timeout) { } |
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
7 changes: 7 additions & 0 deletions
7
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Serval.Translation.Services; | ||
|
||
public class EngineCleanupService( | ||
IServiceProvider services, | ||
ILogger<EngineCleanupService> logger, | ||
TimeSpan? timeout = null | ||
) : UninitializedCleanupService<Engine>(services, logger, timeout) { } |
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.