我正在玩 akka.net 并试图了解监督。我以为我明白了,但它没有像我期望的那样工作。
我尝试获取一个带有监视器和子 Actor 的小样本,其中监视器应在 child 出现异常时重新启动 child 。看起来 child 正在重新启动,但我不明白为什么因为代码似乎没有执行我的 SupervisorStrategy
。我更改策略以返回 Directive.Stop
以检查我是否可以停止 actor,但这也不起作用。所以现在看来我有一个不可阻挡的 Actor ,这是一件好事,只要我不想杀死它:)。运行示例的代码如下:
open Akka
open Akka.Actor
open Akka.Tools
open Akka.FSharp
open System
type MonitorMessage =
| Create
type ChildMessage =
| Ping
| Kill
let test() =
let systemName = "my-system"
let system = System.create systemName (Configuration.load())
let handleChildMessage = function
| Ping ->
printfn "Received %A" Ping
printfn "Pong: %A" (DateTime.Now.Ticks)
| Kill ->
1/0 |> ignore
let createChild parent id =
spawnOpt parent (id.ToString()) (actorOf handleChildMessage)
[ SpawnOption.SupervisorStrategy (Strategy.OneForOne (fun error ->
match error with
| _ ->
printfn "%A" error
Directive.Stop
)) ]
let handleMonitorMessage (actor:Actor<MonitorMessage>) message =
match message with
| Create ->
let sender = actor.Sender()
sender <! createChild actor (Guid.NewGuid())
let monitor = spawn system "monitor" (actorOf2 handleMonitorMessage)
let child = monitor <? Create |> Async.RunSynchronously
child <! Ping
child <! Kill
child <! Ping
test()
Console.ReadLine() |> ignore
最佳答案
- 您的
createChild
函数不会创建一个actor 作为monitor 的 child 。这是因为您已将 actor 系统传递给 spawnOpt 函数 - 这意味着,生成的 actor 将是顶级 actor(直接位于 actor 系统内核下)。您需要将其更改为spawnOpt parent
,以便将其创建为 parent 的 child 。 - 监督策略选项意味着拥有它的 Actor 将把它应用到它的 child 。因此,您需要为监视器而不是 child 设置它。
关于f# - 为什么我的 Actor 没有在 akka.net 中使用 F# 异常停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35688839/