Add remote trigger support for finalization worker
Introduced `RemoteEnvelopeFinalizationTrigger` to enable triggering the envelope finalization worker via a remote HTTP endpoint. Added `BackgroundServicesOptions` for centralized configuration of background services, including enabling/disabling the local worker and configuring the remote trigger URL. Enhanced `TriggerFinalizationWorkerBehavior` with improved error handling and logging using `ILogger`. Added `DisableControllerConvention` to dynamically exclude controllers based on configuration. Updated `Program.cs` for conditional service registration and improved configuration loading. Modified `appsettings.json` and `appsettings.Development.json` to include remote trigger settings. Incremented project version to `2.1.0-beta`. Refactored code for better maintainability, scalability, and observability.
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
using EnvelopeGenerator.Application.Common.Interfaces;
|
using EnvelopeGenerator.Application.Common.Interfaces;
|
||||||
using EnvelopeGenerator.Application.DocReceiverElements.Commands;
|
using EnvelopeGenerator.Application.DocReceiverElements.Commands;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Application.DocReceiverElements.Behaviors;
|
namespace EnvelopeGenerator.Application.DocReceiverElements.Behaviors;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class TriggerFinalizationWorkerBehavior(IEnvelopeFinalizationTrigger? Trigger = null) : IPipelineBehavior<SigningCommand, Unit>
|
public class TriggerFinalizationWorkerBehavior(ILogger<TriggerFinalizationWorkerBehavior> Logger, IEnvelopeFinalizationTrigger? Trigger = null) : IPipelineBehavior<SigningCommand, Unit>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
@@ -20,7 +21,16 @@ public class TriggerFinalizationWorkerBehavior(IEnvelopeFinalizationTrigger? Tri
|
|||||||
public async Task<Unit> Handle(SigningCommand request, RequestHandlerDelegate<Unit> next, CancellationToken cancellationToken)
|
public async Task<Unit> Handle(SigningCommand request, RequestHandlerDelegate<Unit> next, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if(Trigger != null)
|
if(Trigger != null)
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
await Trigger.TriggerAsync(cancellationToken);
|
await Trigger.TriggerAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Log the exception or handle it as needed
|
||||||
|
Logger.LogError(ex, "Error triggering finalization worker");
|
||||||
|
}
|
||||||
|
|
||||||
return await next(cancellationToken);
|
return await next(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
using EnvelopeGenerator.Application.Common.Interfaces;
|
||||||
|
using EnvelopeGenerator.Server.Options;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.Server.BackgroundServices;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Triggers envelope finalization worker via remote HTTP endpoint.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class RemoteEnvelopeFinalizationTrigger(
|
||||||
|
IHttpClientFactory httpClientFactory,
|
||||||
|
IOptions<BackgroundServicesOptions> options,
|
||||||
|
ILogger<RemoteEnvelopeFinalizationTrigger> logger) : IEnvelopeFinalizationTrigger
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a remote trigger request to background-services endpoint.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task TriggerAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var url = options.Value.RemoteEnvelopeFinalizationTrigger.Url;
|
||||||
|
if (string.IsNullOrWhiteSpace(url))
|
||||||
|
throw new InvalidOperationException("BackgroundServices:RemoteEnvelopeFinalizationTrigger:Url is not configured.");
|
||||||
|
|
||||||
|
var client = httpClientFactory.CreateClient(RemoteEnvelopeFinalizationTriggerOptions.HttpClientName);
|
||||||
|
using var request = new HttpRequestMessage(HttpMethod.Post, url);
|
||||||
|
|
||||||
|
using var response = await client.SendAsync(request, cancellationToken);
|
||||||
|
if (response.IsSuccessStatusCode)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var statusCode = (int)response.StatusCode;
|
||||||
|
logger.LogWarning(
|
||||||
|
"Remote envelope finalization trigger failed with status code {StatusCode}.",
|
||||||
|
statusCode);
|
||||||
|
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.Server.Conventions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes selected controller from MVC discovery.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class DisableControllerConvention(Type controllerType) : IApplicationModelConvention
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Removes configured controller from discovered MVC controller set.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="application"></param>
|
||||||
|
public void Apply(ApplicationModel application)
|
||||||
|
{
|
||||||
|
var controller = application.Controllers.FirstOrDefault(c => c.ControllerType == controllerType);
|
||||||
|
if (controller is not null)
|
||||||
|
application.Controllers.Remove(controller);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,9 +10,9 @@
|
|||||||
<Authors>Digital Data GmbH</Authors>
|
<Authors>Digital Data GmbH</Authors>
|
||||||
<Company>Digital Data GmbH</Company>
|
<Company>Digital Data GmbH</Company>
|
||||||
<Product>EnvelopeGenerator.Server</Product>
|
<Product>EnvelopeGenerator.Server</Product>
|
||||||
<Version>2.0.1-beta</Version>
|
<Version>2.1.0-beta</Version>
|
||||||
<FileVersion>2.0.0.0</FileVersion>
|
<FileVersion>2.1.0.0</FileVersion>
|
||||||
<AssemblyVersion>2.0.0.0</AssemblyVersion>
|
<AssemblyVersion>2.1.0.0</AssemblyVersion>
|
||||||
<Copyright>Copyright © 2026 Digital Data GmbH. All rights reserved.</Copyright>
|
<Copyright>Copyright © 2026 Digital Data GmbH. All rights reserved.</Copyright>
|
||||||
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
namespace EnvelopeGenerator.Server.Options;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Configuration options for background service behavior.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class BackgroundServicesOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Configuration section name in appsettings.json.
|
||||||
|
/// </summary>
|
||||||
|
public const string SectionName = "BackgroundServices";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enables local hosted envelope finalization worker.
|
||||||
|
/// </summary>
|
||||||
|
public bool EnableEnvelopeFinalizationWorker { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remote trigger endpoint configuration used when local worker is disabled.
|
||||||
|
/// </summary>
|
||||||
|
public RemoteEnvelopeFinalizationTriggerOptions RemoteEnvelopeFinalizationTrigger { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Configuration options for remote envelope finalization trigger endpoint.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class RemoteEnvelopeFinalizationTriggerOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Named HttpClient key for remote trigger calls.
|
||||||
|
/// </summary>
|
||||||
|
public const string HttpClientName = "EnvelopeFinalizationRemoteTrigger";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Absolute trigger URL (for example: https://host/api/BackgroundServices/envelope-finalization-worker/trigger).
|
||||||
|
/// </summary>
|
||||||
|
public string? Url { get; set; }
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ using EnvelopeGenerator.Infrastructure.Notification;
|
|||||||
using EnvelopeGenerator.Server;
|
using EnvelopeGenerator.Server;
|
||||||
using EnvelopeGenerator.Server.Client.Services;
|
using EnvelopeGenerator.Server.Client.Services;
|
||||||
using EnvelopeGenerator.Server.Components;
|
using EnvelopeGenerator.Server.Components;
|
||||||
|
using EnvelopeGenerator.Server.Conventions;
|
||||||
using EnvelopeGenerator.Server.Models;
|
using EnvelopeGenerator.Server.Models;
|
||||||
using EnvelopeGenerator.Server.Options;
|
using EnvelopeGenerator.Server.Options;
|
||||||
using EnvelopeGenerator.Server.Services;
|
using EnvelopeGenerator.Server.Services;
|
||||||
@@ -84,6 +85,9 @@ try
|
|||||||
.WriteTo.SQLite(sqliteDbPath, storeTimestampInUtc: true));
|
.WriteTo.SQLite(sqliteDbPath, storeTimestampInUtc: true));
|
||||||
|
|
||||||
var config = builder.Configuration;
|
var config = builder.Configuration;
|
||||||
|
var backgroundServicesOptions = config
|
||||||
|
.GetSection(BackgroundServicesOptions.SectionName)
|
||||||
|
.Get<BackgroundServicesOptions>() ?? new BackgroundServicesOptions();
|
||||||
|
|
||||||
var deferredProvider = new DeferredServiceProvider();
|
var deferredProvider = new DeferredServiceProvider();
|
||||||
|
|
||||||
@@ -101,7 +105,11 @@ try
|
|||||||
builder.Services.AddMudServices();
|
builder.Services.AddMudServices();
|
||||||
|
|
||||||
// Add API Controllers
|
// Add API Controllers
|
||||||
builder.Services.AddControllers()
|
builder.Services.AddControllers(options =>
|
||||||
|
{
|
||||||
|
if (!backgroundServicesOptions.EnableEnvelopeFinalizationWorker)
|
||||||
|
options.Conventions.Add(new DisableControllerConvention(typeof(EnvelopeGenerator.Server.Controllers.BackgroundServicesController)));
|
||||||
|
})
|
||||||
.AddJsonOptions(options =>
|
.AddJsonOptions(options =>
|
||||||
{
|
{
|
||||||
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
|
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
|
||||||
@@ -349,6 +357,7 @@ try
|
|||||||
builder.Services.Configure<CacheOptions>(config.GetSection(CacheOptions.SectionName));
|
builder.Services.Configure<CacheOptions>(config.GetSection(CacheOptions.SectionName));
|
||||||
builder.Services.Configure<SenderFeatureAccessOptions>(config.GetSection(SenderFeatureAccessOptions.SectionName));
|
builder.Services.Configure<SenderFeatureAccessOptions>(config.GetSection(SenderFeatureAccessOptions.SectionName));
|
||||||
builder.Services.Configure<SerilogUiAccessOptions>(config.GetSection(SerilogUiAccessOptions.SectionName));
|
builder.Services.Configure<SerilogUiAccessOptions>(config.GetSection(SerilogUiAccessOptions.SectionName));
|
||||||
|
builder.Services.Configure<BackgroundServicesOptions>(config.GetSection(BackgroundServicesOptions.SectionName));
|
||||||
|
|
||||||
// Distributed Cache - SQL Server
|
// Distributed Cache - SQL Server
|
||||||
builder.Services.AddDistributedSqlServerCache(options =>
|
builder.Services.AddDistributedSqlServerCache(options =>
|
||||||
@@ -424,14 +433,18 @@ try
|
|||||||
builder.Services.AddScoped<EnvelopeReceiverAuthorizationService>();
|
builder.Services.AddScoped<EnvelopeReceiverAuthorizationService>();
|
||||||
builder.Services.AddScoped<EnvelopeReceiverPageDataService>();
|
builder.Services.AddScoped<EnvelopeReceiverPageDataService>();
|
||||||
|
|
||||||
var enableEnvelopeFinalizationWorker = config.GetValue("BackgroundServices:EnableEnvelopeFinalizationWorker", false);
|
if (backgroundServicesOptions.EnableEnvelopeFinalizationWorker)
|
||||||
if (enableEnvelopeFinalizationWorker)
|
|
||||||
{
|
{
|
||||||
builder.Services.AddSingleton<EnvelopeFinalizationTrigger>();
|
builder.Services.AddSingleton<EnvelopeFinalizationTrigger>();
|
||||||
builder.Services.AddSingleton<IEnvelopeFinalizationTrigger>(sp =>
|
builder.Services.AddSingleton<IEnvelopeFinalizationTrigger>(sp =>
|
||||||
sp.GetRequiredService<EnvelopeFinalizationTrigger>());
|
sp.GetRequiredService<EnvelopeFinalizationTrigger>());
|
||||||
builder.Services.AddHostedService<EnvelopeFinalizationWorker>();
|
builder.Services.AddHostedService<EnvelopeFinalizationWorker>();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
builder.Services.AddHttpClient(RemoteEnvelopeFinalizationTriggerOptions.HttpClientName);
|
||||||
|
builder.Services.AddSingleton<IEnvelopeFinalizationTrigger, RemoteEnvelopeFinalizationTrigger>();
|
||||||
|
}
|
||||||
|
|
||||||
// DevExpress Server-Side Services (CRITICAL for DxPdfViewer)
|
// DevExpress Server-Side Services (CRITICAL for DxPdfViewer)
|
||||||
builder.Services.AddDevExpressBlazor();
|
builder.Services.AddDevExpressBlazor();
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
{
|
{
|
||||||
"BackgroundServices": {
|
"BackgroundServices": {
|
||||||
"EnableEnvelopeFinalizationWorker": true
|
"EnableEnvelopeFinalizationWorker": false,
|
||||||
|
"RemoteEnvelopeFinalizationTrigger": {
|
||||||
|
"Url": "http://172.24.12.39:1416"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
|
|||||||
@@ -3,7 +3,10 @@
|
|||||||
"UseScalar": true,
|
"UseScalar": true,
|
||||||
"UseDbMigration": false,
|
"UseDbMigration": false,
|
||||||
"BackgroundServices": {
|
"BackgroundServices": {
|
||||||
"EnableEnvelopeFinalizationWorker": false
|
"EnableEnvelopeFinalizationWorker": false,
|
||||||
|
"RemoteEnvelopeFinalizationTrigger": {
|
||||||
|
"Url": "http://172.24.12.39:1416"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"DevEndpoints": {
|
"DevEndpoints": {
|
||||||
"EnableSignatureInkInProduction": false
|
"EnableSignatureInkInProduction": false
|
||||||
|
|||||||
Reference in New Issue
Block a user