View Source SpawnSdk.Actor behaviour (spawn_sdk v1.3.1)

Documentation for Actor.

Actor look like this:

defmodule MyActor do

use SpawnSdk.Actor,
  name: "joe",
  persistent: false,
  state_type: Io.Eigr.Spawn.Example.MyState,
  deactivate_timeout: 5_000,
  snapshot_timeout: 2_000

require Logger
alias Io.Eigr.Spawn.Example.{MyState, MyBusinessMessage}

defact sum(%MyBusinessMessage{value: value} = data}, %Context{state: state} = ctx) do
  Logger.info("Received Request...")

  new_value = (state.value || 0) + value

  %Value{}
  |> Value.of(%MyBusinessMessage{value: new_value}, %MyState{value: new_value})
  |> Value.reply!()
end

Summary

Functions

Sends a assynchronous message to group of actors.

Sends a message to the actor and returns the result.

Invokes a group of actors and returns all results.

Creates a reference to an actor so that it can be invoked.

Spawn an actor and return its reference.

Creates a group of actor reference so that it can be invoked

Creates a group of actor reference so that it can be invoked

Types

@type action() :: String.t()
@type actor() :: String.t()
@type context() :: SpawnSdk.Context.t()
@type data() :: term()
@type error() :: any()
@type group() :: ActorGroup.t()
@type opts() :: [
  action: String.t() | atom() | nil,
  data: any() | nil,
  delay: integer() | nil,
  metadata: map() | nil,
  parent: SpawnSdk.ActorRef.t() | nil,
  pooled: boolean() | nil,
  scheduled_to: DateTime.t() | nil,
  spawn: boolean() | false
]
@type parent() :: actor()
@type system() :: String.t()
@type value() :: SpawnSdk.Value.t()

Callbacks

Link to this callback

handle_action({}, context)

View Source
@callback handle_action(
  {action(), data()},
  context()
) ::
  value() | {:reply, value()} | {:error, error()} | {:error, error(), value()}

Functions

@spec cast(SpawnSdk.ActorRef.t(), opts()) :: {:ok, :async}

Sends a assynchronous message to group of actors.

Example:

alias SpawnSdk.Actor

Actor.ref("spawn-system", "joe")
|> Actor.cast(action: "sum", data: %MyData{value: 1})
Link to this function

group(actors, opts \\ [])

View Source
@spec group([SpawnSdk.ActorRef], opts()) :: SpawnSdk.ActorGroupRef.t()
@spec invoke(SpawnSdk.ActorRef.t(), opts()) :: {:ok, any()} | {:error, any()}

Sends a message to the actor and returns the result.

Example:

alias SpawnSdk.Actor

Actor.ref("spawn-system", "joe")
|> Actor.invoke(action: "sum", data: %MyData{value: 1})
@spec multi(SpawnSdk.ActorGroupRef.t()) :: [any()] | {:error, any()}

Invokes a group of actors and returns all results.

Example:

alias SpawnSdk.Actor

my_data = %MyData{value: 1}

Actor.ref("erlang-system", "joe", action: :sum, data: my_data)
|> Actor.to_group("erlang-system", "robert", action: :sum, data: my_data)
|> Actor.to_group("erlang-system", "mike", action: :sum, data: my_data)
|> Actor.to_group("eigr-system", "adriano", action: :sum, data: my_data)
|> Actor.to_group("eigr-system", "marcel", action: :sum, data: my_data)
|> Actor.to_group("spawn-elixir-system", "elias"", action: "calc", data: my_data)
|> Actor.multi()
@spec pub(SpawnSdk.ActorChannel.t(), opts()) :: :ok
Link to this function

ref(system, name, opts \\ [])

View Source
@spec ref(system(), actor(), opts()) :: SpawnSdk.ActorRef.t()

Creates a reference to an actor so that it can be invoked.

The first argument is ActorSystem name. The second argument is a Actor name.

The third argument is a keyword list of options:

  • spawn - a boolean indicating whether the actor should be generated or not. Default is false.

Example:

iex(spawn_a@127.0.0.1)1> SpawnSdk.Actor.ref("spawn-system", "joe")
%SpawnSdk.ActorRef{system: "spawn-system", name: "joe", opts: []}

To invoke an actor using the obtained reference you could do:

alias SpawnSdk.Actor

my_data = %MyData{value: 1}

Actor.ref("spawn-system", "joe")
|> Actor.invoke(action: "sum", data: my_data)
Link to this function

spawn(system, name, parent, opts \\ [])

View Source
@spec spawn(system(), actor(), parent(), opts()) :: SpawnSdk.ActorRef.t()

Spawn an actor and return its reference.

The first argument is ActorSystem name. The second argument is a Actor name. The third argument is a parent actor name.

Example:

iex(spawn_a@127.0.0.1)1> SpawnSdk.Actor.spawn("spawn-system", "joe", "unnamed_actor")
%SpawnSdk.ActorRef{system: "spawn-system", name: "joe", opts: []}

To invoke an actor using the obtained reference you could do:

alias SpawnSdk.Actor

my_data = %MyData{value: 1}

Actor.spawn("spawn-system", "joe", "unnamed_actor")
|> Actor.invoke(action: "sum", data: my_data)

Creates a group of actor reference so that it can be invoked

Example:

alias SpawnSdk.Actor

my_data = %MyData{value: 1}

Actor.ref("erlang-system", "joe", action: :sum, data: my_data)
|> Actor.to_group("erlang-system", "robert", action: :sum, data: my_data)
|> Actor.multi()
Link to this function

to_group(ref, system, actor, opts \\ [])

View Source

Creates a group of actor reference so that it can be invoked

Example:

alias SpawnSdk.Actor

my_data = %MyData{value: 1}

Actor.ref("erlang-system", "joe")
|> Actor.to_group("erlang-system", "robert")
|> Actor.to_group("erlang-system", "mike")
|> Actor.to_group("eigr-system", "adriano")
|> Actor.to_group("eigr-system", "marcel")
|> Actor.to_group("spawn-elixir-system", "elias")
|> Actor.multi(action: :sum, data: my_data)