Fl4m3Ph03n1x

Fl4m3Ph03n1x

How to update from deprecated Supervisor.spec to new Supervisor.behaviour?

Background

I am trying to build a supervision tree in my app, where a given GenServer will have to supervise other GenServers. This is not an application, just a simple GenServer that needs to supervise others.

To achieve this I mainly focused my attention on the following article:

Code

The above article led me to the following code:

defmodule A.Server do
  use Supervisor

  alias B

  def start_link, do:
    Supervisor.start_link(__MODULE__, nil, name: __MODULE__)

  def init(nil) do
    children = [B]
    supervise(children, strategy: :one_for_one)
  end
end

So as you can see, my GenServer A is trying to supervise another called B.

Problem

The problem here is that everything in this example is deprecated. I tried following the instructions and read the new Supervisor docs, specially start_child which I think will be the correct substitute for the deprecated supervise, but unfortunately I don’t understand how I can apply this to my code.

Question

How can I update my code so it does not use deprecated functions?

Marked As Solved

LostKobrakai

LostKobrakai

A.Server is not a GenServer, it’s a supervisor. There are places where “supervising” processes from another GenServer make sense (see parent library), but in a general fashion I’d suggest not using that unless you really need it. Why do you think you need a GenServer to supervise other processes?

The docs for supervisors do have a section for module based supervisors:

Minimally adapted to your code that would be:

defmodule A.Supervisor do
  # Automatically defines child_spec/1
  use Supervisor

  def start_link(init_arg) do
    Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
  end

  @impl true
  def init(_init_arg) do
    children = [
      B # or more likely `B.Server`
    ]

    Supervisor.init(children, strategy: :one_for_one)
  end
end

Where Next?

Popular in Questions Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement