Skip to content

Instantly share code, notes, and snippets.

@chillitom
Created November 23, 2015 14:26
Show Gist options
  • Save chillitom/76cf68f529838a172043 to your computer and use it in GitHub Desktop.
Save chillitom/76cf68f529838a172043 to your computer and use it in GitHub Desktop.
Trying to use obj as message type for persistent actor in Akkling 0.2
open System
open Akkling
open Akkling.Persistence
type CounterChangedEvent =
{ Delta : int }
type CounterCommand =
| Inc
| Dec
| GetState
let sys = System.create "sys" (Configuration.defaultConfig())
let p = spawnPersist sys "p" <| fun mailbox ->
let rec loop state =
actor {
let! (msg:obj) = mailbox.Receive()
printfn "IsRecovering: %b Received: %A" (mailbox.IsRecovering()) msg
match msg with
| :? CounterChangedEvent as e -> return! loop (state + e.Delta)
| :? CounterCommand as c ->
match c with
| Inc -> return Persist [ {Delta = 1} ]
| Dec -> return Persist [ {Delta = -1} ]
| GetState ->
mailbox.Sender() <! state
return! loop state
| _ -> return! loop state
}
loop 0
p <! (Inc :> obj)
p <! (Dec :> obj)
p <? (GetState :> obj) |> Async.RunSynchronously |> printfn "State: %d"
()
(*
Wanting to be able to handle lifecycle events and other command types so using obj as message type, however get the following error.
throws..
(System.Exception: Cannot use persistent effects in context of non-persistent actor
at Microsoft.FSharp.Core.Operators.Raise[T](Exception exn)
at Akkling.Persistence.PersistentEffect`1.Akkling-Actors-IEffect-OnApplied[a](ExtActor`1 context, a message) in D:\github\Akkling\src\Akkling.Persistence\PersistentActor.fs:line 87
at Akkling.Persistence.FunPersistentActor`1.Handle(Object msg) in D:\github\Akkling\src\Akkling.Persistence\PersistentActor.fs:line 155
at Akkling.Persistence.FunPersistentActor`1.OnCommand(Object msg) in D:\github\Akkling\src\Akkling.Persistence\PersistentActor.fs:line 161
at Akka.Persistence.UntypedPersistentActor.ReceiveCommand(Object message)
at Akka.Persistence.UntypedPersistentActor.Receive(Object message)
at Akka.Actor.ActorBase.AroundReceive(Receive receive, Object message)
at Akka.Persistence.Eventsourced.<ProcessingCommands>b__22(Receive receive, Object message)
at Akka.Persistence.Eventsourced.AroundReceive(Receive receive, Object message)
at Akka.Actor.ActorCell.ReceiveMessage(Object message)
at Akka.Actor.ActorCell.Invoke(Envelope envelope),
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment