Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dynamic supervision example
defmodule SomeModule do
def start(host, options) do
# We are assuming MainSup has been already started somewhere
# (ideally in a supervision tree).
{:ok, child_pid} = SomeSup.attach_to(MainSup, host, options)
end
end
defmodule SomeSup do
use Supervisor
def attach_to sup, host, options do
Supervisor.start_child(sup, [host, options])
end
def start_link do
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end
def init [] do
supervise([worker(SomeWorker, [], restart: :temporary)], strategy: :simple_one_for_one)
end
end
defmodule SomeWorker do
use GenServer
alias :gproc, as: GProc
def start_link host, options do
GenServer.start_link(__MODULE__, [host, options], name: {:via, GProc, {:n, :l, :conv_id}})
end
def init [host, options] do
#Let GProc know that this is a conversation
GProc.reg({:p, :l, :conversation})
{:ok, []}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.