Skip to content

Instantly share code, notes, and snippets.

@LukeWood
Last active February 5, 2018 05:52
Show Gist options
  • Save LukeWood/1832a19baf643d4b73608660909b5069 to your computer and use it in GitHub Desktop.
Save LukeWood/1832a19baf643d4b73608660909b5069 to your computer and use it in GitHub Desktop.
Basic module that supports dynamically attached supervised children in elixir
defmodule Dynamic do
defdelegate start_link(), to: Dynamic.Server
defdelegate new, to: Dynamic.Supervisor
end
defmodule Dynamic.Server do
use GenServer
def start_link() do
GenServer.start_link(__MODULE__, %{})
end
end
defmodule Dynamic.Supervisor do
use Supervisor
def start(_opts, _args) do
Supervisor.start_link(__MODULE__, :ok, name: __MODULE__)
end
def init(:ok) do
children =[
worker(Bullet.Server, [], restart: :temporary)
]
opts = [
strategy: :simple_one_for_one
]
supervise(children, opts)
end
# End use api
def new do
Supervisor.start_child(__MODULE__, [])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment