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
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
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










