#Supervision tree

17 messages · Page 1 of 1 (latest)

plucky cedar
#

Hey, I'm having trouble getting a supervision tree set up.

I have the top-level 'application' file:

defmodule ERS do
  use Application
  
  @impl true
  def start(_type, _args) do
    ERS.Supervisor.start_link(name: ERS.Supervisor)
  end
end

I have a 'supervisor' file:

defmodule ERS.Supervisor do
  use Supervisor
  
  def start_link(opts) do
    Supervisor.start_link(__MODULE__, :ok, opts)
  end

  @impl true
  def init(:ok) do
    children = [Measurements, HTTP, Repo]

    opts = [strategy: :one_for_one, name: ERS.Supervisor]
    Supervisor.init(children, opts)
  end
end

And the three children modules that I want supervised. Each of them have use Supervisor. When I try to run, I get the following:

    ** (EXIT) Measurements.init/1 returned a bad value: {:ok, []}```

I'm not sure how to write the necessary functions. Any advice?
hidden epoch
#

Can you share ERS.Measurements as well?

placid sparrow
#

That is a lot of nested supervisors, why do you have so many?

hidden epoch
#

considering the names I think they're not meant to be supervisors

placid sparrow
hidden epoch
placid sparrow
#

quite possible, given how many supervisors they're making

plucky cedar
#

It is likely I have completely misunderstood
I just want processes to be started again if they crash
I think Ecto needs a supervisor, hence Repo, but I think I've gotten confused along the way

placid sparrow
#

Just put everything in one supervisor then

#

@plucky cedar what does ERS.Measurements look like?

#

(and usually, your application.ex file defines a supervisor)

hidden epoch
#

There's a start_link defined in Ecto.Repo so it can be supervised

placid sparrow
#

your repo should be in the supervisor though, usually like

children = [
  MyRepo,
  ... other stuff
]
plucky cedar
#

Ah, Repo doesn't have use Supervisor -- everything to do with the database has worked fine. I guess I just want to extend the supervisor to supervise other processes, too?

Measurements looks like this:

defmodule Measurements do
  use Supervisor
  require Logger
  # etc.
  
  def start_link(opts) do
    Supervisor.start_link(__MODULE__, :ok, opts)
  end
  
  def init(_options) do
    # Set stuff here...
    {:ok, []}
  end
end
hidden epoch
hidden epoch