diff --git a/README.md b/README.md index 5d0e1e4..d87146b 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/lib/chorex.ex b/lib/chorex.ex index dbf86bf..7971422 100644 --- a/lib/chorex.ex +++ b/lib/chorex.ex @@ -6,6 +6,8 @@ defmodule Chorex do # Trace all Chorex messages @tron false + alias Chorex.RuntimeSupervisor + import FreeVarAnalysis import WriterMonad import Utils @@ -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 = @@ -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 @@ -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: diff --git a/lib/chorex/application.ex b/lib/chorex/application.ex new file mode 100644 index 0000000..1b2f88e --- /dev/null +++ b/lib/chorex/application.ex @@ -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 diff --git a/lib/chorex/runtime_supervisor.ex b/lib/chorex/runtime_supervisor.ex new file mode 100644 index 0000000..0616f55 --- /dev/null +++ b/lib/chorex/runtime_supervisor.ex @@ -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 diff --git a/mix.exs b/mix.exs index 9dc5933..bb54ee8 100644 --- a/mix.exs +++ b/mix.exs @@ -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