sergey-chechaev

sergey-chechaev

Heavy work when starting DinamicSupervisor

Hello, I don’t find any common approach to start heavy work or children’s processes after DynamicSupervisor started. When we restart application or DynamicSupervisor is crash, we need to start DynamicSupervisor children’s processes or do heavy work as the handle_continue function when we use GenServer. In our project, we use start_link for this:

defmodule SomeDynamicSupervisor do

  use DynamicSupervisor
  alias SomeGenServer

  def start_link(_) do
    with link <- DynamicSupervisor.start_link(__MODULE__, %{}, name: __MODULE__) do
      Task.async(fn -> start_child_processors() end)
      link
    end
  end

  def init(_) do
    DynamicSupervisor.init(strategy: :one_for_one, max_restarts: 10)
  end

  def start_child_processors do
    SomeLogic.list_of_ids()
    |> Enum.each(&init_processor/1)
  end

  def init_processor(some_id) do
   
    opts = [name: NameGenerator.processor_registry_name(some_id)]

    DynamicSupervisor.start_child(__MODULE__, %{
      id: "#{SomeGenServer}_#{some_id}",
      start: {SomeGenServer, :start_link, [opts]}
    })
  end
end

What pitfalls does this approach have? Perhaps there is a better way. Can anyone share it with me?

Most Liked

LostKobrakai

LostKobrakai

Ordering for supervisor children is guaranteed. If the process to start your task is after the DynamicSupervisor child then it will only run once the DynamicSupervisor is indeed successfully started. You’re not making this simpler, by trying to mush together different responsibilities.

LostKobrakai

LostKobrakai

Why would you use a DynamicSupervisor is instead of a static/normal one here? It feels like a normal supervisor would suite your need better.

kwando

kwando

I would nest the DynamicSupervisor under another Supervisor with the strategy :rest_for_one and also add a setup Task after the DynamicSupervisor in that supervisor.

Supervisor(strategy: :rest_for_one)
  |----> DynamicSupervisor
  |----> SetupTask / GenServer

If your DynamicSupervisor crash it will be detected by the Supervisor.

  • It will kill the SetupTask / GenServer
  • restart DynamicSupervisor
  • restart your SetupTask / GenServer

Where Next?

Popular in Discussions Top

PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14027 124
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19652 166
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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