feat: begin working on Onspring worker
This commit is contained in:
@@ -17,7 +17,7 @@ public class DatabaseWorker(
|
||||
|
||||
await UpdateDatabase();
|
||||
|
||||
using var scope = _serviceScopeFactory.CreateScope();
|
||||
using var scope = _serviceScopeFactory.CreateAsyncScope();
|
||||
var dbOptions = scope.ServiceProvider.GetRequiredService<DbOptions>();
|
||||
|
||||
_timer = _timeProvider.CreateTimer(
|
||||
@@ -40,10 +40,14 @@ public class DatabaseWorker(
|
||||
public void Dispose()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private async Task UpdateDatabase()
|
||||
{
|
||||
var updateIdProperty = LogContext.PushProperty("DatabaseUpdateId", Guid.NewGuid());
|
||||
|
||||
_logger.LogInformation("Updating database");
|
||||
|
||||
try
|
||||
@@ -63,5 +67,9 @@ public class DatabaseWorker(
|
||||
{
|
||||
_logger.LogError(ex, "Error updating database");
|
||||
}
|
||||
finally
|
||||
{
|
||||
updateIdProperty.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
|
||||
namespace SanctionsSearch.Worker.Workers;
|
||||
|
||||
public class OnspringWorker(
|
||||
ILogger<OnspringWorker> logger,
|
||||
TimeProvider timeProvider,
|
||||
IServiceScopeFactory serviceScopeFactory
|
||||
) : IHostedService, IDisposable
|
||||
{
|
||||
private readonly ILogger<OnspringWorker> _logger = logger;
|
||||
private readonly TimeProvider _timeProvider = timeProvider;
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory = serviceScopeFactory;
|
||||
private ITimer? _timer;
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Onspring worker started");
|
||||
|
||||
_timer = _timeProvider.CreateTimer(
|
||||
callback: async _ => await RunSearchRequests(),
|
||||
state: null,
|
||||
dueTime: TimeSpan.Zero,
|
||||
period: TimeSpan.FromMinutes(1)
|
||||
);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Onspring worker stopped");
|
||||
|
||||
_timer?.Change(Timeout.InfiniteTimeSpan, TimeSpan.Zero);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private Task RunSearchRequests()
|
||||
{
|
||||
_logger.LogInformation("Running search requests");
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user