chattes

chattes

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)>

Showing Posts 1 to 5

saulecabrera

saulecabrera

The error that you’re experiencing is because in Podder.DynamicSupervisor/start_podcast_server you have defined the child spec as %{id: Podder, start: {Podder, :start_link, []}}, which means that when the dynamic supervisor tries to start the child, it will invoke Podder.start_link/0 with no arguments, but in your Podder module you have no such definition, instead you have Podder.start_link/1.

This can be fixed by either defining Podder.start_link/0 or by passing a param to the child spec in Podder.DynamicSupervisor/start_podcast_server (i.e. %{id: Podder, start: {Podder, :start_link, [:ok]}})

chattes

chattes OP

Sorry that was my mistake.
But actually I am do passing the args , in the updated code as so.

  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

But I still get the same error running in iex

 iex(5)> 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(5)> 
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

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/

chattes

chattes OP

Yeah , I think I understand now what i was doing wrong.
So basically Supervisor.start_link starts the main process along with the Child Processes in my case the Dynamic Supervisor.
I was duplicating the process name and I think that probably did not even start the child process.
Even i got the error, but was unable to wrap my head around what it is.

Its a lot clearer for me now.
Thanks for the help!

— 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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
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

Other Trending Topics Top

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
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews