chattes

chattes

Dynamic Supervisor Fails to Start GenServer

Hello Experts,
I am just starting to play with Supervisors and I have created the Project using mix new podder --sup
I get the Following error when I try to Invoke a Process using the Dynamic Supervisor.
This is my code.

I am using Elixir 1.7.3.

Applicaiton.ex

defmodule Podder.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false
  use Application

  def start(_type, _args) do
    # List all child processes to be supervised
    children = [
      # Starts a worker by calling: Podder.Worker.start_link(arg)
      # {Podder.Worker, arg},
      %{
        id: Podder.DynamicSupervisor,
        start: {Podder.DynamicSupervisor, :start_link, []}
      }
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Podder.DynamicSupervisor]
    Supervisor.start_link(children, opts)
  end
end

Supervisor

defmodule Podder.DynamicSupervisor do
  use DynamicSupervisor

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

  @doc """
  Server Side
  """
  def init(_arg) do
DynamicSupervisor.init(strategy: :one_for_one)
  end

  def start_work do
{:ok, pid} = start_podcast_server
  end

  defp start_podcast_server do
spec = %{id: Podder, start: {Podder, :start_link, []}}
DynamicSupervisor.start_child(__MODULE__, spec)
  end
end

GenServer

defmodule Podder do
  use GenServer

  @moduledoc """
  Documentation for Podder.
  """

  @doc """
  Hello world.

  ## Examples

      iex> Podder.hello()
      :world

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

  def handle_call(:fetch, _from, state) do
    new_state = Map.put(state, "fetched", 1)
    {:reply, new_state, new_state}
  end

  def handle_call(:get_state, _from, state) do
    {:reply, state, state}
  end

  @doc """
  Client Implementation
  """
  def start_link(state) do
    GenServer.start_link(__MODULE__, state)
  end

  def fetch_episodes(pid) do
    GenServer.call(pid, :fetch)
  end

  def get_episodes(pid) do
    GenServer.call(pid, :get_state)
  end
end

I am trying this from iex

iex(4)> Podder.DynamicSupervisor.start_work
** (MatchError) no match of right hand side value: {:error, {:invalid_child_spec, {{Podder, :start_link, []}, :permanent, 5000, :worker, [Podder]}}}
    (podder) lib/podcastsupervisor.ex:16: Podder.DynamicSupervisor.start_work/0
iex(4)>

Marked As Solved

saulecabrera

saulecabrera

I tried replicating your code in a new mix project, may I ask how are you starting iex?

I tried running iex -S mix and the application is not even able to start because the current code is registering two processes with the same name (Podder.DynamicSupervisor), one for the top level supervisor and another for the actual DynamicSupervisor:

def start(_type, _args) do
    
    children = [
      %{
        id: Podder.DynamicSupervisor, #Here
        start: {Podder.DynamicSupervisor, :start_link, []}
      }
    ]

    opts = [strategy: :one_for_one, name: Podder.DynamicSupervisor] #Here
    Supervisor.start_link(children, opts)
  end

The name for the top level supervisor is normally omitted, unless you actually need it:

opts = [strategy: :one_for_one] #Here
Supervisor.start_link(children, opts)

With those changes I was able to succesfully call Podder.DynamicSupervisor.start_work

Also Liked

david_ex

david_ex

Have you tried

  defp start_podcast_server do
    spec = DynamicSupervisor.child_spec({Podder, %{}}, id: Podder)
    DynamicSupervisor.start_child(__MODULE__, spec)
  end

Typically, I think providing a child spec “by hand” is asking for trouble. If you’re interested, I’ve covered child specs and dynamic supervisors in more depth at http://davidsulc.com/blog/2018/07/10/pooltoy-a-toy-process-pool-manager-in-elixir-1-6-part-1-9/

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement