Created
June 8, 2014 14:05
-
-
Save josevalim/9cbad551db6ce5fb9cbc to your computer and use it in GitHub Desktop.
Table inheritance with supervisors
This file contains 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 Todo.Manager do | |
use GenServer | |
## Client API | |
@doc """ | |
Starts the `Todo.Manager`. | |
""" | |
def start_link(table, sup, event, opts \\ []) do | |
{:ok, pid} = GenServer.start_link(__MODULE__, {table, sup, event}, opts) | |
:ets.setopts(table, {:heir, self(), :ok}) | |
:ets.give_away(table, pid, :ok) | |
{:ok, pid} | |
end | |
end |
This file contains 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 Todo.Supervisor do | |
use Supervisor | |
def start_link do | |
Supervisor.start_link(__MODULE__, :ok, name: Todo.Supervisor) | |
end | |
@manager Todo.Manager | |
@list_sup Todo.List.Supervisor | |
@event_manager Todo.EventManager | |
def init(:ok) do | |
ets = :ets.new(@manager, | |
[:set, :protected, :named_table, {:read_concurrency, true}]) | |
children = [ | |
worker(GenEvent, [[name: @event_manager]]), | |
worker(Todo.Manager, [ets, @list_sup, @event_manager, [name: @manager]]), | |
supervisor(Todo.List.Supervisor, [[name: @list_sup]]) | |
] | |
supervise(children, strategy: :one_for_one) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment