Fl4m3Ph03n1x

Fl4m3Ph03n1x

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?

Showing Posts 1 to 3

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
Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

Because I have a stateful library A that will have to supervise another stateful library B (which it depends on). The reason for this being that an application will use A but doesn’t care how A operates. That’s A’s responsibility. If A needs a stateful or stateless library(ies) to do it’s job, is only A’s concern.

Also, aren’t Supervisor’s GenServers in general?
Sorry for the confusion.

LostKobrakai

LostKobrakai

So I know you picked up the “stateful library” name from my post in another topic, but this one was meant only for demonstrational purpose. It’s not really a term used by anyone and won’t generally help people understand.

No. Both are processes, but not every process is automatically a GenServer. Basically there are processes, which are bare bone (created with spawn) and then there are various otp behaviours, which run in processes, like :gen_server (GenServer being the elixir wrapper), :supervisor (again Supervisor) but also :gen_event, :gen_statem, …, which don’t have wrapper modules in elixir core.

Generally I’d suggest you start with one application here (I’ll just take :a). The callback module of :a usually is A.Application. start/2 within that module commonly starts the root supervisor A.Supervisor. This one is started just via Supervisor API, without an actual module backing it up (A.Supervisor is just for naming the process). That supervisor (like any other) can now have children, which (in the general case) either are supervisors by themselves or workers (genservers, genstatem processes, …). Within the child supervisors you then have again children and with that nesting you can build up a tree of processes, with workers at the leaves and the rest being supervisors.

How you want to compose those is imo quite well explained here: The Hitchhiker's Guide to the Unexpected

The only reason for moving things into separate applications is either some self-contained code/functionality shall be shared between multiple projects depending on it or if (e.g. in prod) those applications might not run within the same beam instance or not even on the same host.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews