Can't get supervisor to work

Tom Brunoli Source

I'm trying to get my head around supervisors in OTP and such in erlang (although I am using elixir) and I can't seem to figure out why the main supervisor tree won't start.

I am making a http server with one listener worker, and a supervisor has handlers (dynamically started). I can start each of these on their own, and I can start the tree if the listener is the only thing in the tree.

Here's the supervisor definitions in elixir

defmodule HTTPServer.Supervisor do
  use Supervisor.Behaviour

  def start_link(port) do
    :supervisor.start_link({ :local, :sup }, __MODULE__, [port])

  def init(port) do
    tree = [
      worker(HTTPServer.Listener, [port], id: :listener_sup),
      supervisor(HTTPServer.HandlerSupervisor, [], shutdown: :infinity,
                                                   modules: [])
    supervise(tree, strategy: :one_for_one)

defmodule HTTPServer.HandlerSupervisor do
  use Supervisor.Behaviour

  def start_link(_) do
    IO.puts "starting handler supervisor"
    :supervisor.start_link({ :local, :handler_sup }, __MODULE__, [])

  def init(_) do
    tree = [ worker(HTTPServer.Handler, [], restart: :temporary,
                                            id: nil) ]
    supervise(tree, strategy: :simple_one_for_one)

  def start_child([socket]) do
    IO.puts "starting handler child"
    :supervisor.start_child(:handler_sup, [socket])

Something about the supervisor definition in the tree is wrong and I can't figure out what, and it seems to be basically the only thing holding my project back.

Thanks for any assistance!



answered 6 years ago Tom Brunoli #1

I figured out why it was having a fit by trying to add it dynamically. It turns out that the arity for HTTPServer.HandlerSupervisor.start_link should have been 0, not 1.

comments powered by Disqus