tao

tao

Understanding "the process is not alive or there's no process currently associated with the given name"

Hi!

I’m writing an Elixir application with a DynamicSupervisor. Once the application has started, I’d like to add some children to this dynamic supervisor, but I receive an error when calling start_child():

** (Mix) Could not start application backend: exited in: Backend.Application.start(:normal, [])
    ** (EXIT) exited in: GenServer.call(Backend.Crawler.CrawlerSupervisor, {:start_child, {{Backend.Crawler.Server, :start_link, [[domain: "mastodon.social"]]}, :permanent, 5000, :worker, [Backend.Crawler.Server]}}, :infinity)
        ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started

The same error occurs when I run through the steps one at a time in iex.

I’m confused by this error because I know that the dynamic supervisor CrawlerSupervisor is alive (I can see it in the supervision tree). The GenServer I’m trying to start, Backend.Crawler.Server, does exist – but why would it need to be alive before I call start_child? Can someone clear this up for me?

Here’s the full code, if it’s helpful.

The application

defmodule Backend.Application do
  @moduledoc false

  use Application
  alias Backend.Crawler.CrawlerSupervisor
  alias Backend.{Instance, Repo}
  import Ecto.Query

  def start(_type, _args) do
    children = [
      Backend.Repo,
      BackendWeb.Endpoint,
      CrawlerSupervisor
    ]

    opts = [strategy: :one_for_one, name: Backend.Supervisor]

    case Supervisor.start_link(children, opts) do
      ok = {:ok, _pid} ->
        start_instance_servers()
        ok

      other ->
        other
    end
  end

  defp start_instance_servers() do
    domains = ["mastodon.social"]
    domains
    |> Enum.each(fn domain -> CrawlerSupervisor.start_child(domain) end)
  end
end

The dynamic supervisor

defmodule Backend.Crawler.CrawlerSupervisor do
  use DynamicSupervisor
  alias Backend.Crawler.Server

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

  @impl true
  def init(_opts) do
    DynamicSupervisor.init(strategy: :one_for_one)
  end

  def start_child(domain) do
    spec = {Server, domain: domain}
    DynamicSupervisor.start_child(__MODULE__, spec)
  end
end

The GenServer child process

defmodule Backend.Crawler.Server do
  use GenServer
  import Backend.Crawler.Util
  alias Backend.Crawler.Crawler

  # Client
  def start_link(domain) do
    GenServer.start_link(__MODULE__, domain, name: domain)
  end

  # Server
  @impl true
  def init(domain) do
    schedule_crawl()
    {:ok, domain}
  end

  @impl true
  def handle_cast(:crawl, state) do
    Crawler.run(state)
    schedule_crawl()
    {:noreply, state}
  end

  defp schedule_crawl() do
    interval = get_config(:crawl_interval_mins) * 60_000
    Process.send_after(self(), :crawl, interval)
  end
end

Marked As Solved

idi527

idi527

Seems like it wasn’t named since DynamicSupervisor.start_link/2,3 accepts init_arg as the second argument and option list – which includes :name – as the third argument:

DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__) from

https://hexdocs.pm/elixir/master/DynamicSupervisor.html?#module-module-based-supervisors

Also Liked

tao

tao

Amazing – thank you so much, that did it!

A lot of take-away points from this for a new Elixir dev like me.

  • Be really careful about the types of function arguments.
  • The name of a supervisor matters – it doesn’t seem like you can refer to it just by its module name if it isn’t named correctly.
  • Use a registry for dynamic supervisors :slight_smile:

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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 t...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement