using System.Text.Json;
using ClassNotify.Server.Data;
using ClassNotify.Server.Hubs;
using ClassNotify.Shared.Contracts;
using ClassNotify.Shared.Models;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
namespace ClassNotify.Server.Services;
///
/// 终端批量自检后台分发调度器 (P2.5 RC1-2)
/// 严格基于数据库 PendingDispatch 状态作为可靠队列,支持服务重启后自动续跑
/// 并发上限受控 (MaxDegreeOfParallelism = 10),设备真实离线判定为 Unreachable (绝不伪造 Failed/Healthy)
///
public sealed class SelfCheckDispatchWorker : BackgroundService
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly ISelfCheckDispatchNotifier _notifier;
private readonly ILogger _logger;
public SelfCheckDispatchWorker(
IServiceScopeFactory scopeFactory,
ISelfCheckDispatchNotifier notifier,
ILogger logger)
{
_scopeFactory = scopeFactory;
_notifier = notifier;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("SelfCheckDispatchWorker started.");
while (!stoppingToken.IsCancellationRequested)
{
try
{
var processed = await ProcessPendingBatchAsync(stoppingToken);
if (!processed)
{
// Wait for dispatch notification or fallback interval (2s)
await Task.WhenAny(_notifier.WaitAsync(stoppingToken), Task.Delay(2000, stoppingToken));
}
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
break;
}
catch (Exception ex)
{
_logger.LogError(ex, "Unexpected error in SelfCheckDispatchWorker loop");
await Task.Delay(2000, stoppingToken);
}
}
_logger.LogInformation("SelfCheckDispatchWorker stopped.");
}
private async Task ProcessPendingBatchAsync(CancellationToken ct)
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService();
var pendingRunIds = await db.DeviceSelfChecks
.AsNoTracking()
.Where(r => r.RunState == SelfCheckRunStates.PendingDispatch)
.OrderBy(r => r.RequestedAtUtc)
.Take(50)
.Select(r => r.Id)
.ToListAsync(ct);
if (pendingRunIds.Count == 0)
{
return false;
}
// Bounded concurrency dispatch (MaxDegreeOfParallelism = 10)
await Parallel.ForEachAsync(
pendingRunIds,
new ParallelOptions
{
MaxDegreeOfParallelism = 10,
CancellationToken = ct
},
async (runId, token) =>
{
try
{
await DispatchSingleRunAsync(runId, token);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error dispatching self-check run {RunId}", runId);
}
});
return true;
}
private async Task DispatchSingleRunAsync(string runId, CancellationToken ct)
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService();
var presenceService = scope.ServiceProvider.GetRequiredService();
var securityService = scope.ServiceProvider.GetRequiredService();
var hubContext = scope.ServiceProvider.GetRequiredService>();
var run = await db.DeviceSelfChecks.FirstOrDefaultAsync(r => r.Id == runId, ct);
if (run == null || run.RunState != SelfCheckRunStates.PendingDispatch)
{
return;
}
var device = await db.Devices.AsNoTracking().FirstOrDefaultAsync(d => d.Id == run.DeviceId, ct);
if (device == null)
{
run.RunState = SelfCheckRunStates.Unreachable;
run.CompletedAtUtc = DateTimeOffset.UtcNow;
await db.SaveChangesAsync(ct);
return;
}
// 1. Check device presence (Truthfulness guarantee: offline devices are Unreachable, not Failed)
bool isOnline = presenceService.IsOnline(device.Id, device.LastHeartbeatAt);
if (!isOnline)
{
run.RunState = SelfCheckRunStates.Unreachable;
run.OverallStatus = null;
run.CompletedAtUtc = DateTimeOffset.UtcNow;
await db.SaveChangesAsync(ct);
_logger.LogInformation("Device {DeviceId} is offline. SelfCheck {RunId} (Batch {BatchId}) marked Unreachable.",
run.DeviceId, run.Id, run.BatchId);
return;
}
// 2. Dispatch via tenant-qualified SignalR connection
var connId = securityService.GetDeviceConnectionId(device.Id);
if (string.IsNullOrEmpty(connId))
{
run.RunState = SelfCheckRunStates.Unreachable;
run.OverallStatus = null;
run.CompletedAtUtc = DateTimeOffset.UtcNow;
await db.SaveChangesAsync(ct);
return;
}
var cmdReq = new DeviceSelfCheckRequestDto
{
SelfCheckId = run.Id,
CorrelationId = run.CorrelationId,
RequestedAtUtc = run.RequestedAtUtc,
TimeoutSeconds = (int)Math.Max(5, (run.DeadlineAtUtc - DateTimeOffset.UtcNow).TotalSeconds)
};
var payload = JsonSerializer.Serialize(cmdReq);
var cmdDto = await securityService.CreateDeviceCommandAsync(
run.TenantId, run.DeviceId, DeviceCommandTypes.SelfCheck, payload, run.RequestedByUserId, "SuperAdmin", "127.0.0.1");
try
{
await hubContext.Clients.Client(connId).ReceiveRemoteCommand(cmdDto);
await securityService.MarkCommandDispatchedAsync(cmdDto.CommandId);
run.RunState = SelfCheckRunStates.Dispatched;
run.DispatchedAtUtc = DateTimeOffset.UtcNow;
run.DispatchAttemptCount++;
await db.SaveChangesAsync(ct);
_logger.LogInformation("Dispatched SelfCheck {RunId} (Batch {BatchId}) to Device {DeviceId} (Conn: {ConnId})",
run.Id, run.BatchId, run.DeviceId, connId);
}
catch (Exception ex)
{
run.DispatchAttemptCount++;
_logger.LogWarning(ex, "Failed to dispatch SelfCheck {RunId} to Device {DeviceId} (attempt {Attempt}/3)",
run.Id, run.DeviceId, run.DispatchAttemptCount);
if (run.DispatchAttemptCount >= 3)
{
run.RunState = SelfCheckRunStates.Unreachable;
run.OverallStatus = null;
run.CompletedAtUtc = DateTimeOffset.UtcNow;
}
await db.SaveChangesAsync(ct);
}
}
}