feat: begin working on Onspring worker
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Interfaces;
|
||||||
|
|
||||||
|
interface IOnspringService
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Options;
|
||||||
|
|
||||||
|
class OnspringOptions
|
||||||
|
{
|
||||||
|
public string BaseUrl { get; set; } = "https://api.onspring.com";
|
||||||
|
public string ApiKey { get; set; } = string.Empty;
|
||||||
|
public int SearchIntervalInMinutes { get; set; } = 1;
|
||||||
|
public SearchRequestOptions SearchRequestOptions { get; set; } = new SearchRequestOptions();
|
||||||
|
public SearchResultOptions SearchResultOptions { get; set; } = new SearchResultOptions();
|
||||||
|
}
|
||||||
|
|
||||||
|
class SearchRequestOptions
|
||||||
|
{
|
||||||
|
public int AppId { get; set; }
|
||||||
|
public int NameFieldId { get; set; }
|
||||||
|
public int AddressFieldId { get; set; }
|
||||||
|
public int CityFieldId { get; set; }
|
||||||
|
public int StateFieldId { get; set; }
|
||||||
|
public int ZipFieldId { get; set; }
|
||||||
|
public int CountryFieldId { get; set; }
|
||||||
|
public int StatusFieldId { get; set; }
|
||||||
|
public Guid AwaitingProcessingStatusId { get; set; }
|
||||||
|
public Guid ProcessingStatusId { get; set; }
|
||||||
|
public Guid ProcessedSuccessStatusId { get; set; }
|
||||||
|
public Guid ProcessedErrorStatusId { get; set; }
|
||||||
|
public int ErrorFieldId { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
class SearchResultOptions
|
||||||
|
{
|
||||||
|
public int AppId { get; set; }
|
||||||
|
public int SearchRequestFieldId { get; set; }
|
||||||
|
public int NameFieldId { get; set; }
|
||||||
|
public int AddressFieldId { get; set; }
|
||||||
|
public int TypeFieldId { get; set; }
|
||||||
|
public int ProgramsFieldId { get; set; }
|
||||||
|
}
|
||||||
@@ -64,6 +64,9 @@ class Program
|
|||||||
builder.Services.ConfigureOptions<DbOptionsSetup>();
|
builder.Services.ConfigureOptions<DbOptionsSetup>();
|
||||||
builder.Services.AddScoped(rs => rs.GetRequiredService<IOptionsSnapshot<DbOptions>>().Value);
|
builder.Services.AddScoped(rs => rs.GetRequiredService<IOptionsSnapshot<DbOptions>>().Value);
|
||||||
|
|
||||||
|
builder.Services.ConfigureOptions<OnspringOptionsSetup>();
|
||||||
|
builder.Services.AddScoped(rs => rs.GetRequiredService<IOptionsSnapshot<OnspringOptions>>().Value);
|
||||||
|
|
||||||
builder.Services.AddSingleton(TimeProvider.System);
|
builder.Services.AddSingleton(TimeProvider.System);
|
||||||
|
|
||||||
builder.Services
|
builder.Services
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Services;
|
||||||
|
|
||||||
|
class OnspringService : IOnspringService
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Setup;
|
||||||
|
|
||||||
|
class OnspringOptionsSetup(IConfiguration configuration) : IConfigureOptions<OnspringOptions>
|
||||||
|
{
|
||||||
|
private const string SectionName = nameof(OnspringOptions);
|
||||||
|
private readonly IConfiguration _configuration = configuration;
|
||||||
|
|
||||||
|
public void Configure(OnspringOptions options) => _configuration.GetSection(SectionName).Bind(options);
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@ global using SanctionsSearch.Worker.Setup;
|
|||||||
global using SanctionsSearch.Worker.Workers;
|
global using SanctionsSearch.Worker.Workers;
|
||||||
|
|
||||||
global using Serilog;
|
global using Serilog;
|
||||||
|
global using Serilog.Context;
|
||||||
global using Serilog.Exceptions;
|
global using Serilog.Exceptions;
|
||||||
global using Serilog.Formatting.Compact;
|
global using Serilog.Formatting.Compact;
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ public class DatabaseWorker(
|
|||||||
|
|
||||||
await UpdateDatabase();
|
await UpdateDatabase();
|
||||||
|
|
||||||
using var scope = _serviceScopeFactory.CreateScope();
|
using var scope = _serviceScopeFactory.CreateAsyncScope();
|
||||||
var dbOptions = scope.ServiceProvider.GetRequiredService<DbOptions>();
|
var dbOptions = scope.ServiceProvider.GetRequiredService<DbOptions>();
|
||||||
|
|
||||||
_timer = _timeProvider.CreateTimer(
|
_timer = _timeProvider.CreateTimer(
|
||||||
@@ -40,10 +40,14 @@ public class DatabaseWorker(
|
|||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_timer?.Dispose();
|
_timer?.Dispose();
|
||||||
|
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task UpdateDatabase()
|
private async Task UpdateDatabase()
|
||||||
{
|
{
|
||||||
|
var updateIdProperty = LogContext.PushProperty("DatabaseUpdateId", Guid.NewGuid());
|
||||||
|
|
||||||
_logger.LogInformation("Updating database");
|
_logger.LogInformation("Updating database");
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -63,5 +67,9 @@ public class DatabaseWorker(
|
|||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error updating database");
|
_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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,5 +19,33 @@
|
|||||||
"DbOptions": {
|
"DbOptions": {
|
||||||
"DatabaseName": "SanctionsSearch.example.db",
|
"DatabaseName": "SanctionsSearch.example.db",
|
||||||
"RefreshIntervalInHours": 1
|
"RefreshIntervalInHours": 1
|
||||||
|
},
|
||||||
|
"OnspringOptions": {
|
||||||
|
"BaseUrl": "https://api.onspring.com",
|
||||||
|
"ApiKey": "000000ffffff000000ffffff/00000000-ffff-0000-ffff-000000000000",
|
||||||
|
"SearchIntervalInMinutes": 1,
|
||||||
|
"SearchRequestOptions": {
|
||||||
|
"AppId": 949,
|
||||||
|
"NameFieldId": 20946,
|
||||||
|
"AddressFieldId": 20947,
|
||||||
|
"CityFieldId": 20948,
|
||||||
|
"StateFieldId": 20949,
|
||||||
|
"ZipFieldId": 20967,
|
||||||
|
"CountryFieldId": 20950,
|
||||||
|
"StatusFieldId": 20964,
|
||||||
|
"AwaitingProcessingStatusId": "3cea19f0-afae-4dac-ac5b-e6f5555659b3",
|
||||||
|
"ProcessingStatusId": "597c3096-ab0e-4910-a7a9-98d149acee81",
|
||||||
|
"ProcessedSuccessStatusId": "52eeaf52-7058-4c30-b147-2e36b78a8371",
|
||||||
|
"ProcessedErrorStatusId": "ccf2651c-7f70-409c-adc1-b930ab24be6d",
|
||||||
|
"ErrorFieldId": 20968
|
||||||
|
},
|
||||||
|
"SearchResultOptions": {
|
||||||
|
"AppId": 950,
|
||||||
|
"SearchRequestFieldId": 20962,
|
||||||
|
"NameFieldId": 20958,
|
||||||
|
"AddressFieldId": 20959,
|
||||||
|
"TypeFieldId": 20965,
|
||||||
|
"ProgramsFieldId": 20966
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user