Last active
August 29, 2015 14:22
-
-
Save rhalukongor/61a6c3124128560b3057 to your computer and use it in GitHub Desktop.
Dynamic supervision example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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