Skip to content

Instantly share code, notes, and snippets.

@M0ns1gn0r
Created May 25, 2016 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M0ns1gn0r/45ca2e0e22069c1ce38b394b235b0566 to your computer and use it in GitHub Desktop.
Save M0ns1gn0r/45ca2e0e22069c1ce38b394b235b0566 to your computer and use it in GitHub Desktop.
// Download NuGet packages "Akka" and "Akka.FSharp" to get these dlls.
#r @"System.Collections.Immutable.dll"
#r @"Newtonsoft.Json.dll"
#r @"Akka.dll"
#r @"Akka.FSharp.dll"
open Akka.Actor
open Akka.FSharp
let system = System.create "FSharpSystem" <| Configuration.defaultConfig ()
/// Creates an actor with a dynamic name.
let spawnChild (af : IActorRefFactory) (f : Actor<'Message> -> Cont<'Message, 'Returned>) =
let e = Linq.Expression.ToExpression(fun () -> new FunActor<'Message, 'Returned>(f))
let props = Props.Create e
af.ActorOf(props)
let coordinator (mailbox: Actor<string>) =
let rec loop () =
actor {
let! msg = mailbox.Receive()
logInfof mailbox "Coordinator received message %s." msg
match msg with
| "start" ->
spawn mailbox "child1" (actorOf2 (fun box _ -> box.Sender() <! "continue1")) <! "do"
| "continue1" ->
spawn mailbox "child2" (actorOf2 (fun box _ -> box.Sender() <! "continue2")) <! "do"
| "continue2" ->
spawn mailbox "child3" (actorOf2 (fun box _ -> box.Sender() <! "finish")) <! "do"
| "finish" ->
mailbox.Context.Stop mailbox.Self
| _ -> mailbox.Unhandled msg
return! loop ()
}
loop ()
let receiver =
let body mailbox msg =
logInfo mailbox "Receiver got a request."
let sender = mailbox.Sender()
spawnChild mailbox coordinator <! "start"
spawn system "receiver" (actorOf2 body)
receiver <! "incoming_request"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment