Skip to content

Commit

Permalink
Start supervising actors
Browse files Browse the repository at this point in the history
  • Loading branch information
ashton314 committed Jan 24, 2025
1 parent a03d135 commit edcc708
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ Chorex - Choreographic Programming in Elixir

**Note:** this documentation is current as of 2024-07-22. The project is evolving rapidly, so this README may occasionally get out-of-sync with what the project can do.

Add `Chorex.Registry` to your application setup:

```elixir
# part of application startup; e.g. in a Phoenix application this
# would be in MyApp.Application located at lib/my_app/application.ex
children = [
{Registry, name: Chorex.Registry, keys: :unique}
]
```

Describe the choreography in a module with the `defchor` macro:

```elixir
Expand Down Expand Up @@ -84,6 +94,16 @@ def deps do
end
```

Add `Chorex.Registry` to your application setup:

```elixir
# part of application startup; e.g. in a Phoenix application this
# would be in MyApp.Application located at lib/my_app/application.ex
children = [
{Registry, name: Chorex.Registry, keys: :unique}
]
```

Note that this is *experimental software* and stuff *will* break. Please don't rely on this for anything production-grade. Not yet at least.


Expand Down
10 changes: 9 additions & 1 deletion lib/chorex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule Chorex do
# Trace all Chorex messages
@tron false

alias Chorex.RuntimeSupervisor

import FreeVarAnalysis
import WriterMonad
import Utils
Expand Down Expand Up @@ -43,6 +45,8 @@ defmodule Chorex do
def start(chorex_module, actor_impl_map, init_args) do
session_token = UUID.uuid4()

{:ok, supervisor} = RuntimeSupervisor.start_link(session_token)

actor_list = chorex_module.get_actors()

pre_config =
Expand All @@ -55,7 +59,10 @@ defmodule Chorex do

m when is_atom(a) ->
# Spawn the process
{:ok, pid} = GenServer.start_link(m, {a, m, self(), session_token})
{:ok, pid} = RuntimeSupervisor.start_child(supervisor,
m,
{a, m, self(), session_token})
# {:ok, pid} = GenServer.start_link(m, {a, m, self(), session_token})
{a, pid}
end
end
Expand Down Expand Up @@ -297,6 +304,7 @@ defmodule Chorex do
3. Name of the actor currently under projection. Atom.
4. Extra information about the expansion. Map. Contains:
- vars :: set of live variables
- actors :: list of all actor names
Returns an instance of the `WriterMonad`, which is just a 3-tuple
containing:
Expand Down
10 changes: 10 additions & 0 deletions lib/chorex/application.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Chorex.Application do
use Application

def start(_type, _args) do
children = [
{Registry, name: Chorex.Registry, keys: :unique}
]
Supervisor.start_link(children, name: Chorex.Supervisor, strategy: :one_for_one)
end
end
25 changes: 25 additions & 0 deletions lib/chorex/runtime_supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Chorex.RuntimeSupervisor do
@moduledoc """
DynamicSupervisor for Chorex actors.
"""

@registry_name Chorex.Registry

use DynamicSupervisor

@impl true
def init(_init_arg) do
DynamicSupervisor.init(strategy: :one_for_one)
end

def start_link(session_token) do
DynamicSupervisor.start_link(__MODULE__,
[],
name: {:via, Registry, {@registry_name, session_token}})
end

def start_child(sup_name, mod_name, arg) do
spec = %{id: mod_name, start: {GenServer, :start_link, [mod_name, arg]}}
DynamicSupervisor.start_child(sup_name, spec)
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Chorex.MixProject do

# Run "mix help compile.app" to learn about applications.
def application do
[]
[mod: {Chorex.Application, []}]
end

defp deps do
Expand Down

0 comments on commit edcc708

Please sign in to comment.