2015-01-23 3 views
1

구현하고자하는 것은 아이템 맵을 조작하는 책임을 에이전트에게 부여하는 것입니다. 그게 쉬운 파티예요.하지만 지금은 그지도에 대한 쿼리를 어떻게 할 수 있을까요?MailboxProcessor 상태를 쿼리하는 방법은 무엇입니까?

이 코드를 살펴 보자

printf "%i" timeTable.Count 

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

을하지만, 항목 수는 0이고 쿼리가 아무것도 반환하지 않습니다 그래서

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

을 지금은 같은 것을 할 싶습니다 : (

나는 시간표의 상태가 불변이라는 것을 알고 있지만, 불변의 바스를 새로운 인스턴스로 대체하는 것이 가능하다는 것을 기억한다. ...

누군가 나를 도와 줄 수 있습니까?

+1

을 명시 보면 에이전트. 이 작업을 수행하는 올바른 방법은 AsyncReplyChannel을 사용하는 것입니다. 예를 들어 다음을 참조하십시오. https://msdn.microsoft.com/en-us/library/hh297109.aspx#fdtyuu –

답변

1

위의 예를 사용하면 다음을 수행 할 수 있습니다. 에이전트 메시지 처리기에서

는 에이전트가 외부 아무것도 변이 안 그런 다음 에이전트의 뷰를 쿼리하는 명령을 사용할 수 있습니다

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

다른 명령을 추가

let timeTableCount = timeTableAgent.PostandReply(Command.GetCount) 
관련 문제