Skip to content

Instantly share code, notes, and snippets.

@StevenXL
Created May 7, 2016 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenXL/faa6041ddd4507d0ace4be4f56f74d64 to your computer and use it in GitHub Desktop.
Save StevenXL/faa6041ddd4507d0ace4be4f56f74d64 to your computer and use it in GitHub Desktop.
GenServer Supervisor Revision 2
defmodule GenServerSupervisor do
use GenServer
# Client API #
def start_link(child_specs_list) do
GenServer.start_link(__MODULE__, child_specs_list)
end
# Server API #
def init(child_specs_list) do
Process.flag(:trap_exit, true)
child_specs_list |> start_children
end
# Helper Functions #
defp start_children([child_spec | rest]) do
case start_child(child_spec) do
{:ok, pid} ->
[{pid, child_spec} | start_children(rest)]
:error -> :error
end
end
defp start_children([]), do: []
defp start_child({module, fun, args}) do
case apply(module, fun, args) do
{:ok, pid} -> {:ok, pid}
_ -> :error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment