I가 내가 다시 시작하고 다시 배우로 실패 메시지를 다시 시도하고 다음과 배우 :Akka.Net PreRestart 실행하지 않을 경우 비동기 처리기에서 예외
public class BuildActor : ReceivePersistentActor
{
public override string PersistenceId => "asdad3333";
private readonly IActorRef _nextActorRef;
public BuildActor(IActorRef nextActorRef)
{
_nextActorRef = nextActorRef;
Command<Workload>(x => Build(x));
RecoverAny(workload =>
{
Console.WriteLine("Recovering");
});
}
public void Build(Workload Workload)
{
var context = Context;
var self = Self;
Persist(Workload, async x =>
{
//after this line executes
//application goes into break mode
//does not execute PreStart or Recover
var workload = await BuildTask(Workload);
_nextActorRef.Tell(workload);
context.Stop(self);
});
}
private Task<Workload> BuildTask(Workload Workload)
{
//works as expected if method made synchronous
return Task.Run(() =>
{
//simulate exception
if (Workload.ShowException)
{
throw new Exception();
}
return Workload;
});
}
protected override void PreRestart(Exception reason, object message)
{
if (message is Workload workload)
{
Console.WriteLine("Prestart");
workload.ShowException = false;
Self.Tell(message);
}
}
}
내가 Persist
의 성공 핸들러 내부 throw되는 예외를 시뮬레이트하려고하지만 예외적으로 응용 프로그램이 중단 모드로 들어가고 PreRestart
후크가 호출되지 않습니다. 그러나 만약 BuildTask
메서드를 Task.Run
을 제거하여 동기식으로 만들면 예외가 발생하면 PreRestart
과 Recover<T>
메서드가 모두 호출됩니다.
누군가가이 패턴을 추천 받아야 할 부분과 내가 잘못 될 부분을 지적 할 수 있다면 정말 감사 할 것입니다.
지속성과 관련이 있습니까? 비동기 메서드에서 예외가 발생하면 내 전체 액터 시스템이 종료되고 지속성을 사용하지 않는 것을 상기합니다. 비동기 호출이 모든 예외를 포착하고 대신 몇 가지 오류 메시지가있는 Tell을 수행하도록 코딩이 끝났습니다. ("실패하자"철학으로 멋지게 앉아 있지는 않지만 적절한 해결책이었습니다.) – mwardm