Add envelope finalization trigger and workflow updates
Introduced `EnvelopeFinalizationTrigger` to enable external triggering of the `EnvelopeFinalizationWorker` for immediate envelope processing. Updated `EnvelopeFinalizationWorker` to support both polling and external triggers, improving performance and flexibility. Added `BackgroundServicesController` with an API endpoint to trigger the worker, including configuration checks for availability. Temporarily used `[AllowAnonymous]` with plans to replace it with sender-scoped authorization. Enhanced manual testing workflows in `AGENTS.md`: - Standardized request-to-domain mapping policy. - Migrated dynamic SQL to parameterized queries for security. - Improved UX for deterministic row double-click behavior. - Implemented SPA-style language switching. - Added sender-scoped caching for recent receiver suggestions. Updated dependency injection in `Program.cs` to register `EnvelopeFinalizationTrigger` and ensure the worker is only enabled when configured.
This commit is contained in:
@@ -294,6 +294,7 @@ Manual testing workflow:
|
|||||||
- Alternative UX design (Server component): in `EnvelopeGenerator.Server/EnvelopeGenerator.Server/Components/Pages/EnvelopeSenderPage.razor`, enforce deterministic row double-click behavior as exactly one action (preview OR edit), and keep row action buttons/tooltips aligned with the same rule to eliminate ambiguous navigation.
|
- Alternative UX design (Server component): in `EnvelopeGenerator.Server/EnvelopeGenerator.Server/Components/Pages/EnvelopeSenderPage.razor`, enforce deterministic row double-click behavior as exactly one action (preview OR edit), and keep row action buttons/tooltips aligned with the same rule to eliminate ambiguous navigation.
|
||||||
- Implement SPA-style language switching so the UI culture updates immediately without a full page reload.
|
- Implement SPA-style language switching so the UI culture updates immediately without a full page reload.
|
||||||
- Implement sender-scoped caching for "recent receiver suggestions" used during envelope creation (wizard/editor flows): cache key should include sender identity, support short TTL + invalidation hooks after successful envelope receiver writes, and prevent cross-sender suggestion leakage.
|
- Implement sender-scoped caching for "recent receiver suggestions" used during envelope creation (wizard/editor flows): cache key should include sender identity, support short TTL + invalidation hooks after successful envelope receiver writes, and prevent cross-sender suggestion leakage.
|
||||||
|
- Add authorization model for background-service trigger endpoints (`BackgroundServicesController`): replace temporary `AllowAnonymous`, define sender/admin policy, and enforce audit/rate-limit rules.
|
||||||
|
|
||||||
## Database
|
## Database
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
namespace EnvelopeGenerator.Application.Common.Interfaces;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public interface IEnvelopeFinalizationTrigger
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task TriggerAsync();
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
using EnvelopeGenerator.Application.Common.Interfaces;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.Server.BackgroundServices;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default signal implementation for immediate envelope finalization runs.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class EnvelopeFinalizationTrigger : IEnvelopeFinalizationTrigger
|
||||||
|
{
|
||||||
|
private readonly SemaphoreSlim _signal = new(0);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public void Trigger() => _signal.Release();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Task TriggerAsync()
|
||||||
|
{
|
||||||
|
Trigger();
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Task WaitAsync(CancellationToken cancellationToken) => _signal.WaitAsync(cancellationToken);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public void Drain()
|
||||||
|
{
|
||||||
|
while (_signal.Wait(0))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using EnvelopeGenerator.Application.Common.Interfaces;
|
||||||
using EnvelopeGenerator.Application.PdfSigning;
|
using EnvelopeGenerator.Application.PdfSigning;
|
||||||
using EnvelopeGenerator.Domain.Constants;
|
using EnvelopeGenerator.Domain.Constants;
|
||||||
using EnvelopeGenerator.Infrastructure;
|
using EnvelopeGenerator.Infrastructure;
|
||||||
@@ -11,12 +12,14 @@ namespace EnvelopeGenerator.Server.BackgroundServices;
|
|||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="scopeFactory"></param>
|
/// <param name="scopeFactory"></param>
|
||||||
|
/// <param name="trigger"></param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
public sealed class EnvelopeFinalizationWorker(
|
public sealed class EnvelopeFinalizationWorker(
|
||||||
IServiceScopeFactory scopeFactory,
|
IServiceScopeFactory scopeFactory,
|
||||||
|
EnvelopeFinalizationTrigger trigger,
|
||||||
ILogger<EnvelopeFinalizationWorker> logger) : BackgroundService
|
ILogger<EnvelopeFinalizationWorker> logger) : BackgroundService
|
||||||
{
|
{
|
||||||
private static readonly TimeSpan PollInterval = TimeSpan.FromSeconds(10);
|
private static readonly TimeSpan PollInterval = TimeSpan.FromMinutes(1);
|
||||||
private static readonly TimeSpan FailureBlockDuration = TimeSpan.FromHours(6);
|
private static readonly TimeSpan FailureBlockDuration = TimeSpan.FromHours(6);
|
||||||
private static readonly ConcurrentDictionary<int, DateTimeOffset> BlockedEnvelopes = new();
|
private static readonly ConcurrentDictionary<int, DateTimeOffset> BlockedEnvelopes = new();
|
||||||
|
|
||||||
@@ -97,7 +100,12 @@ public sealed class EnvelopeFinalizationWorker(
|
|||||||
logger.LogError(ex, "EnvelopeFinalizationWorker failed while reading envelopes.");
|
logger.LogError(ex, "EnvelopeFinalizationWorker failed while reading envelopes.");
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.Delay(PollInterval, stoppingToken);
|
var delayTask = Task.Delay(PollInterval, stoppingToken);
|
||||||
|
var triggerTask = trigger.WaitAsync(stoppingToken);
|
||||||
|
var completedTask = await Task.WhenAny(delayTask, triggerTask);
|
||||||
|
|
||||||
|
if (completedTask == triggerTask)
|
||||||
|
trigger.Drain();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
using EnvelopeGenerator.Server.BackgroundServices;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.Server.Controllers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Config"></param>
|
||||||
|
/// <param name="EnvFinalTrigger"></param>
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class BackgroundServicesController(IConfiguration Config, EnvelopeFinalizationTrigger EnvFinalTrigger) : ControllerBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
// TODO: Replace AllowAnonymous with sender-scoped authorization when background-service ACL is introduced.
|
||||||
|
[AllowAnonymous]
|
||||||
|
[HttpPost("envelope-finalization-worker/trigger")]
|
||||||
|
public IActionResult TriggerEnvelopeFinalizationWorker()
|
||||||
|
{
|
||||||
|
var isEnabled = Config.GetValue("BackgroundServices:EnableEnvelopeFinalizationWorker", false);
|
||||||
|
|
||||||
|
if (!isEnabled)
|
||||||
|
{
|
||||||
|
return StatusCode(StatusCodes.Status503ServiceUnavailable, new
|
||||||
|
{
|
||||||
|
Message = "Envelope finalization worker is not available."
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
EnvFinalTrigger.Trigger();
|
||||||
|
return Accepted(new
|
||||||
|
{
|
||||||
|
Message = "Envelope finalization worker trigger accepted."
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -427,6 +427,9 @@ try
|
|||||||
var enableEnvelopeFinalizationWorker = config.GetValue("BackgroundServices:EnableEnvelopeFinalizationWorker", false);
|
var enableEnvelopeFinalizationWorker = config.GetValue("BackgroundServices:EnableEnvelopeFinalizationWorker", false);
|
||||||
if (enableEnvelopeFinalizationWorker)
|
if (enableEnvelopeFinalizationWorker)
|
||||||
{
|
{
|
||||||
|
builder.Services.AddSingleton<EnvelopeFinalizationTrigger>();
|
||||||
|
builder.Services.AddSingleton<IEnvelopeFinalizationTrigger>(sp =>
|
||||||
|
sp.GetRequiredService<EnvelopeFinalizationTrigger>());
|
||||||
builder.Services.AddHostedService<EnvelopeFinalizationWorker>();
|
builder.Services.AddHostedService<EnvelopeFinalizationWorker>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user