Skip to content

Commit

Permalink
Merge pull request #702 from jorgef/dev
Browse files Browse the repository at this point in the history
Added support to the F# API for actor objects
  • Loading branch information
Horusiath committed Mar 3, 2015
2 parents 66e7498 + 951c76a commit f319c27
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
2 changes: 2 additions & 0 deletions documentation/wiki/FSharp API.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Paragraph above already has shown, how actors may be created with help of the sp
- `spawn (actorFactory : ActorRefFactory) (name : string) (f : Actor<'Message> -> Cont<'Message, 'Returned>) : ActorRef` - spawns an actor using specified actor computation expression. The actor can only be used locally.
- `spawnOpt (actorFactory : ActorRefFactory) (name : string) (f : Actor<'Message> -> Cont<'Message, 'Returned>) (options : SpawnOption list) : ActorRef` - spawns an actor using specified actor computation expression, with custom spawn option settings. The actor can only be used locally.
- `spawne (actorFactory : ActorRefFactory) (name : string) (expr : Expr<Actor<'Message> -> Cont<'Message, 'Returned>>) (options : SpawnOption list) : ActorRef` - spawns an actor using specified actor computation expression, using an Expression AST. The actor code can be deployed remotely.
- `spawnObj (actorFactory : ActorRefFactory) (name : string) (f : Quotations.Expr<(unit -> #ActorBase)>) : ActorRef` - spawns an actor using specified actor quotation. The actor can only be used locally.
- `spawnObjOpt (actorFactory : ActorRefFactory) (name : string) (f : Quotations.Expr<(unit -> #ActorBase)>) (options : SpawnOption list) : ActorRef` - spawns an actor using specified actor quotation, with custom spawn option settings. The actor can only be used locally.

All of these functions may be used with either actor system or actor itself. In the first case spawned actor will be placed under */user* root guardian of the current actor system hierarchy. In second option spawned actor will become child of the actor used as [actorFactory] parameter of the spawning function.

Expand Down
41 changes: 35 additions & 6 deletions src/core/Akka.FSharp/FsApi.fs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ module Logging =

module Linq =
open System.Linq.Expressions
open Microsoft.FSharp.Linq

let (|Lambda|_|) (e : Expression) =
match e with
Expand All @@ -285,13 +286,17 @@ module Linq =
| _ -> None

let (|Ar|) (p : System.Collections.ObjectModel.ReadOnlyCollection<Expression>) = Array.ofSeq p

type Expression =
static member ToExpression(f : System.Linq.Expressions.Expression<System.Func<FunActor<'Message, 'v>>>) =

let toExpression<'Actor>(f : System.Linq.Expressions.Expression) =
match f with
| Lambda(_, Invoke(Call(null, Method "ToFSharpFunc", Ar [| Lambda(_, p) |]))) ->
Expression.Lambda(p, [||]) :?> System.Linq.Expressions.Expression<System.Func<FunActor<'Message, 'v>>>
| Lambda(_, Invoke(Call(null, Method "ToFSharpFunc", Ar [| Lambda(_, p) |])))
| Call(null, Method "ToFSharpFunc", Ar [| Lambda(_, p) |]) ->
Expression.Lambda(p, [||]) :?> System.Linq.Expressions.Expression<System.Func<'Actor>>
| _ -> failwith "Doesn't match"

type Expression =
static member ToExpression(f : System.Linq.Expressions.Expression<System.Func<FunActor<'Message, 'v>>>) = toExpression<FunActor<'Message, 'v>> f
static member ToExpression<'Actor>(f : Quotations.Expr<(unit -> 'Actor)>) = toExpression<'Actor> (QuotationEvaluator.ToLinqExpression f)

module Serialization =
open Nessos.FsPickler
Expand Down Expand Up @@ -411,7 +416,7 @@ module Spawn =
actorFactory.ActorOf(props, name)

/// <summary>
/// Spawns an actor using specified actor computation expression, with custom actor Props settings.
/// Spawns an actor using specified actor computation expression, with custom spawn option settings.
/// The actor can only be used locally.
/// </summary>
/// <param name="actorFactory">Either actor system or parent actor</param>
Expand All @@ -434,6 +439,30 @@ module Spawn =
let spawn (actorFactory : ActorRefFactory) (name : string) (f : Actor<'Message> -> Cont<'Message, 'Returned>) : ActorRef =
spawnOpt actorFactory name f []

/// <summary>
/// Spawns an actor using specified actor quotation, with custom spawn option settings.
/// The actor can only be used locally.
/// </summary>
/// <param name="actorFactory">Either actor system or parent actor</param>
/// <param name="name">Name of spawned child actor</param>
/// <param name="f">Used to create a new instance of the actor</param>
/// <param name="options">List of options used to configure actor creation</param>
let spawnObjOpt (actorFactory : ActorRefFactory) (name : string) (f : Quotations.Expr<(unit -> #ActorBase)>)
(options : SpawnOption list) : ActorRef =
let e = Linq.Expression.ToExpression<'Actor> f
let props = applySpawnOptions (Props.Create e) options
actorFactory.ActorOf(props, name)

/// <summary>
/// Spawns an actor using specified actor quotation.
/// The actor can only be used locally.
/// </summary>
/// <param name="actorFactory">Either actor system or parent actor</param>
/// <param name="name">Name of spawned child actor</param>
/// <param name="f">Used to create a new instance of the actor</param>
let spawnObj (actorFactory : ActorRefFactory) (name : string) (f : Quotations.Expr<(unit -> #ActorBase)>) : ActorRef =
spawnObjOpt actorFactory name f []

/// <summary>
/// Wraps provided function with actor behavior.
/// It will be invoked each time, an actor will receive a message.
Expand Down
2 changes: 2 additions & 0 deletions src/core/Akka.FSharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Paragraph above already has shown, how actors may be created with help of the sp
- `spawn (actorFactory : ActorRefFactory) (name : string) (f : Actor<'Message> -> Cont<'Message, 'Returned>) : ActorRef` - spawns an actor using specified actor computation expression. The actor can only be used locally.
- `spawnOpt (actorFactory : ActorRefFactory) (name : string) (f : Actor<'Message> -> Cont<'Message, 'Returned>) (options : SpawnOption list) : ActorRef` - spawns an actor using specified actor computation expression, with custom spawn option settings. The actor can only be used locally.
- `spawne (actorFactory : ActorRefFactory) (name : string) (expr : Expr<Actor<'Message> -> Cont<'Message, 'Returned>>) (options : SpawnOption list) : ActorRef` - spawns an actor using specified actor computation expression, using an Expression AST. The actor code can be deployed remotely.
- `spawnObj (actorFactory : ActorRefFactory) (name : string) (f : Quotations.Expr<(unit -> #ActorBase)>) : ActorRef` - spawns an actor using specified actor quotation. The actor can only be used locally.
- `spawnObjOpt (actorFactory : ActorRefFactory) (name : string) (f : Quotations.Expr<(unit -> #ActorBase)>) (options : SpawnOption list) : ActorRef` - spawns an actor using specified actor quotation, with custom spawn option settings. The actor can only be used locally.

All of these functions may be used with either actor system or actor itself. In the first case spawned actor will be placed under */user* root guardian of the current actor system hierarchy. In second option spawned actor will become child of the actor used as [actorFactory] parameter of the spawning function.

Expand Down

2 comments on commit f319c27

@Petabridge-CI
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity Akka.NET :: Akka.NET PR Build Build 91 is now running

@Petabridge-CI
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity Akka.NET :: Akka.NET PR Build Build 91 outcome was FAILURE
Summary: System.Exception: xUnit failed for the following assemblies: D:\BuildAgent\work\49b164d63843fb4\src\core\Akka.Persistence.Tests\bin\Release\Akka.Persistence.Tests.dll, D:\BuildAgent\work\49b164d63843fb4\src\core\Akka.Tests\bin\Release\Akka.Tests.dll ... Build time: 00:10:56

Failed tests

Akka.Tests.dll: Akka.Tests.Actor.SupervisorHierarchySpec.A_supervisor_hierarchy_must_handle_failure_in_creation_when_supervision_strategy_returns_Resume_and_Restart: <no details avaliable>

Akka.Persistence.Tests.dll: Akka.Persistence.Tests.GuaranteedDeliveryCrashSpec.GuaranteedDelivery_should_not_send_when_actor_crashes: <no details avaliable>

Please sign in to comment.