From 836c24f4d166fc2c0649ecef6ac090e1984a1c83 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:26:16 -0500 Subject: [PATCH] feat: begin working on Onspring worker --- .../Interfaces/IOnspringService.cs | 5 ++ .../Options/OnspringOptions.cs | 37 ++++++++++++++ src/SanctionsSearch.Worker/Program.cs | 3 ++ .../Services/OnspringService.cs | 5 ++ .../Setup/OnspringOptionsSetup.cs | 9 ++++ src/SanctionsSearch.Worker/Usings.cs | 1 + .../Workers/DatabaseWorker.cs | 10 +++- .../Workers/OnspringWorker.cs | 51 +++++++++++++++++++ .../appsettings.Example.json | 28 ++++++++++ 9 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 src/SanctionsSearch.Worker/Interfaces/IOnspringService.cs create mode 100644 src/SanctionsSearch.Worker/Options/OnspringOptions.cs create mode 100644 src/SanctionsSearch.Worker/Services/OnspringService.cs create mode 100644 src/SanctionsSearch.Worker/Setup/OnspringOptionsSetup.cs create mode 100644 src/SanctionsSearch.Worker/Workers/OnspringWorker.cs diff --git a/src/SanctionsSearch.Worker/Interfaces/IOnspringService.cs b/src/SanctionsSearch.Worker/Interfaces/IOnspringService.cs new file mode 100644 index 0000000..0803de8 --- /dev/null +++ b/src/SanctionsSearch.Worker/Interfaces/IOnspringService.cs @@ -0,0 +1,5 @@ +namespace SanctionsSearch.Worker.Interfaces; + +interface IOnspringService +{ +} \ No newline at end of file diff --git a/src/SanctionsSearch.Worker/Options/OnspringOptions.cs b/src/SanctionsSearch.Worker/Options/OnspringOptions.cs new file mode 100644 index 0000000..40f350e --- /dev/null +++ b/src/SanctionsSearch.Worker/Options/OnspringOptions.cs @@ -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; } +} \ No newline at end of file diff --git a/src/SanctionsSearch.Worker/Program.cs b/src/SanctionsSearch.Worker/Program.cs index 3883e8e..7838353 100644 --- a/src/SanctionsSearch.Worker/Program.cs +++ b/src/SanctionsSearch.Worker/Program.cs @@ -64,6 +64,9 @@ class Program builder.Services.ConfigureOptions(); builder.Services.AddScoped(rs => rs.GetRequiredService>().Value); + builder.Services.ConfigureOptions(); + builder.Services.AddScoped(rs => rs.GetRequiredService>().Value); + builder.Services.AddSingleton(TimeProvider.System); builder.Services diff --git a/src/SanctionsSearch.Worker/Services/OnspringService.cs b/src/SanctionsSearch.Worker/Services/OnspringService.cs new file mode 100644 index 0000000..7eddc3e --- /dev/null +++ b/src/SanctionsSearch.Worker/Services/OnspringService.cs @@ -0,0 +1,5 @@ +namespace SanctionsSearch.Worker.Services; + +class OnspringService : IOnspringService +{ +} \ No newline at end of file diff --git a/src/SanctionsSearch.Worker/Setup/OnspringOptionsSetup.cs b/src/SanctionsSearch.Worker/Setup/OnspringOptionsSetup.cs new file mode 100644 index 0000000..1c9e934 --- /dev/null +++ b/src/SanctionsSearch.Worker/Setup/OnspringOptionsSetup.cs @@ -0,0 +1,9 @@ +namespace SanctionsSearch.Worker.Setup; + +class OnspringOptionsSetup(IConfiguration configuration) : IConfigureOptions +{ + private const string SectionName = nameof(OnspringOptions); + private readonly IConfiguration _configuration = configuration; + + public void Configure(OnspringOptions options) => _configuration.GetSection(SectionName).Bind(options); +} \ No newline at end of file diff --git a/src/SanctionsSearch.Worker/Usings.cs b/src/SanctionsSearch.Worker/Usings.cs index b600fcc..8317b5b 100644 --- a/src/SanctionsSearch.Worker/Usings.cs +++ b/src/SanctionsSearch.Worker/Usings.cs @@ -16,6 +16,7 @@ global using SanctionsSearch.Worker.Setup; global using SanctionsSearch.Worker.Workers; global using Serilog; +global using Serilog.Context; global using Serilog.Exceptions; global using Serilog.Formatting.Compact; diff --git a/src/SanctionsSearch.Worker/Workers/DatabaseWorker.cs b/src/SanctionsSearch.Worker/Workers/DatabaseWorker.cs index 473cb93..da48ea8 100644 --- a/src/SanctionsSearch.Worker/Workers/DatabaseWorker.cs +++ b/src/SanctionsSearch.Worker/Workers/DatabaseWorker.cs @@ -17,7 +17,7 @@ public class DatabaseWorker( await UpdateDatabase(); - using var scope = _serviceScopeFactory.CreateScope(); + using var scope = _serviceScopeFactory.CreateAsyncScope(); var dbOptions = scope.ServiceProvider.GetRequiredService(); _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(); + } } } \ No newline at end of file diff --git a/src/SanctionsSearch.Worker/Workers/OnspringWorker.cs b/src/SanctionsSearch.Worker/Workers/OnspringWorker.cs new file mode 100644 index 0000000..57232af --- /dev/null +++ b/src/SanctionsSearch.Worker/Workers/OnspringWorker.cs @@ -0,0 +1,51 @@ + +namespace SanctionsSearch.Worker.Workers; + +public class OnspringWorker( + ILogger logger, + TimeProvider timeProvider, + IServiceScopeFactory serviceScopeFactory +) : IHostedService, IDisposable +{ + private readonly ILogger _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; + } +} \ No newline at end of file diff --git a/src/SanctionsSearch.Worker/appsettings.Example.json b/src/SanctionsSearch.Worker/appsettings.Example.json index ed4fffe..384dd61 100644 --- a/src/SanctionsSearch.Worker/appsettings.Example.json +++ b/src/SanctionsSearch.Worker/appsettings.Example.json @@ -19,5 +19,33 @@ "DbOptions": { "DatabaseName": "SanctionsSearch.example.db", "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 + } } } \ No newline at end of file