private void FinishRecreate(Exception cause, ActorBase failedActor)
{
// need to keep a snapshot of the surviving children before the new actor instance creates new ones
var survivors = ChildrenContainer.Children;
try
{
try { ResumeNonRecursive(); }
finally { ClearFailed(); } // must happen in any case, so that failure is propagated
var freshActor = NewActor();
_actor = freshActor; // this must happen before postRestart has a chance to fail
if (ReferenceEquals(freshActor, failedActor))
SetActorFields(freshActor); // If the creator returns the same instance, we need to restore our nulled out fields.
UseThreadContext(() => freshActor.AroundPostRestart(cause, null));
if (System.Settings.DebugLifecycle)
Publish(new Debug(_self.Path.ToString(), freshActor.GetType(), "Restarted (" + freshActor + ")"));
// only after parent is up and running again do restart the children which were not stopped
foreach (var survivingChild in survivors)
{
try
{
survivingChild.Restart(cause);
}
catch (Exception e)
{
var child = survivingChild; //Needed since otherwise it would be access to foreach variable in closure
HandleNonFatalOrInterruptedException(() => Publish(new Error(e, _self.Path.ToString(), freshActor.GetType(), "restarting (" + child + ")")));
}
}
}
catch (Exception e)
{
ClearActor(_actor); // in order to prevent preRestart() from happening again
HandleInvokeFailure(new PostRestartException(_self, e, cause), survivors);
}
}