簡體   English   中英

如何查詢MailboxProcessor狀態?

[英]How to query for the MailboxProcessor state?

我要實現的是要有一個負責處理項目地圖的代理。 那是一個簡單的聚會,但是現在我想知道如何在該Map上進行查詢?

看一下這段代碼:

(* 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))

所以現在我想做這樣的事情:

printf "%i" timeTable.Count

timeTable |> Map.iter (fun k v -> printf "%s" v.Name) 

但項目計數為0,查詢不返回任何內容:(

我知道時間表的狀態是不可變的,但是我記得可以用新實例替換不可變的var。

有人可以幫我嗎?

以上面的示例為例,您可以執行以下操作。

在代理消息處理程序中,查找添加另一個命令

(** Previous match clauses **)
| Command.GetCount(reply) -> 
    reply.Reply(timeTable.Count)

然后,您可以使用命令查詢代理狀態的該視圖

let timeTableCount = timeTableAgent.PostandReply(Command.GetCount)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM