Elixir Supervisor "supervise" and "worker" deprecation errors

148 views Asked by At

What I am asking

I am reading through the tutorial linked below and all code works except for a single deprecation warning. I want to know how to rewrite the code to remove the deprecation warning.

The Tutorial

I have read through the tutorial at the URL below. It's the clearest and most direct instruction that I've found explaining how to wire up GenServers, Supervisors and Applications. The problem is the tutorial is slightly outdated.

Working with Supervisors in Elixir | Culttt

Here is the relevant piece:

defmodule Flakey.Service do
  use GenServer

  def start_link do
    GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
  end

  def init(:ok) do
    {:ok, :ok}
  end

  def check do
    GenServer.call(__MODULE__, {:check})
  end

  def handle_call({:check}, _from, :ok) do
    {:reply, :ok, :ok}
  end
end

defmodule Flakey.Supervisor do
  use Supervisor

  def start_link do
    Supervisor.start_link(__MODULE__, [])
  end

  def init(_) do
    children = [
      worker(Flakey.Service, [])
    ]

    supervise(children, strategy: :one_for_one)
  end
end

The code works - except for a deprecation warning when I start up the supervisor.

The error is:

Compiling 1 file (.ex)
warning: Supervisor.Spec.supervise/2 is deprecated. Use the new child specifications outlined in the Supervisor module instead
 lib/flakey.ex:34: Flakey.Supervisor.init/1
warning: Supervisor.Spec.worker/2 is deprecated. Use the new child specifications outlined in the Supervisor module instead
 lib/flakey.ex:31: Flakey.Supervisor.init/1
I want to know how to update the code to reflect the current API and remove the deprecation warning.

I've read the updated documentation here: https://hexdocs.pm/elixir/1.13/Supervisor.html

I've tried to change the original code to reflect the newer example but I haven't been able to do so without error. I've tried to tweak the syntax little by little to reflect what I perceive as the newer style. I get a cascade of errors and I figure it's easier to just ask someone that knows what they are doing.

I understand that the "worker" function is deprecated and that the children list now uses a different syntax. Still, I can't seem to figure it out.

I want to see a working example so I can work backwards to understand everything. It's easier to learn that way.

Thank you.

EDIT

The answer to this query is below as updated code.

defmodule Flakey.Service do
  use GenServer

  def start_link([])do
    GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
  end

  def init(:ok) do
    {:ok, :ok}
  end

  def check do
    GenServer.call(__MODULE__, {:check})
  end

  def handle_call({:check}, _from, :ok) do
    {:reply, :ok, :ok}
  end
end

defmodule Flakey.Supervisor do
  use Supervisor

  def start_link do
    Supervisor.start_link(__MODULE__, [])
  end

  def init(_) do
    children = [
      {Flakey.Service, []}
    ]

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

There are 0 answers