using Akka.Actor;
using Akka.Event;
using Microsoft.Extensions.Logging;
namespace ScadaLink.Host.Actors;
///
/// Subscribes to Akka.NET dead letter events, logs them, and tracks count
/// for health monitoring integration.
///
public class DeadLetterMonitorActor : ReceiveActor
{
private long _deadLetterCount;
public DeadLetterMonitorActor(ILogger logger)
{
Receive(dl =>
{
_deadLetterCount++;
logger.LogWarning(
"Dead letter: {MessageType} from {Sender} to {Recipient}",
dl.Message.GetType().Name,
dl.Sender,
dl.Recipient);
});
Receive(_ => Sender.Tell(new DeadLetterCountResponse(_deadLetterCount)));
}
protected override void PreStart()
{
Context.System.EventStream.Subscribe(Self, typeof(DeadLetter));
}
protected override void PostStop()
{
Context.System.EventStream.Unsubscribe(Self, typeof(DeadLetter));
}
}
///
/// Message to request the current dead letter count.
///
public sealed class GetDeadLetterCount
{
public static readonly GetDeadLetterCount Instance = new();
private GetDeadLetterCount() { }
}
///
/// Response containing the current dead letter count.
///
public sealed record DeadLetterCountResponse(long Count);