using AttendanceHub.Sync.Configuration;
using AttendanceHub.Sync.Domain;
using AttendanceHub.Sync.Workers;
using Microsoft.Extensions.Logging;
namespace AttendanceHub.Sync.Host;
///
/// Prints the plan this process is actually going to run, at startup, as the first thing an
/// operator reads.
///
/// The 2026-10-11 outage was invisible precisely because the effective plan was never stated: the
/// file on disk said one thing, the process behaved as acquisition-only, and every log line after
/// that was individually healthy. A reader had no way to tell from the log what the host was
/// SUPPOSED to be doing, so nothing looked anomalous. Naming the plan once, loudly, closes that:
/// "delivery=DISABLED" is the line that would have ended that incident in seconds.
///
/// Every value here is the resolved one, not the configured one — the point is to show what the
/// code will do after defaults, overrides and dynamic device resolution have been applied.
///
public static class EffectivePlan
{
public static void Print(
ILogger log,
DeviceEndpoint staticDevice,
DevicesConfig? devicesCfg,
IReadOnlyList activeDevices,
UploadConfig? uploadCfg,
HeartbeatConfig? heartbeatCfg,
ReadConfig read,
SupervisorConfig supervisor,
SdkConfig sdk,
string dbPath)
{
log.LogInformation("EFFECTIVE_PLAN_BEGIN ==========================================");
// ---- delivery: the single most important fact, stated first ----
if (uploadCfg is not null)
{
log.LogInformation(
"EFFECTIVE_PLAN_DELIVERY enabled=YES endpoint={Ep} path={Path} batchSize={B} "
+ "maxConcurrency={C} pollMs={P} timeoutSec={T} payloadDeviceId={Pd}",
new Uri(uploadCfg.BaseAddress, uploadCfg.BatchPath), uploadCfg.BatchPath,
uploadCfg.BatchSize, uploadCfg.MaxConcurrency, uploadCfg.PollingIntervalMs,
(int)uploadCfg.Timeout.TotalSeconds, uploadCfg.DeviceId);
}
else
{
// Same severity as UPLOAD_INACTIVE, and in the same block, so the plan cannot be read
// without also reading that nothing will be delivered.
log.LogWarning(
"EFFECTIVE_PLAN_DELIVERY enabled=NO endpoint=(none) — this host is ACQUISITION-ONLY. "
+ "Punches will be captured to the local ledger and will NOT reach the API/MySQL.");
}
// ---- acquisition ----
log.LogInformation(
"EFFECTIVE_PLAN_ACQUISITION staticDevice={Dev} source={Src} refreshSec={Ref} "
+ "activeDevices={N} ids={Ids} commTimeoutSec={T}",
staticDevice.DeviceId,
devicesCfg?.Source ?? "static",
devicesCfg?.RefreshSeconds ?? 0,
activeDevices.Count,
activeDevices.Count == 0 ? "(none)" : string.Join(",", activeDevices),
sdk.CommTimeoutSeconds);
// ---- cadence: what the reader costs and how often it is paid ----
// The effective read cadence is not any single configured number. While a device's windowed
// API is still being validated the supervisor uses readIntervalMs; once the window is proven
// valid it uses windowedIntervalMs; once it is proven unusable it drops to the full-reconcile
// period. Printing all three states means a reader can predict the load from the log alone.
log.LogInformation(
"EFFECTIVE_PLAN_CADENCE liveEventMask=1 healthMs={H} windowedReadMs={W} "
+ "fallbackReadMs={R} fullReconcileMin={F} overlapMin={O} lookbackMin={Lb} "
+ "effectiveReadWhenWindowValidMs={E}",
supervisor.HealthIntervalMs,
read.WindowedIntervalMs,
supervisor.ReadIntervalMs,
read.FullReconcileMinutes,
read.OverlapMinutes,
read.LookbackMinutes,
Math.Min(read.WindowedIntervalMs, supervisor.ReadIntervalMs));
// ---- retention: the ledger is bounded, and by what ----
log.LogInformation(
"EFFECTIVE_PLAN_RETENTION db={Db} punchRetentionDays={P} dlqRetentionDays={D} "
+ "deliveriesAre_PRUNED_FROM_THE_LEDGER_ONLY — pruning never touches the API/MySQL copy",
dbPath, RetentionPolicy.PunchRetentionDays, LedgerMaintenance.DlqRetentionDays);
// ---- the guard rails ----
log.LogInformation(
"EFFECTIVE_PLAN_WATCHDOG pollMs={P} stallThresholdSamples={T} stallAfterSec={S} "
+ "predicate=growth_not_occupancy — raises UPLOAD_STALL if the backlog climbs and nothing drains it",
(int)DeliveryWatchdog.PollInterval.TotalMilliseconds,
DeliveryWatchdog.StallThreshold,
(int)(DeliveryWatchdog.PollInterval * DeliveryWatchdog.StallThreshold).TotalSeconds);
// ---- liveness reporting: is anyone being told this host is alive? ----
// Stated next to the guard rails because it is one: without it the panel shows every device
// offline forever, and an operator's first conclusion from that picture is that acquisition
// is broken, not that nobody is reporting.
if (heartbeatCfg is { Enabled: true })
{
log.LogInformation(
"EFFECTIVE_PLAN_HEARTBEAT enabled=YES path={Path} intervalSec={I} "
+ "thresholdSec={T} — devices appear online in the panel only while beats arrive",
"/api/v1/sync/heartbeat",
heartbeatCfg.IntervalSeconds,
HeartbeatConfig.LaravelOnlineThresholdSeconds);
}
else
{
log.LogWarning(
"EFFECTIVE_PLAN_HEARTBEAT enabled=NO — this host will NOT report liveness. Devices will "
+ "show as offline in the panel and in the monitoring service even while acquisition and "
+ "delivery are healthy.");
}
log.LogInformation("EFFECTIVE_PLAN_END ============================================");
}
}