SupervisorStrategy策略不遵守maxNrOfRetries

时间:2020-07-28 06:51:44

标签: akka.net

我正在学习Akka.net,并且试图让演员在失败后重新启动。

使用以下命令,我希望“子项有错误”字符串显示5次(maxNrOfRetries),但是我只看到一次。

有人可以指出我所缺少的吗?我的理解是错误的吗?都是吗?

这是LINQPad脚本http://share.linqpad.net/t8x5wl.linq

谢谢

async Task Main()
{
    var system = ActorSystem.Create("MySystem");
    var parent = system.ActorOf(Props.Create<ParentActor>(), "parent");

    var child = await parent.Ask<IActorRef>(new GetChild());

    child.Tell(new HelloChild());
    child.Tell(new CauseChildToThrow());
    child.Tell(new HelloChild());
    child.Tell(new StopChild());
    
    await parent.Ask<object>(new EndProcess());
    "fin".Dump();
    
}


public class ParentActor : ReceiveActor
{

    public ParentActor()
    {
        var child = Context.ActorOf(Props.Create<ChildActor>(), "child");
        Receive<GetChild>(msg => Sender.Tell(child));
        Receive<EndProcess>(msg => Sender.Tell(new {}));
    }

    protected override SupervisorStrategy SupervisorStrategy()
    {
        return new OneForOneStrategy(
        maxNrOfRetries: 5,
        withinTimeRange: Timeout.InfiniteTimeSpan,
        localOnlyDecider: x =>
        {
            //return Directive.Resume; // skips the bad messsage?
            return Directive.Restart; // retries with the bad message?
        });
    }
}

public class ChildActor : ReceiveActor
{

    protected override void PreStart()
    {
        "child prestart".Dump();
    }

    protected override void PreRestart(Exception reason, object message)
    {
        "child pre restart".Dump(reason.Message + " " + message.ToString());
    }

    public ChildActor()
    {
        "child constructor".Dump();
        Receive<HelloChild>(msg => "Child says hi".Dump());
        Receive<StopChild>(msg =>
        {
            "child stopping gracefully".Dump();
            Context.Stop(Self);
        });
        Receive<CauseChildToThrow>(_ =>
        {
            "child has error".Dump();
            throw new MyException();
        });
    }
}


public class GetChild { }
public class HelloChild { }
public class StopChild { }
public class CauseChildToThrow { }
public class EndProcess { }

public class MyException : Exception{ }

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。垂死的演员需要将失败的消息重新发送回自己。

users.Brand.groups: (fields.E304) Reverse accessor for 'Brand.groups' clashes with reverse accessor for 'Creator.groups'.
        HINT: Add or change a related_name argument to the definition for 'Brand.groups' or 'Creator.groups'.
users.Brand.groups: (fields.E304) Reverse accessor for 'Brand.groups' clashes with reverse accessor for 'PlatformAdmin.groups'.
        HINT: Add or change a related_name argument to the definition for 'Brand.groups' or 'PlatformAdmin.groups'.
users.Brand.user_permissions: (fields.E304) Reverse accessor for 'Brand.user_permissions' clashes with reverse accessor for 'Creator.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'Brand.user_permissions' or 'Creator.user_permissions'.
users.Brand.user_permissions: (fields.E304) Reverse accessor for 'Brand.user_permissions' clashes with reverse accessor for 'PlatformAdmin.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'Brand.user_permissions' or 'PlatformAdmin.user_permissions'.
users.Creator.groups: (fields.E304) Reverse accessor for 'Creator.groups' clashes with reverse accessor for 'Brand.groups'.
        HINT: Add or change a related_name argument to the definition for 'Creator.groups' or 'Brand.groups'.
users.Creator.groups: (fields.E304) Reverse accessor for 'Creator.groups' clashes with reverse accessor for 'PlatformAdmin.groups'.
        HINT: Add or change a related_name argument to the definition for 'Creator.groups' or 'PlatformAdmin.groups'.
users.Creator.user_permissions: (fields.E304) Reverse accessor for 'Creator.user_permissions' clashes with reverse accessor for 'Brand.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'Creator.user_permissions' or 'Brand.user_permissions'.
users.Creator.user_permissions: (fields.E304) Reverse accessor for 'Creator.user_permissions' clashes with reverse accessor for 'PlatformAdmin.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'Creator.user_permissions' or 'PlatformAdmin.user_permissions'.
users.PlatformAdmin.groups: (fields.E304) Reverse accessor for 'PlatformAdmin.groups' clashes with reverse accessor for 'Brand.groups'.
        HINT: Add or change a related_name argument to the definition for 'PlatformAdmin.groups' or 'Brand.groups'.
users.PlatformAdmin.groups: (fields.E304) Reverse accessor for 'PlatformAdmin.groups' clashes with reverse accessor for 'Creator.groups'.
        HINT: Add or change a related_name argument to the definition for 'PlatformAdmin.groups' or 'Creator.groups'.
users.PlatformAdmin.user_permissions: (fields.E304) Reverse accessor for 'PlatformAdmin.user_permissions' clashes with reverse accessor for 'Brand.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'PlatformAdmin.user_permissions' or 'Brand.user_permissions'.
users.PlatformAdmin.user_permissions: (fields.E304) Reverse accessor for 'PlatformAdmin.user_permissions' clashes with reverse accessor for 'Creator.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'PlatformAdmin.user_permissions' or 'Creator.user_permissions'.

此外,我不得不删除对的呼叫

protected override void PreRestart(Exception reason, object message)
{
    "child pre restart".Dump(reason.Message + " " + message.ToString());
    Self.Tell(message);
}

此处http://share.linqpad.net/hv6w4g.linq

使用LINQPad脚本