feat(auditlog): make SiteAuditBacklogReporter cadence configurable (deferred #21)
Consolidates the reporter's poll cadence onto SqliteAuditWriterOptions instead of the hard-coded 30 s constant, closing deferred-work register item #21 (the config- shape cleanup its own XML doc flagged as a follow-up). - SqliteAuditWriterOptions.BacklogPollIntervalSeconds (default 30). - Reporter reads it via IOptions; precedence explicit-override (tests) > configured > 30 s default; a non-positive value falls back to the default. Corrected the stale "hard-code / tunable in a follow-up" class-doc. - Cadence tests + register updated (row 21 -> Resolved). Claude-Session: https://claude.ai/code/session_01MtdgwpEeCUn6cUA5f1LMPj
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
||||
using ZB.MOM.WW.ScadaBridge.HealthMonitoring;
|
||||
|
||||
@@ -25,9 +26,8 @@ namespace ZB.MOM.WW.ScadaBridge.AuditLog.Site;
|
||||
/// <b>Cadence.</b> 30 s by default — coarse enough to amortise the SQL probe
|
||||
/// across many reports, fine enough that the central dashboard never lags by
|
||||
/// more than one health-report interval. Tunable via
|
||||
/// <see cref="ZB.MOM.WW.ScadaBridge.AuditLog.Site.SqliteAuditWriterOptions"/> in a follow-up
|
||||
/// if ops needs a different cadence; for now we hard-code the value because the
|
||||
/// brief calls it out explicitly.
|
||||
/// <see cref="ZB.MOM.WW.ScadaBridge.AuditLog.Site.SqliteAuditWriterOptions.BacklogPollIntervalSeconds"/>
|
||||
/// (a non-positive value falls back to the 30 s default).
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Failure containment.</b> The probe call is wrapped in a try/catch so a
|
||||
@@ -56,17 +56,42 @@ public sealed class SiteAuditBacklogReporter : IHostedService, IDisposable
|
||||
/// <param name="queue">The site audit queue used to probe the backlog count.</param>
|
||||
/// <param name="collector">The site health collector that receives the backlog snapshot.</param>
|
||||
/// <param name="logger">Logger instance.</param>
|
||||
/// <param name="refreshInterval">Poll interval override; defaults to <see cref="DefaultRefreshInterval"/> (30 s).</param>
|
||||
/// <param name="refreshInterval">
|
||||
/// Explicit poll-interval override (used by tests). When null, the cadence comes from
|
||||
/// <see cref="SqliteAuditWriterOptions.BacklogPollIntervalSeconds"/>, falling back to
|
||||
/// <see cref="DefaultRefreshInterval"/> (30 s) when that is unset or non-positive.
|
||||
/// </param>
|
||||
/// <param name="options">
|
||||
/// SQLite audit-writer options supplying the configurable poll cadence
|
||||
/// (<see cref="SqliteAuditWriterOptions.BacklogPollIntervalSeconds"/>). Optional so
|
||||
/// direct construction in tests need not build the options graph.
|
||||
/// </param>
|
||||
public SiteAuditBacklogReporter(
|
||||
ISiteAuditQueue queue,
|
||||
ISiteHealthCollector collector,
|
||||
ILogger<SiteAuditBacklogReporter> logger,
|
||||
TimeSpan? refreshInterval = null)
|
||||
TimeSpan? refreshInterval = null,
|
||||
IOptions<SqliteAuditWriterOptions>? options = null)
|
||||
{
|
||||
_queue = queue ?? throw new ArgumentNullException(nameof(queue));
|
||||
_collector = collector ?? throw new ArgumentNullException(nameof(collector));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_refreshInterval = refreshInterval ?? DefaultRefreshInterval;
|
||||
// Precedence: explicit override (tests) → configured cadence → 30 s default.
|
||||
_refreshInterval = refreshInterval ?? ResolveInterval(options) ?? DefaultRefreshInterval;
|
||||
}
|
||||
|
||||
/// <summary>The resolved poll cadence (exposed for tests).</summary>
|
||||
internal TimeSpan RefreshInterval => _refreshInterval;
|
||||
|
||||
/// <summary>
|
||||
/// Maps <see cref="SqliteAuditWriterOptions.BacklogPollIntervalSeconds"/> to a
|
||||
/// <see cref="TimeSpan"/>, or null when unconfigured / non-positive (caller then
|
||||
/// falls back to <see cref="DefaultRefreshInterval"/>).
|
||||
/// </summary>
|
||||
private static TimeSpan? ResolveInterval(IOptions<SqliteAuditWriterOptions>? options)
|
||||
{
|
||||
var seconds = options?.Value.BacklogPollIntervalSeconds ?? 0;
|
||||
return seconds > 0 ? TimeSpan.FromSeconds(seconds) : null;
|
||||
}
|
||||
|
||||
/// <summary>Starts the background polling loop, running an immediate first probe before entering the timed cycle.</summary>
|
||||
|
||||
@@ -24,4 +24,13 @@ public sealed class SqliteAuditWriterOptions
|
||||
|
||||
/// <summary>Soft flush interval the writer enforces when fewer than BatchSize events are queued.</summary>
|
||||
public int FlushIntervalMs { get; set; } = 50;
|
||||
|
||||
/// <summary>
|
||||
/// Poll cadence, in seconds, for the <c>SiteAuditBacklogReporter</c> hosted service
|
||||
/// that probes the SQLite audit backlog and pushes the snapshot onto the site health
|
||||
/// report. Half a typical 60 s health-report interval keeps the snapshot fresh without
|
||||
/// spinning the SQL probe more often than necessary. A non-positive value falls back to
|
||||
/// the reporter's 30 s default.
|
||||
/// </summary>
|
||||
public int BacklogPollIntervalSeconds { get; set; } = 30;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user