[英]How to query for the MailboxProcessor state?
What I want to implement is to have an agent responsible for manipulating the Map of items. 我要实现的是要有一个负责处理项目地图的代理。 Thats an easy party, but now Im wondering how can a query on that Map ?
那是一个简单的聚会,但是现在我想知道如何在该Map上进行查询?
Take a look at this code: 看一下这段代码:
(* APPLIACATION STATE *)
let rec timeTable = Map.empty<string, TimeTableEntry>
let timeTableAgent = new TimeTableAgent(fun inbox ->
(* Internal functions *)
let validateCronExpr task =
try
task.CronExpr |> CrontabSchedule.Parse |> Success
with | ex -> Failure "Cron expression is invalid."
let rec schedule (timeTable : Map<string, TimeTableEntry>) (entry : TimeTableEntry) =
match validateCronExpr(entry) with
| Failure err -> Failure err
| Success _ -> match timeTable.ContainsKey(entry.Name) with
| false ->
let timeTable = timeTable.Add(entry.Name, entry)
Success "Task has been scheduled."
| true -> Failure "Task already exists."
(* message processing *)
let rec messageLoop (timeTable : Map<string, TimeTableEntry>) =
async {
let! message = inbox.Receive()
match message with
| Command.Schedule (entry, reply) ->
let timeTable = timeTable.Add(entry.Name, entry)
reply.Reply(schedule timeTable entry)
| Command.RecalculateOccurance (key, reply) -> reply.Reply(Success("OK"))
| Command.UnSchedule (key, reply) -> reply.Reply(Success("OK"))
return! messageLoop timeTable
}
// start the loop
messageLoop timeTable
)
timeTableAgent.Start()
let task = { Name = ""; CronExpr = "* * * * *"; Job = FileName(""); NextOccurance = DateTime.Now }
let messageAsync = timeTableAgent.PostAndAsyncReply(fun replyChannel -> Command.Schedule(task, replyChannel))
so now I would like to do something like this: 所以现在我想做这样的事情:
printf "%i" timeTable.Count
timeTable |> Map.iter (fun k v -> printf "%s" v.Name)
but the item count is 0 and the query does not return anything :( 但项目计数为0,查询不返回任何内容:(
I know that the state of the timetable is immutable, but I remember it is possible to just replace the immutable vars with the new instances.... 我知道时间表的状态是不可变的,但是我记得可以用新实例替换不可变的var。
Could someone help me on this, please ? 有人可以帮我吗?
Taking your example above you could do the following. 以上面的示例为例,您可以执行以下操作。
In the agents message handler look add another command 在代理消息处理程序中,查找添加另一个命令
(** Previous match clauses **)
| Command.GetCount(reply) ->
reply.Reply(timeTable.Count)
you can then use the command to query for that view of the agents state 然后,您可以使用命令查询代理状态的该视图
let timeTableCount = timeTableAgent.PostandReply(Command.GetCount)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.