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

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
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
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement